If the "files" and "include" are both left unspecified, the compiler defaults to including all TypeScript (.ts, .d.ts and .tsx) files in the containing directory and subdirectories except those excluded using the "exclude" property. JS files (.js and .jsx) are also included if allowJs is set to true. If the "files" or "include" properties are specified, the compiler will instead include the union of the files included by those two properties. Files in the directory specified using the "outDir" compiler option are excluded as long as "exclude" property is not specified.
进而导致 VSCode 提示包含文件过多的 warning(issue),所以 include 或 exclude如果有 node_modules 的话一定要指定。
rootDir 的作用:就是控制生成的 JS 的层级目录
There is rootDir compiler option, which is not used to specify input to a compiler. It’s used to control the output directory structure alongside with outDir.
根据官方对 rootDir 的默认值的解释:
(common root directory is computed from the list of input files)
}
// An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
// ❌
type SomeType1 = {
}
// An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
// ❌
interface SomeInterface2 {
}
// A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.
// A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.
// Cannot find name 'prop'.
TypeScript: Restart TS server
重启即可(要在 TS/TSX 的文件中唤起控制台才会有对应的 command)。进而导致 VSCode 提示包含文件过多的 warning(issue),所以 include 或 exclude如果有 node_modules 的话一定要指定。
根据官方对 rootDir 的默认值的解释:
如果不指定 rootDir 的话,自动计算的 rootDir 可能会是包含了所有 TS 文件的的顶层文件夹
比如 es 文件夹指定的 rootDir 为
src
,es2 指定的 rootDir 为./
参考:https://stackoverflow.com/questions/41007001/output-and-directory-structure-in-typescript
遇到 @types 包之间类型重复导致冲突的时候,可以简单粗暴的通过 yarn autoclean 来将不需要的 @types 包干掉解决冲突。https://yarnpkg.com/lang/zh-hans/docs/cli/autoclean/
magic TypeScript
// ❌ interface SomeInterface1 {
} // An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
// ❌ type SomeType1 = {
} // An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
// ❌ interface SomeInterface2 {
} // A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type. // A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type. // Cannot find name 'prop'.
// ✅ type SomeType2 = {
}
// ✅ type Workaround = Record<K, any>