-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
TS demands that abstract properties with type never are implemented #55121
Comments
I would say that TypeScript's behaviour here makes sense, the type definition says it needs a field of
If this is the goal, it would make more sense for it to use optional field syntax: abstract class P {
abstract foo?: number;
}
class C extends P {} // error C does not implement foo though unfortunately this doesn't work either. |
A property typed |
I understand never as meaning "this never happens". A function that always throws never returns. A function argument that is never cannot be passed. An abstract property of type never is never declared on a child class. I didn't explain well, but never is different to ?: - ?: Can be satisfied by prop = undefined where as never cannot. JS has two types of undefined on objects, prop not set, and prop set to value of undefined. Never would allow limiting to the first option only. I support respecting ?: syntax in addition |
TS may behave that way with object typings, because JS objects are extensible, and object types are essentially interfaces (in the way that interfaces exist in other languages, stating possibly only part of the object content). But generally never is applied strictly - it would be more useful to apply it strictly in classes derived from abstract classes. I don't think we are in an "interface" paradigm at that point. But I'm not totally sure - I'm sure we can extend classes with funky JS wonk, but is that allowed/recommended in TS? |
Ergo properties that are typed TypeScript does not have a way to describe a type that never has a specific property. For this you would require the "Exact Types" feature: #12936 |
Right, so it's a language issue |
This behavior follows from the plain definitions of
|
Yes, I think now that
But it does not mean absence of a thing. |
The first point is a direct consequence of the second: the type can’t be satisfied, it’s an empty set, so any operation that “produces” a value of type To be clear: the only reason absent properties can’t be typed |
Thanks for the explanation. Do you disagree with me when I say that a routine that returns never could be a routine that exits the application, or an infinite loop, instead of an exception? But you and Ryan have said that it must throw an exception, yet the other 2 examples are both accepted by the type checker. |
Which other 2 examples? |
No, I don’t disagree. I just consider those cases analogous (i.e. normal control flow diverges) so I say “throw an exception” as shorthand, as that is by far the most common case. The main point is that the code that “asks” for a value of type |
A func that exits the process (process.exit in node) or a func with an infinite loop |
Yes I think we agree. Please don't use that shorthand though :) it's confusing because it suggests that somehow never is related to exceptions, which it's really not besides casuality |
fwiw, I sometimes feel like But alas, that ship sailed long ago. 😄 |
Blame Scala 😉 |
Bug Report
🔎 Search Terms
abstract property inheritance never
🕗 Version & Regression Information
Current nightly v5.2.0-dev.20230720
⏯ Playground Link
Playground
💻 Code
🙁 Actual behavior
Error in class C, TS demands foo to be implemented.
🙂 Expected behavior
class C should not produce an error. Absence of foo matches the type constraint in P.
Comments
I suppose this is a bug - certainly I did not expect TS to ask me to implement a property with type never. A function with a type never argument would not let me pass it, and I expected abstract properties to be similar.
"Fixing" this provides the benefit of being able to create optional abstract properties, e.g.
Relates to #40635 (Optional abstract properties)
The text was updated successfully, but these errors were encountered: