频道
bg

TypeScript Typing总结

coding九月 06, 20201mins
TypeScript

有两种类型的.ts 文件

  • scripts 其中定义的对象是全局的,只需要在tsconfig.jsoninclude属性所定义的路径中。scripts文件不能包含任何import/export 相关语句,否则他会变为modules。
  • modules 其中定义的对象是模块化的,需要使用import引入

声明文件H2

通常我们会把声明语句放到一个单独的文件(jQuery.d.ts)中,这就是声明文件,声明文件必需以 .d.ts 为后缀

jsx

// src/jQuery.d.ts
declare var jQuery: (selector: string) => any;

对于模块化的ts文件的声明文件,模块解析逻辑如下

  • package.json 中的 types 或 typings 字段指定的类型声明文件
  • 同目录d.ts
  • 可见的@types 目录

默认所有可见的@types“包会在编译过程中被包含进来。 node_modules/@types文件夹下以及它们子文件夹下的所有包都是可见的; 也就是说, ./node_modules/@types/../node_modules/@types/../../node_modules/@types/等等。

如果指定了typeRoots只有typeRoots下面的包才会被包含进来

参考:

https://www.typescriptlang.org/docs/handbook/module-resolution.html

https://stackoverflow.com/questions/42233987/how-to-configure-custom-global-interfaces-d-ts-files-for-typescript

https://ts.xcatliu.com/basics/declaration-files.html

评论


新的评论

匹配您的Gravatar头像

Joen Yu

@2022 JoenYu, all rights reserved. Made with love.