Skip to content

Improve type inference by object parameters types #50685

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
5 tasks done
jozefchutka opened this issue Sep 8, 2022 · 3 comments
Closed
5 tasks done

Improve type inference by object parameters types #50685

jozefchutka opened this issue Sep 8, 2022 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@jozefchutka
Copy link

Suggestion

πŸ” Search Terms

inference

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

It would be nice if typescript could recognize/infer and apply safe guard for the following scenario:

type T1 = {foo:number | undefined}
type T2 = {foo:number}

const t1:T1 = {foo:123};
if(t1.foo !== undefined) {
    const t2:T2 = t1;
    console.log(t2);
}

Currently the compiler fails with the following (playground)

Type 'T1' is not assignable to type 'T2'.
  Types of property 'foo' are incompatible.
    Type 'number | undefined' is not assignable to type 'number'.
      Type 'undefined' is not assignable to type 'number'.
@MartinJohns
Copy link
Contributor

MartinJohns commented Sep 8, 2022

Essentially the same issue as #50651.

But this would easily lead to unsoundness. You could store t2 somewhere, and set the property to undefined via t1.

@jakebailey jakebailey added the Duplicate An existing issue was already created label Sep 12, 2022
@typescript-bot
Copy link
Collaborator

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

@ericbf
Copy link
Contributor

ericbf commented Mar 20, 2023

But this would easily lead to unsoundness. You could store t2 somewhere, and set the property to undefined via t1.

The same can be said for almost any valid typescript.

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