简体   繁体   English

范围有限的 Typescript strictNullChecks

[英]Typescript strictNullChecks with limited scope

I'm using the Ionic v2 beta with Typescript, and would love to use --strictNullChecks .我正在将 Ionic v2 beta 与 Typescript 一起使用,并且很想使用--strictNullChecks However, dependencies of my project, such as @angular/core, cause compilation errors when I put strictNullChecks: true in my tsconfig.json .然而,我的项目的相关性,比如@角/核心,导致编译错误,当我把strictNullChecks: true在我的tsconfig.json

Is there a way to declare a directory / file / module / class as abiding by strictNullChecks, even when the overall project cannot be compiled with the flag?有没有办法声明一个目录/文件/模块/类遵守strictNullChecks,即使整个项目不能用标志编译?

It seems that, since --strictNullChecks is not the default, it will have extremely limited use (despite being an extremely useful feature) as a result of dependencies that were written without the flag.似乎,因为--strictNullChecks不是默认值,所以它的使用非常有限(尽管它是一个非常有用的功能),因为依赖项是在没有标志的情况下编写的。

If you'd like to scope strictNullChecks to a particular directory within your project, you can do it with an extends clause in your tsconfig.json .如果您想将strictNullChecks范围strictNullChecks到项目中的特定目录,您可以使用tsconfig.jsonextends子句来tsconfig.json For example, if your project looks like this:例如,如果您的项目如下所示:

a/
  code.ts
b/
  other.ts
tsconfig.json

And you want to enable strict null checking within a , you could add a/tsconfig.json :并且您想在a启用严格的空值检查,您可以添加a/tsconfig.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "strictNullChecks": true
  }
}

When you run tsc from inside directory a , you'll get strict null checks for a/code.ts .当您从目录a运行tsc ,您将对a/code.ts进行严格的空检查。

This approach isn't without drawbacks:这种方法并非没有缺点:

  • If a/code.ts imports ../b/other.ts , then you'll get strict null checking for b/other.ts , too.如果a/code.ts导入../b/other.ts ,那么您也会对b/other.ts进行严格的空检查。
  • If you run tsc from the root directory, you won't get strict null checking for a/code.ts .如果从根目录运行tsc ,则不会对a/code.ts进行严格的空检查。

You're effectively creating a new, strict, sub-project within your larger project.您正在有效地在更大的项目中创建一个新的、严格的子项目。 You'd want to make sure to run tsc for both projects as part of your build process.作为构建过程的一部分,您需要确保为两个项目运行tsc It's not perfect, but it might help you migrate a large project to strictNullChecks bit by bit.它并不完美,但它可能会帮助您将大型项目strictNullChecks一点地迁移到strictNullChecks

Is there a way to declare a directory / file / module / class as abiding by strictNullChecks, even when the overall project cannot be compiled with the flag有没有办法声明一个目录/文件/模块/类遵守strictNullChecks,即使整个项目不能用标志编译

No.没有。

Note: There is a feature request to ignore errors for certain files that hasn't come to fruitation : https://github.com/Microsoft/TypeScript/issues/11051注意:有一个功能请求可以忽略某些尚未实现的文件的错误: https : //github.com/Microsoft/TypeScript/issues/11051

If your dependencies are all in .js and .d.ts files (that is, not ts source) then you can use a flag and tell the compiler to skip checking your libs, which will result in no errors from the libs.如果您的依赖项都在.js.d.ts文件中(即,不是 ts 源),那么您可以使用一个标志并告诉编译器跳过检查您的库,这将导致库中没有错误。

There are two new flags for that:两个新标志

skipLibCheck:跳过库检查:

Don't check a the default library (lib.d.ts) file's validity不要检查默认库 (lib.d.ts) 文件的有效性

skipDefaultLibCheck:跳过默认库检查:

Don't check a user-defined default library (*.d.ts) file's validity不要检查用户定义的默认库 (*.d.ts) 文件的有效性

And a bit more about it in the What's new in Typescript 2 : 在 Typescript 2 中的新功能中有更多关于它的信息

TypeScript 2.0 adds a new --skipLibCheck compiler option that causes type checking of declaration files (files with extension .d.ts) to be skipped. TypeScript 2.0 添加了一个新的 --skipLibCheck 编译器选项,可以跳过声明文件(扩展名为 .d.ts 的文件)的类型检查。 When a program includes large declaration files, the compiler spends a lot of time type checking declarations that are already known to not contain errors, and compile times may be significantly shortened by skipping declaration file type checks.当程序包含大型声明文件时,编译器会花费大量时间对已知不包含错误的声明进行类型检查,并且通过跳过声明文件类型检查可能会显着缩短编译时间。

Since declarations in one file can affect type checking in other files, some errors may not be detected when --skipLibCheck is specified.由于一个文件中的声明会影响其他文件中的类型检查,因此在指定 --skipLibCheck 时可能无法检测到某些错误。 For example, if a non-declaration file augments a type declared in a declaration file, errors may result that are only reported when the declaration file is checked.例如,如果非声明文件扩充了声明文件中声明的类型,则可能会导致仅在检查声明文件时报告的错误。 However, in practice such situations are rare.然而,在实践中,这种情况很少见。

No.没有。

strictNullChecks works by removing the undefined and null values from the domains of all types. strictNullChecks通过从所有类型的域中删除undefinednull值来工作。 It's a global setting that changes how all interpretations of types behave.这是一个全局设置,它改变了所有类型解释的行为方式。

There's not even a coherent definition of what it would mean to have one file or module or class where this flag is off -- it'd be like having the laws of physics be different in one country versus another, even though you can freely ship goods and people between those countries.对于一个文件、模块或类在这个标志关闭的情况下意味着什么,甚至没有一个连贯的定义——这就像一个国家的物理定律与另一个国家不同,即使你可以自由发送这些国家之间的货物和人员。

You can use typescript-strict-plugin which allows you to turn on strict mode in specific files or directories.您可以使用typescript-strict-plugin ,它允许您在特定文件或目录中打开严格模式。 It does not work with specific tsc options though.但是它不适用于特定的 tsc 选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM