Skip to content

Type assertion should have a runtime check #234

Closed
@andy-hanson

Description

@andy-hanson

Suggestion: The expression <T> x should be equivalent to n instanceof T ? <T>n : unreachable();. In debug builds there will be an error when the cast is mistaken. In optimized builds there would be no runtime cost if unreachable code is optimized away.
Similarly, x! should be equivalent to x !== null ? x : unreachable().

Verified that this is not the corrent behavior:

class C {}

export function f(n: C | null): C {
	return <C> n;
}

export function test(): void {
	f(null); // No error?
}

Note: This is not what typescript does when compiling these expressions, but it compiles to JavaScript which has well-defined semantics for any kind of argument -- when compiling to wasm I presume it's more important for the types to definitely be correct.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions