-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Function returning a key with type never can be assigned to a typed Object #58565
Comments
|
To clarify since this is a common point of confusion: |
Or a property that throws upon access. It's fine to create a throwing getter. |
Thanks for your fast replies :)
That is exactly my point. Yet, the example creates such a value of the impossible I fail to see, that assigning an impossible type to a possible type can be a correct, type safe assignment. I feel like, I am missing something here? The |
The problem with the example, and why itβs not an error as written, is because |
Itβs a bit weird, but the mathematical explanation is that itβs the same as how the empty set is a subset of all other sets. More prosaically, if you have a function of type |
Long story short: We can close this issue as works as intended, right? Thanks a lot for your detailed answers! :) Nevertheless, I think it would be useful, if assigning an impossible value to something would be a caught error by the compiler. I see no intentional use of writing a program, whose static types cannot reflect its runtime types. From my point of view, programming languages should be useful and help me to prevent errors: In the example Do you think a feature request for that would be valid? If not, I will trust your judgement π P.S.:
I think you missed the implicit cast due to the type annotation in the function header π P.P.S: Just in case someone finds this helpful: Instead of mapping the not JSONable keys to |
You still seem to mistakingly believe that having a type T = { value: never }
const t: T = { get value(): never { throw new Error() } } |
That's not a cast, it's just a normal return type annotation. If The bottom type means you can never access this value without throwing. As long as that invariant is maintained, then any assignment from |
The ideal solution for this use case would be #23689, but I don't know if we'll ever get it |
awesome, I learned something. I agree that it works as intended and #23689 is the feature I am looking for. Thanks a lot for taking the time! |
π Search Terms
never, function, assignable, assign
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.5.0-dev.20240518#code/C4TwDgpgBAMghgIwPYCcIqgXigbygd1QGsAuKACgEosA+KAOwFcBbBdKAXwG4AoUSWHABeIeMjQZseQilIMIAN3bcePAGaN6AY2ABLJPSgBzCMDGp05AM7pdcADa6hEACYA1B4whkrwFLvojSjJ4EXMJXB4oKDRgRhRDACkAZQB5ADkAOjA4FBtrWwcnVw97L0peDlV7UyhmEAB1YnQQxAsUXnqm2XZsEzM2iXIAIkyx4YqoAHopqGAAC10rKCt5pEZ7FwYkYCg2KAd7JHxXABoD5bAUJEgUUChhmSJhqCW58Gh6RXYAh9DRQboYY8LrNFCZJ5ULjTWb4XT2exzebXfAHXYoTR6ZgQHhAA
π» Code
π Actual behavior
The code compiles without an error.
π Expected behavior
The code should not compile, as we assign
work: never
to a real object.Additional information about the issue
My real use case is, that I want to check, if a type is serializable with JSON. Inspired by the blog mastering-type-safe-json-serialization-in-typescript, I wanted to write a small library like the following
And the real code would look like the following:
The text was updated successfully, but these errors were encountered: