1. 当代码中导入了第三方库的时候,直接编译文件报错。例如以下目录:
1 | . |
其中demo.ts的代码如下:1
2
3import { of } from 'rxjs'
of(1, 2, 3).subscribe(x => console.log(x))
用以下方式运行:1
2
3
4pixcai@sierra:~/demo|⇒ ./node_modules/.bin/tsc demo.ts
node_modules/rxjs/internal/Observable.d.ts(82,59): error TS2693: 'Promise' only refers to a type,but is being used as a value here.
node_modules/rxjs/internal/types.d.ts(35,84): error TS2304: Cannot find name 'Iterable'.
node_modules/rxjs/internal/types.d.ts(39,6): error TS2304: Cannot find name 'Symbol'.
如果TypeScript装在全局,即npx tsc demo.ts,也是一样。
问题原因:tsconfig.json没生效。
解决办法:npx tsc -p tsconfig.json或npx tsc -p .或npx tsc --lib es2015,dom demo.ts。
2. document.getElementById()、document.querySelector()等函数返回的是Element类型,如果要取某些属性,比如:
1 | const target = document.querySelector('.target') |
则会报错:1
error TS2339: Property 'style' does not exist on type 'Element'.
解决办法:转换为HTMLElement类型,const target = <HTMLElement>document.querySelector('.target')。