never
type in optional parameter should return more helpful type errors
#50940
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
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
whereT
is the provided or inferred type of a generic. While there are existing guards (ex:extends
) that polish the error message, types that produce anever
in the parameter are common.ex)
Many TypeScript developers will recognize "
{...}
is not assignable tonever
" as an indicator for "the provided type doesn't meet the constraints". This is the reason why this issue exists #23689If 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 toundefined
"Summary of points
throws
keyword get introduced as an extension tonever
, this issue will become a greater issue and will limit the developer's ability to read the custom error messageAll reasons considered, I think it is beneficial for TypeScript to override the error message for optional parameters/properties when a union with
never
is presentTL;DR: Change error message for optional parameter incompatibility with
never
type🔍 Search Terms
Optional parameter never
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
TS playground
📃 Motivating Example & Use Cases
Copied from playground
The text was updated successfully, but these errors were encountered: