Skip to content

Instantly share code, notes, and snippets.

@P4

P4/nested-sum-types.ts

Last active Dec 21, 2018
Embed
What would you like to do?
interface Inner<T> {
value: T;
}
interface Numeric {
content: Inner<number>;
}
interface Text {
content: Inner<string>;
}
type Expr = Text | Numeric;
function evaluate<T>(content: Inner<T>): T {
return content.value;
}
// compiles
function ok(expr: Expr) {
const content: Inner<string|number> = expr.content;
evaluate(content);
}
// doesn't
function err(expr: Expr) {
evaluate(expr.content);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment