Skip to content
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

Incorrect type inference with nullish coalescing operator #37872

Closed
starsolaris opened this issue Apr 9, 2020 · 2 comments
Closed

Incorrect type inference with nullish coalescing operator #37872

starsolaris opened this issue Apr 9, 2020 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@starsolaris
Copy link

TypeScript Version: 3.8.3, Nightly

Search Terms: nullish coalescing operator, type inference

Code

(a: { a: {} | undefined}) => {
    const b = a?.a ?? "";    
}

Expected behavior:
type of b should inferred as: {} | ""
Actual behavior:
type of b is inferred as: {}

Playground Link: Provided

Related Issues: #36393

@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Apr 10, 2020
@DanielRosenwasser
Copy link
Member

This is because {} is a supertype of string. The object type has the same structure implied by {}, but explicitly rejects direct compatibility with primitives like string.

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants