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

Give ts error when using optional chaining or nullish coalescing on nonnullable object #46268

Open
3 of 5 tasks
fa93hws opened this issue Oct 8, 2021 · 4 comments
Open
3 of 5 tasks
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@fa93hws
Copy link

fa93hws commented Oct 8, 2021

Suggestion

It sometimes happens that a variable / prop was original nullable, but evolved to be non-nullable, but there are places using it with ? not removed.

🔍 Search Terms

optional chainning, feature request, nonnullable

✅ 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

Gives ts error when optional chaining is used on a nonnullable object.

📃 Motivating Example

const a: {
  b: number;
} | undefined;

const bar = a?.b;
const foo = a ?? { b: 2 };

After few iteration, a becomes nonnullable:

const a: {
  b: number
}

However, we are not aware of those usages. This can help to reduce the output JS file size by removing unnecessary code.

💻 Use Cases

We want use this to remove unnecessary code and hence reduce the size of emitted JS file.

@nmain
Copy link

nmain commented Oct 8, 2021

Closely related to #11920

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Oct 8, 2021

Also, the question of "does this introduce undefined to the type doesn't already include undefined?" was a little tied in, and I think it's unfortunate that we didn't make it do that for the "defensive code when the types are wrong" use-case.

#36623

@soffes
Copy link

soffes commented Oct 11, 2021

I recently ran into a bug caused by this. I updated my case before the ?? and didn't realize it would never fall through. Coming from Swift, I expected this to be an error if I did it wrong so I didn't look closely at this part of the code when debugging.

I personally love errors whenever code is unreachable. Totally understand if this isn't always possible in TypeScript.

@andrewbranch
Copy link
Member

@andrewbranch andrewbranch added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Oct 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants