Skip to content

Optional chaining breaking function inference. #35274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ShanonJackson opened this issue Nov 21, 2019 · 4 comments
Closed

Optional chaining breaking function inference. #35274

ShanonJackson opened this issue Nov 21, 2019 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@ShanonJackson
Copy link

ShanonJackson commented Nov 21, 2019

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;

@fatcerberus
Copy link

fatcerberus commented Nov 21, 2019

This is quite bizarre. It would be understandable if there were some bug where it inferred different types based on the form of the expression passed to Maybe(). But here they're both assigned to a variable first, which should equalize things--yet it doesn't.

Since the type of cookie is already string | undefined, could it be that the optional chain is actually yielding a type of string | undefined | undefined and this is not being properly normalized... only to become observable later when used as an inference source for A.

@ajafff
Copy link
Contributor

ajafff commented Nov 21, 2019

Should be fixed by #34588 which is already available in typescript@next and will be in the next patch release.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 22, 2019
@RyanCavanaugh
Copy link
Member

Y'all know you can try "Nightly" in the Playground, right?

Duplicate #34579

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants