Typescript strictNullChecks 和数组

Typescript strictNullChecks and arrays

我不完全理解 Typescript 在启用编译器选项 strictNullChecks 时的行为。似乎有时 Typescript(版本 2.4.1)理解 string[] 中的项目是 string,有时却不理解:

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

function f(myMap: MyMap) {
    const keys = Object.keys(myMap); // keys: string[] => Fine.
    for (let key of keys) { // key: string | undefined => Why?
        key = key as string // So a cast is needed.
        const strings = myMap[key]; // strings: string[] => Fine.
        const s = strings[0]; // s: string => Fine.

        // Error:
        // Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
        // Type 'undefined' is not assignable to type 'string'.
        useVarArgs(...strings);
    }
}
function useVarArgs(...strings: string[]) {
}

2017-07-14 更新:

只有在使用 downlevelIteration 时才会观察到这种奇怪的行为。我的 tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "outDir": "target",
    "downlevelIteration": true,
    "strictNullChecks": true
  }
}

经过进一步调查,我可以确认这不是 Typescript 问题。问题的根源是用于 IteratorResult<T> 的类型。我用过 @types/core-js 0.9.36:

interface IteratorResult<T> {
    done: boolean;
    value?: T;
}

value 是可选的,这在技术上是正确的,因为根据 the iterator protocolvalue "Can be omitted when done is true." 正如我的问题所证明的那样,可选性在实践中没有用不过

Typescript 附带的类型("es2015" 在 tsconfig.json 的 "lib" 部分中配置,即文件 lib.es2015.iterable.d.ts)采用更实用的方法,显然假设当 donetrue:

时,将不会使用 value
interface IteratorResult<T> {
    done: boolean;
    value: T;
}

为了解决这个问题,您可以编辑 @types/core-js 或用 Typescript 附带的库替换它。不过,替代品并非 100% 等效 - 请查看 this issue 进行讨论。