-
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
Implicit downcasting to object
and classes
#26199
Comments
Not this one https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-all-types-assignable-to-empty-interfaces @RyanCavanaugh could you, please, point to exact location where it says that interfaces (which may be used for any JavaSript types except And why, in this case, the empty interface is not implicitly convertable into IMHO, it's a bug. And I can't see a reason to have this as a feature. function acceptObjectOnly(_: object) {}
acceptObjectOnly(3) // error: as expected
const emptyInterface: {} = 3
acceptObjectOnly(emptyInterface) // ok: but error is expected
class MyClass {}
function acceptMyClassOnly(_: MyClass) {}
acceptMyClassOnly(3) // ok: but error is expected
function acceptNumberOnly(_: 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: const a: object = 3 // fails
const b: object = 3 as {} // works
const c: string = 3 as {} // fails |
BTW, it could be also non-empty interface. IMHO, the problem is that TS considers interfaces are derived from More fun function acceptObjectOnly(_: object) {}
acceptObjectOnly(3) // error: as expected
interface SomeInterface {
toString(): string
}
const someInterface: SomeInterface = 3
acceptObjectOnly(someInterface) // ok: but error is expected
class MyClass {}
function acceptMyClassOnly(_: MyClass) {}
acceptMyClassOnly(3) // ok: but error is expected
function acceptNumberOnly(_: 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 (
You're asking why type relationships are directional? |
@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 pure interface Empty {
} |
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! |
TypeScript Version: 3.1.0-dev.201xxxxx
Search Terms: implicit downcast
Code
Expected behavior:
Actual behavior:
Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered: