TypeScript Version: 2.7.0-dev.20171230
Code
interface ProcessEnv {
[key: string]: string | undefined;
}
const env: ProcessEnv = { FOO: 'bar' };
const envVar: string | undefined = env.FOO;
if (typeof envVar === `string`) {
// error TS2532: Object is possibly 'undefined'.
console.log(envVar.slice(0));
}
if (typeof envVar === 'string') {
console.log(envVar.slice(0));
}
Expected behavior:
Should compile fine.
Actual behavior:
error TS2532: Object is possibly 'undefined'. typeof envVar === `string` does not narrow the string | undefined union down to string.
TypeScript Version: 2.7.0-dev.20171230
Code
Expected behavior:
Should compile fine.
Actual behavior:
error TS2532: Object is possibly 'undefined'.typeof envVar === `string`does not narrow thestring | undefinedunion down tostring.