Skip to content

Instantly share code, notes, and snippets.

@alloy

alloy/1-input.ts

Last active Feb 17, 2020
Embed
What would you like to do?
Why does TypeScript inline the typeof type?
const x = {
foo: "bar",
answer: 42
}
type X = typeof x
export default {
get X(): X {
return x
}
}
declare const x: {
foo: string;
answer: number;
};
declare type X = typeof x;
declare const _default: {
// Why inline the type here?
readonly X: {
foo: string;
answer: number;
};
};
export default _default;
declare const x: {
foo: string;
answer: number;
};
declare type X = typeof x;
declare const _default: {
// Just use the named type reference
readonly X: X;
};
export default _default;
@alloy

This comment has been minimized.

Copy link
Owner Author

@alloy alloy commented Feb 16, 2020

I thought I had a solution by capturing the type as an interface:

export const x = {
    foo: "bar",
    answer: 42
}

// Capturing the type as an interface will stop tsc from inlining the type.
type _X = typeof x
interface X extends _X {}

export default {
    get X(): X {
        return x
    }
}

However, that unfortunately doesn’t work with unions:

An interface can only extend an object type or intersection of object types with statically known members. ts(2312)
@alloy

This comment has been minimized.

Copy link
Owner Author

@alloy alloy commented Feb 17, 2020

Ok, the solution looks to be to “brand” the type, making it a nominal type:

type UniqueBranding = { " Thou shalt ignore this here prop or risketh a runtime mad as a bag of ferrets."?: never };
type X = typeof x & UniqueBranding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.