You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionacceptObjectOnly(_: object){}acceptObjectOnly(3)// error: as expectedconstemptyInterface: {}=3acceptObjectOnly(emptyInterface)// ok: but error is expectedclassMyClass{}functionacceptMyClassOnly(_: MyClass){}acceptMyClassOnly(3)// ok: but error is expected
Ryan Cavanaugh (@RyanCavanaugh) could you, please, point to exact location where it says that interfaces (which may be used for any JavaSript types except null and undefined) are implicitly convertible to object (AFAIK, classes are derived from object)?
And why, in this case, the empty interface is not implicitly convertable into number for example?
IMHO, it's a bug. And I can't see a reason to have this as a feature.
functionacceptObjectOnly(_: object){}acceptObjectOnly(3)// error: as expectedconstemptyInterface: {}=3acceptObjectOnly(emptyInterface)// ok: but error is expectedclassMyClass{}functionacceptMyClassOnly(_: MyClass){}acceptMyClassOnly(3)// ok: but error is expectedfunctionacceptNumberOnly(_: number){}acceptNumberOnly(emptyInterface)// error: as expected
I'm not sure how the rule is being applied in this case - definitely something strange with the empty interface that doesn't seem to be covered in the docs:
BTW, it could be also non-empty interface. IMHO, the problem is that TS considers interfaces are derived from object. Which is not true. For example, string is not an object but it may implement some interfaces.
More fun
functionacceptObjectOnly(_: object){}acceptObjectOnly(3)// error: as expectedinterfaceSomeInterface{toString(): string}constsomeInterface: SomeInterface=3acceptObjectOnly(someInterface)// ok: but error is expectedclassMyClass{}functionacceptMyClassOnly(_: MyClass){}acceptMyClassOnly(3)// ok: but error is expectedfunctionacceptNumberOnly(_: number){}acceptNumberOnly(someInterface)// error: as expected
You'll have to read the document and understand it holistically. No documentation can cover the entire matrix of all things you might do and comprehensively specify every outcome; there are implicit isomorphisms (interface X { } and { } are very similar for example).
object is assignable from { } for mostly historical reasons.
And why, in this case, the empty interface is not implicitly convertable into number for example?
You're asking why type relationships are directional?
Ryan Cavanaugh (@RyanCavanaugh) I understand the historical context but it doesn't mean that the problem shouldn't be fixed.
For example, we may have a strict mode when interfaces are not convertible to object. Or we may have special interfaces which aren't convertible to object (for backward compatibility). Something like
Please log a suggestion if you have a proposal for what should be done and how/why (i.e. motivating use cases where this would provide value above the status quo). Thanks!
Activity
RyanCavanaugh commentedon Aug 3, 2018
This is covered, multiple times, in the FAQ.
sergey-shandar commentedon Aug 3, 2018
Not this one https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-all-types-assignable-to-empty-interfaces
Not this one https://github.com/Microsoft/TypeScript/wiki/FAQ#why-do-these-empty-classes-behave-strangely
Ryan Cavanaugh (@RyanCavanaugh) could you, please, point to exact location where it says that interfaces (which may be used for any JavaSript types except
nullandundefined) are implicitly convertible toobject(AFAIK, classes are derived from object)?And why, in this case, the empty interface is not implicitly convertable into
numberfor example?IMHO, it's a bug. And I can't see a reason to have this as a feature.
bsiegel commentedon Aug 4, 2018
I'm not sure how the rule is being applied in this case - definitely something strange with the empty interface that doesn't seem to be covered in the docs:
sergey-shandar commentedon Aug 4, 2018
BTW, it could be also non-empty interface. IMHO, the problem is that TS considers interfaces are derived from
object. Which is not true. For example,stringis not anobjectbut it may implement some interfaces.More fun
RyanCavanaugh commentedon Aug 4, 2018
You'll have to read the document and understand it holistically. No documentation can cover the entire matrix of all things you might do and comprehensively specify every outcome; there are implicit isomorphisms (
interface X { }and{ }are very similar for example).objectis assignable from{ }for mostly historical reasons.You're asking why type relationships are directional?
sergey-shandar commentedon Aug 4, 2018
Ryan Cavanaugh (@RyanCavanaugh) I understand the historical context but it doesn't mean that the problem shouldn't be fixed.
For example, we may have a strict mode when interfaces are not convertible to
object. Or we may have special interfaces which aren't convertible toobject(for backward compatibility). Something likeRyanCavanaugh commentedon Aug 4, 2018
Please log a suggestion if you have a proposal for what should be done and how/why (i.e. motivating use cases where this would provide value above the status quo). Thanks!