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

never type in optional parameter should return more helpful type errors #50940

Closed
5 tasks done
nopeless opened this issue Sep 25, 2022 · 2 comments
Closed
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

@nopeless
Copy link

nopeless commented Sep 25, 2022

Suggestion

never type in optional parameter currently behaves as expected, but returns a type error that is not semantic. Instead, TypeScript should adjust the error message to match the human developer's perspective.

The optional parameter is type undefined | T where T is the provided or inferred type of a generic. While there are existing guards (ex: extends) that polish the error message, types that produce a never in the parameter are common.

ex)

function func<T>(arg: T extends SomeGuard<infer R> ? T : never) {}

Many TypeScript developers will recognize "{...} is not assignable to never" as an indicator for "the provided type doesn't meet the constraints". This is the reason why this issue exists #23689

If the throws keyword derives from never, this error will persist and developers will not get the error message. Instead, they will get "{...} is not assigable to undefined"

Summary of points

  • Developers mostly assume the variable they pass in to an optional parameter is not undefined
  • If throws keyword get introduced as an extension to never, this issue will become a greater issue and will limit the developer's ability to read the custom error message

All reasons considered, I think it is beneficial for TypeScript to override the error message for optional parameters/properties when a union with never is present

TL;DR: Change error message for optional parameter incompatibility with never type

🔍 Search Terms

Optional parameter never

✅ 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

TS playground

📃 Motivating Example & Use Cases

Copied from playground

function optionalNever(arg?: never) {}

// Argument of type '{}' is not assignable to parameter of type 'undefined'.(2345)
optionalNever({})

// This makes perfect sense from the compilers perspective as never | undefined is undefined
// However, most developers who are supplying an argument don't expect that undefined can be an argument by itself

// # Example

type Option<T> = {
    func1: (arg: T) => any;
    func2: (arg: T) => any;
}

function pipe<Opts>(opts?: Opts extends Option<infer T> ? Option<T> : never) {}

const o = {
    func1: (a: string) => 1,
    func2: (a: number) => ""
}

// Argument of type '{ func1: (a: string) => number; func2: (a: number) => string; }' is not assignable to parameter of type 'undefined'.(2345)
pipe(o)

// Proposal: 
// Change the error message to
// Argument of type '$1' was provided but resolved to never for an optional parameter 'opts'
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Sep 26, 2022
@nopeless
Copy link
Author

Just as refresher, this issue is completely solved by the satisfies in future code bases. The problem is with existing code bases that utilize this "typeguard never" parameter

@nopeless
Copy link
Author

I realized this was invalid/bad practice.

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

2 participants