Skip to content

type invariants assertions #20188

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

Closed
zpdDG4gta8XKpMCd opened this issue Nov 21, 2017 · 2 comments
Closed

type invariants assertions #20188

zpdDG4gta8XKpMCd opened this issue Nov 21, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@zpdDG4gta8XKpMCd
Copy link

zpdDG4gta8XKpMCd commented Nov 21, 2017

closed in favor of #16605

in my code i often use, let's call them, type invariants assertions:

function mustBe<T>(_: () => T): void {}
interface A {
    isThat: true;
    text: string;
}
interface B {
    isThat: false;
    value: number;
}
type C = A | B;
declare const C: C;
mustBe<true>(() => C.isThat); // <-- type error as expected

now i wish i could do type invariant assertions without emitting any useless code, making it a zero cost abstraction:

makesure C.isThat sameas true;

where makesure is a new TS syntax/construct that participates in type checking, but doesn't get emitted

few more examples

makesure typeof c.isThat subtypeof boolean
makesure MyClass subtypeof BaseClass
// ...
@gcnew
Copy link
Contributor

gcnew commented Nov 21, 2017

I use type aliases for pseudo static asserts.

type HasType<T, Q extends T> = Q;

type StaticAssert1 = HasType<true, A['isThat']> // OK
type StaticAssert2 = HasType<true, C['isThat']> // Error as expected

@mhegazy
Copy link
Contributor

mhegazy commented Nov 21, 2017

duplicate of #16605?

@mhegazy mhegazy added the Duplicate An existing issue was already created label Nov 21, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants