vue2.x+Typescript报错

发布于 / Vue

问题场景

<script lang="tsx">
import { Vue, Component, Prop } from 'vue-property-decorator';
import { ElIssueIcon } from '@/components';
@Component({
  components: { ElIssueIcon }
})
export default class ElEpicTitle extends Vue {
  @Prop() title!: string;
  private render() {
    return (
      <div class='epic-container'>
        <ElIssueIcon type={4} />
        {this.title}
      </div>
    );
  }
}
</script>

出现如下错误

Property 'type' does not exist on type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<...>>'

解决方法

修改shims-tsx.d.ts文件

import Vue, { VNode } from 'vue';

declare global {
  namespace JSX {
    interface Element extends VNode {}
    interface ElementClass extends Vue {}
    interface IntrinsicElements {
      [elem: string]: any;
    }
    // new add
    interface ElementAttributesProperty {
      $props: {};
    }
  }
}

参考

https://github.com/vuejs/vue-cli/issues/2417

本文采用 CC BY-NC-SA 3.0 Unported 协议进行许可
本文链接: https://www.ahwgs.cn/vue2-xtypescriptbaocuo.html