0%

解决一些TS的错误

刚学 ts,转换 ts 文件的时候碰到了一些错误。mark 一下解决方法。

error TS2451

代码

1
2
3
4
5
6
7
8
let isDone: boolean = false;
// ES5:var isDone = false;

let count: number = 10;
// ES5:var count = 10;

let name: string = 'semliker';
// ES5:var name = 'semlinker';

错误信息

1
2
3
4
5
6
7
8
9
10
11
../../../../AppData/Roaming/npm/node_modules/typescript/lib/lib.dom.d.ts:19484:15 - error TS2451: Cannot redeclare block-scoped variable 'name'.

19484 declare const name: never;
~~~~

index.ts:7:5
7 let name: string = 'semliker';
~~~~
'name' was also declared here.

index.ts:7:5 - error TS2451: Cannot redeclare block-scoped variable 'name'.

解决方法,把 name 改成其他的值

error TS2585

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let isDone: boolean = false;
// ES5:var isDone = false;

let count: number = 10;
// ES5:var count = 10;

let namea: string = 'semliker';
// ES5:var name = 'semlinker';

const sym = Symbol();
let obj = {
[sym]: 'semlinker',
};

console.log(obj[sym]); // semlinker

错误信息

1
2
3
index.ts:10:13 - error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

10 const sym = Symbol();

解决方法

添加 tsconfig.json 文件,代码如下

1
2
3
4
5
6
7
8
9
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"strict": true,
"declaration": true,
"lib": ["ES2018", "DOM"]
}
}

然后用命令行运行

1
tsc -p tsconfig.json

参考文章