Closed Hedayet closed 2 weeks ago
Type assertions are unsafe by design, as they allow to cast less specific types to more specific types. You can check out #56235 for safe type assertions proposal and its current alternatives.
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
🔎 Search Terms
interface assertion
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgJIDED2nkG8C+AUIQgDZwDOFyWOwAtgA6kT0TjUbZ6HLIKYQFMFACuCMJigAKRqIBGpYAmQg4bAFzJhUUAHMAlHiJ8KcAJ4AJCKVI4AvMmlH7APh58+AoZhYA6Oz1pAHIACxs7ZAB3KVIAE2CDAG5eZHwUohJBYWQYbC0uB2MUrKEwXOwAeXkAKwgJLVpkRzycShpsJOQAem7kMFDgagpQzFF45HkUUAA3OCU40opfCADMINbquok-MysIzGTPXuRREDiIGFAIRe9l-0DpTdr6sD81NiPPY76zi6uQDdCEA
💻 Code
🙁 Actual behavior
Structural type checking might be confusing and leaves space for unintended runtime errors.
What Went Wrong:
🙂 Expected behavior
The type assertion to cast foo as Foo should fail
Additional information about the issue
I understand TypeScript's structural typing system does not check whether the object was created by which class constructor. But in my opinion this is a dangerous gap (even if intentional)