3 ms·
By the way, it's entirely fair game for you to create config/domain specific things and not simply (Left|Right) dichotomies. And by the way, TypeScript DOES ha
by KirinDave 8y ago
By the way, it's entirely fair game for you to create config/domain specific things and not simply (Left|Right) dichotomies.
And by the way, TypeScript DOES have a form of Sum typing like that as of 2017! You can say something like this from the manual:
type Shape = Square | Rectangle | Circle | Triangle;
function area(s: Shape) {
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.height * s.width;
case "circle": return Math.PI * s.radius ** 2;
}
// should error here - we didn't handle case "triangle"
}
(You can see more about it here: https://www.typescriptlang.org/docs/handbook/advanced-types.html https://www.typescriptlang.org/docs/handbook/advanced-types...., search for "Discriminated Unions".)
You can also make these ad-hoc ,and they're useful for harnessing underlying functions that might return null from stdlibs. E.g.,
function strToInt( x: string ): number | null
And then you're required to check for nulls and the compiler will complain if you don't check for them. Pretty useful!