Closed
Description
TypeScript Version: 10/18/2019 nightly
Search Terms: optional chaining generic inference undefined
Code
declare function unbox<T>(box: { value: T | undefined }): T;
declare const su: string | undefined;
const b1 = { value: su?.length };
// Incorrect error - T inferred to be number | undefined
const v1: number = unbox(b1);
// No error
const b2 = { value: su?.length as number | undefined };
const v2: number = unbox(b2);
// No error
const b3: { value: number | undefined } = { value: su?.length };
const v3: number = unbox(b3);
Expected behavior: In all cases, T
should be inferred to be number
Actual behavior: When the undefined
part of the union of the type of value
originates in an optional chaining, it doesn't infer correctly
Related Issues: Nope