4 ms·
You can kind of do it: interface RobotName { value: string; type: "RobotName"; } Or if you don't want to make an extra wrapper around every object
by chpatrick 4y ago
You can kind of do it:
interface RobotName {
value: string;
type: "RobotName";
}
Or if you don't want to make an extra wrapper around every object:
interface RobotName {
_phantomType: "RobotName";
}
function makeRobotName(name: string): RobotName {
return name as any as RobotName;
}
function getRobotName(robotName: RobotName): string {
return robotName as any as string;
}
Presumably V8 is smart enough to inline the wrapper functions.
- Waterluvian 4y agoOh cool. So you basically lie about the runtime structure of the type, which works out fine during type checking at compilation.
- Waterluvian 4y agoDiscriminators are super power powerful. Love them. But they can be just so heavy for things.