2

我想切换到对我的 typescript 2.0 项目使用严格的空检查,但是我在输入我的一个依赖项的依赖项时遇到了一些困难(如果你愿意,可以依赖祖父母)。

更详细地说,我有依赖关系BC它们都依赖于A. 所有这些都是转译的 TS 项目,代码和类型都在一个lib文件夹中,它们还没有切换到严格的空检查。

相关输入A如下:

interface IInterface {
  [key: string]: string;
}

两者都使用,B如下C所示:

import { IInterface } from 'A/lib/iinterface';

interface IExtended extends IInterface {
  myOptionalProperty?: string
}

使用严格的空检查,这会产生以下编译错误:

node_modules/B/lib/extended.d.ts(4,3): error TS2411: Property 'myOptionalProperty' of type 'string | undefined' is not assignable to string index type 'string'
node_modules/C/lib/extended.d.ts(4,3): error TS2411: Property 'myOptionalProperty' of type 'string | undefined' is not assignable to string index type 'string'

那么问题是双重的:

  1. 为了遵守严格的检查,A 中的输入需要更改为:

    interface IInterface { [key: string]: string | undefined; }

    I am not sure if it is possible to override such a type, as this is not simply an extension of existing types. If possible, how is it done?

  2. If possible, how should it be included such that the typings in B and C are checked against my overridden typing, and not what is in their local node_modules directory?

4

1 回答 1

5

It's possible to just tell the compiler to skip the checks for the libs you are using.
The compiler options now have the skipDefaultLibCheck:

Don’t check a user-defined default library (*.d.ts) file’s validity.

And skipLibCheck:

Don’t check a the default library (lib.d.ts) file’s validity.

So if you compile using that option set to true then you shouldn't get errors for the libs you are using.

There's more about it in the what's new for 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. 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.

于 2016-10-10T09:58:04.070 回答