Skip to content

Optional chaining breaking function inference. #35274

Closed
@ShanonJackson

Description

@ShanonJackson

TypeScript Version: 3.7.2

Search Terms:
optional chaining
Code

interface Maybe<A> { };
export declare function Maybe<A>(value: A | null | undefined): Maybe<A>;
declare const ctx: {req?: {headers?: {cookie?: string}}}
const testopt = ctx.req?.headers?.cookie
const teststring = "" as string | undefined;
const test = Maybe(testopt) // Maybe<string | undefined> (wrong)
const test1 = Maybe(teststring ) // Maybe<string>  (correct)

Expected behavior:
const test should be of type Maybe<string> like const test1 because this is the behavior of inference on a function with this signature.

Actual behavior:
const test is of type Maybe<string | undefined;

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions