TypeScript 使用问题记录

设置 alias ,vscode 会有报错提示,vetur 插件的问题。

解决方式:
项目作为根目录不会有该问题。
或者改为使用 volar 插件

引入第三方的js库,不支持ts类型:

解决方式:

  • 尝试安装 @types/js库名,如果有,直接安装了就不会报错了。
  • 手写类型声明文件。

例如:
安装 jsencrypt ,引入之后报错:
Could not find a declaration file for module 'jsencrypt'. '/Users/sia/djwork/dd-inpass-psw/node_modules/jsencrypt/bin/jsencrypt.js' implicitly has an 'any' type.
Try npm install @types/jsencrypt if it exists or add a new declaration (.d.ts) file containing declare module 'jsencrypt';

vant3 less变量无法覆盖

vant3如果使用ts-import-plugin,会出现无法覆盖less变量的情况,建议还是使用 babel-import-plugin。

webpack require.context 无法使用 , 使用 process.env.xx 报错

安装依赖
npm i @types/webpack-env @types/node -D

如何定义 vue 组件 props 的类型

使用 PropType

import { PropType } from 'vue'
type FormItemConfig = {
    ...
}
someProp: {
    type: Object as PropType<FormItemConfig>
}

setup 获取 props 的值失败

指定 props 的类型

setup (props: FormProp, context){}

忽略 node_modules 里面的检查

首先设置 exclude :

"exclude": [
    "node_modules/**",
  ]

如果无效,再设置 compilerOptions :

"compilerOptions" : {
    ....,
    "skipLibCheck": true,
}
posted @ 2021-09-06 09:02  下小朋友  阅读(617)  评论(1编辑  收藏  举报