Skip to content

解决一些 TypeScript 错误

Published: at 03:17 PMSuggest Changes

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

error TS2451

代码

let isDone: boolean = false;
// ES5:var isDone = false;

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

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

错误信息

../../../../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

代码

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

错误信息

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 文件,代码如下

{
  "compilerOptions": {
    "target": "ES6",
    "module": "CommonJS",
    "strict": true,
    "declaration": true,
    "lib": ["ES2018", "DOM"]
  }
}

然后用命令行运行

tsc -p tsconfig.json

参考文章


Previous Post
Mac 免密登录
Next Post
使用 Yarn 更新包