I do not fully understand Typescript's behavior with compiler option strictNullChecks enabled. It seems that sometimes Typescript (version 2.4.1) understands that an item in a string[] is a string, and sometimes it does not:
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[]) {
}
Update 2017-07-14:
This strange behavior is only observed when using downlevelIteration. My tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"outDir": "target",
"downlevelIteration": true,
"strictNullChecks": true
}
}