Lu4*_*Lu4 50 typescript typescript2.0
根据这篇文章,打字稿2.0的打字系统已经改变,因此现在还不清楚如何附加自定义打字.我应该总是为此创建NPM包吗?
先感谢您!
Mis*_*hor 78
您可以为项目创建本地自定义类型,您可以在其中声明JS库的类型.为此,您需要:
创建目录结构以保留类型声明文件,以使您的目录结构看起来类似于:
.
??? custom_typings
? ??? some-js-lib
? ??? index.d.ts
??? tsconfig.json
Run Code Online (Sandbox Code Playgroud)在index.d.ts
文件中,为JS库添加一个声明:
declare module 'some-js-lib' {
export function hello(world: string): void
}
Run Code Online (Sandbox Code Playgroud)在以下compilerOptions
部分中添加对此类型声明的引用tsconfig.json
:
{
"compilerOptions": {
...
"typeRoots": ["./node_modules/@types", "./custom_typings"]
},
...
}
Run Code Online (Sandbox Code Playgroud)在代码中使用声明的模块:
import { hello } from 'some-js-lib'
hello('world!')
Run Code Online (Sandbox Code Playgroud) 归档时间: |
|
查看次数: |
25857 次 |
最近记录: |