關(guān)于TypeScript的踩坑記錄
用字符串做下標報錯
代碼示例:
const person = {
name: '張三',
age: 10
};
function getValue(arg: string) {
return person[arg];
}錯誤信息:
Element implicitly has an ‘any’ type because expression of type ‘string’ can’t be used to index type ‘{ name: string; age: number; }’.
No index signature with a parameter of type ‘string’ was found on type ‘{ name: string; age: number; }’.ts(7053)
解決方法1:
在tsconfig.json中配置suppressImplicitAnyIndexErrors: true
{
"compilerOptions": {
"suppressImplicitAnyIndexErrors": true,
...
},
...
}解決方法2:
給person定義接口
const person = {
name: '張三',
age: 10
};
function getValue(arg: string) {
interface IPerson {
[key: string]: any
}
return (<IPerson>person)[arg];
}函數(shù)內(nèi)使用this報錯
代碼示例:
function test() {
this.title = 'hello';
}錯誤信息:
‘this’ implicitly has type ‘any’ because it does not have a type annotation.ts(2683)
解決方法:
在tsconfig.json中配置noImplicitThis: true
{
"compilerOptions": {
"noImplicitThis": true,
...
},
...
}找不到模塊XXX
代碼示例:
import CryptoJS from 'crypto-js';
錯誤信息:
Cannot find module ‘crypto-js’.ts(2307)
解決方法:
安裝對應的聲明文件
cnpm install --save-dev @types/crypto-js
模塊聲明文件搜索: https://microsoft.github.io/TypeSearch/
如果安裝不了或搜不到聲明文件,請看下面這種方法
引入模塊提示找不到聲明文件(接上一個問題)
示例代碼
import CryptoJS from 'crypto-js';
錯誤信息:

解決方法:
在src目錄下修改shims-vue.d.ts聲明文件,在末尾增加一行 declare module 'xxx模塊名';
shims-vue.d.ts文件內(nèi)容如下:
declare module '*.vue' {
import Vue from 'vue';
export default Vue;
}
declare module 'crypto-js';JSON直接解析localStorage值報錯
代碼示例:
JSON.parse(window.localStorage.getItem('token'));錯誤信息:
Argument of type ‘string | null’ is not assignable to parameter of type ‘string’.
Type ‘null’ is not assignable to type ‘string’.ts(2345)
解決方法:
定義一個指定類型為string的變量接收localStorage值
let token: string | null = window.localStorage.getItem('token');
if (token) {
JSON.parse(token);
}初始加載的組件未命名,瀏覽器打開頁面后控制臺報錯
代碼示例:
//index.vue
@Component
export default class extends Vue {}//router.ts
import Index from '@/views/index.vue';
const routes: Array<RouteConfig> = [
{
path: '/',
name: 'index',
component: Index,
}
];錯誤信息:
Invalid component name: “_class2”. Component names should conform to valid custom element name in html5 specification.
解決方法:
給初始加載的組件命名
//index.vue
@Component({
name: 'Index'
})
export default class extends Vue {}初始值未定義類型,后面賦值報錯
代碼示例:
export default class extends Vue {
private search = {
name: '',
types: [];
};
private typesChange(value: string[]) {
this.search.types = value; //這里報錯
}
}錯誤信息:
Type ‘string[]’ is not assignable to type ‘never[]’.
Type ‘string’ is not assignable to type ‘never’.
解決方法:
給初始賦值類型斷言
export default class extends Vue {
private search = {
name: '',
types: [] as string[]; //這里加斷言
};
private typesChange(value: string[]) {
this.search.types = value;
}
}在Vue原型上添加屬性使用時報錯
示例代碼
import Vue from 'vue'; import http from './http'; Vue.prototype.$http = http;
this.$http.post('/test', {}).then(
(resolve: any) => {
console.log(resolve);
},
(reject: any) => {
console.log(reject);
}
);錯誤信息:

解決方法:
在src目錄下新建vue.d.ts聲明文件
vue.d.ts文件內(nèi)容如下:
import Vue from 'vue';
declare module 'vue/types/vue' {
interface Vue {
$http: any;
}
}element-ui使用$message報錯
解決方法:
在src目錄下新建vue.d.ts聲明文件
vue.d.ts文件內(nèi)容如下:
import Vue from 'vue';
import { ElMessage } from 'element-ui/types/message';
declare module 'vue/types/vue' {
interface Vue {
$message: ElMessage;
}
}vue-cli里使用process對象報錯類型找不到
解決方法:
修改項目根目錄下的tsconfig.json文件中的compilerOptions.types值,新增node
compilerOptions.types配置內(nèi)容如下:
"compilerOptions": {
"types": ["webpack-env", "node"],
}vue-cli里tsconfig.json文件報錯
錯誤信息:
JSON schema for the typescript compiler's configuration file.
cannot find type definition file for 'webpack-env'.
解決方法:
沒找到好的解決方法,偶然間嘗試了下面的方法居然就不報錯了,這種方法不一定適用所有人的項目
修改項目根目錄下的tsconfig.json文件中的compilerOptions.types值,先新增"nodejs",再刪除"nodejs"
先新增:
"compilerOptions": {
"types": ["webpack-env", "nodejs"],
}再刪除:
"compilerOptions": {
"types": ["webpack-env"],
}邊踩坑,邊更新。。。
————————————分割線————————————
tsconfig.json配置解釋
{
"compilerOptions": {
"noEmitOnError": true // 編譯的源文件中存在錯誤的時候不再輸出編譯結(jié)果文件
}
}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue-router+vuex addRoutes實現(xiàn)路由動態(tài)加載及菜單動態(tài)加載
本篇文章主要介紹了vue-router+vuex addRoutes實現(xiàn)路由動態(tài)加載及菜單動態(tài)加載,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09
關(guān)于vue-property-decorator的基礎(chǔ)使用實踐
這篇文章主要介紹了關(guān)于vue-property-decorator的基礎(chǔ)使用實踐,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue項目中v-bind動態(tài)綁定src路徑不成功問題及解決
這篇文章主要介紹了Vue項目中v-bind動態(tài)綁定src路徑不成功問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
如何利用vue+vue-router+elementUI實現(xiàn)簡易通訊錄
這篇文章主要介紹了如何利用vue+vue-router+elementUI實現(xiàn)簡易通訊錄,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05

