Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Knowledge fragment #42

Open
fi3ework opened this issue Aug 30, 2019 · 0 comments
Open

Knowledge fragment #42

fi3ework opened this issue Aug 30, 2019 · 0 comments

Comments

@fi3ework
Copy link
Owner

fi3ework commented Aug 30, 2019

  1. 有的时候 VSCode 的 TS langserver 会卡死,或者在修改了 tsconfig 后需要重启确保修改生效,可以通过 VSCode 提供的 command TypeScript: Restart TS server 重启即可(要在 TS/TSX 的文件中唤起控制台才会有对应的 command)。
  2. 如果 tsconfig 中没有制定 include / exclude / files 的话,会默认包含进所有的 TS 支持的文件

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 的话一定要指定。

  1. 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)

如果不指定 rootDir 的话,自动计算的 rootDir 可能会是包含了所有 TS 文件的的顶层文件夹

image

比如 es 文件夹指定的 rootDir 为 src,es2 指定的 rootDir 为 ./

参考:https://stackoverflow.com/questions/41007001/output-and-directory-structure-in-typescript

  1. 遇到 @types 包之间类型重复导致冲突的时候,可以简单粗暴的通过 yarn autoclean 来将不需要的 @types 包干掉解决冲突。https://yarnpkg.com/lang/zh-hans/docs/cli/autoclean/

  2. magic TypeScript

type K = 'foo' | 'bar'

// ❌
interface SomeInterface1 {
  [prop: K]: any
}
// An index signature parameter type cannot be a union type. Consider using a mapped object type instead.

// ❌
type SomeType1 = {
  [prop: K]: any
}
// An index signature parameter type cannot be a union type. Consider using a mapped object type instead.

// ❌
interface SomeInterface2 {
  [prop in K]: any
}
// 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 = {
  [prop in K]: any
}

// ✅
type Workaround = Record<K, any>

microsoft/TypeScript#24220

  1. 在 ambient 的声明文件中引入其他 types

https://stackoverflow.com/a/51114250
https://devblogs.microsoft.com/typescript/announcing-typescript-2-9-rc/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant