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

strictNullCheck should detect unreachable default values and code after nullish operator #43040

Open
5 tasks done
marcandre opened this issue Mar 2, 2021 · 3 comments
Open
5 tasks done
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

@marcandre
Copy link

marcandre commented Mar 2, 2021

Suggestion

Using strictNullCheck should allow detection of code that isn't supposed to be reached according to type analysis

πŸ” Search Terms

strictNullCheck unreachble nullish default value

βœ… 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.

Yes, especially "Statically identify constructs that are likely to be errors."

⭐ Suggestion

This assumes that it is desired to detect errors with unreachable code due to type analysis (see microsoft/TypeScript-Website#1604 ).

With strictNullCheck turned on, some code that is meant to be unreachable and thus an error is not flagged.

πŸ“ƒ Motivating Example

The = 42 below is unreachable (playground) and is the sign that some code is likely to be an error, but nothing is flagged

class Example {
  foo({
    x = 42
  }: {
    x: number
  }) {
    return x
  }
}

Either the declaration should be changed to x?: number, or the default should be removed.

Another example (thanks to @jcalz) using the nullish operator.

function foo(x: number) { x ??= 42 }

Again, it would be helpful if an error or a warning would be raised as that code seems clearly to be a mistake.

@marcandre marcandre changed the title strictNullCheck should detect unreachable default values and useless nullish operator strictNullCheck should detect unreachable default values and code after nullish operator Mar 2, 2021
@MrAndersen1
Copy link

Just wanted to say your playground link has x?: number in it instead of the expected x: number

@marcandre
Copy link
Author

@MrAndersen1 thanks, updated.

@RyanCavanaugh RyanCavanaugh 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 Mar 2, 2021
@zoontek
Copy link

zoontek commented Sep 1, 2021

I can agree more on this feature. Currently, this is valid TS code:

let foo = "foo";
let bar = foo ?? 42;

bar will never be of type string | number, the compiler should return an error since foo is not of type string | null, string | undefined or string | null | undefined

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

4 participants