mp
09/19/2018, 4:32 PMrrader
09/20/2018, 4:30 AMbaz
and bar
fields are requireddiesieben07
09/20/2018, 7:31 AMnull
can come in from the outside world, the type in kotlin must be nullable. Validation is a separate step.rrader
09/20/2018, 7:35 AMdiesieben07
09/20/2018, 8:38 AMEmail
.rrader
09/20/2018, 8:43 AMdiesieben07
09/20/2018, 8:59 AMtype Foo<T> = {
baz: string | T
bar: number | T
}
type FooUnvalidated = Foo<null>
type FooValidated = Foo<never>
function validate(input: FooUnvalidated): FooValidated {
const {baz, bar} = input;
if (baz === null) throw Error();
if (bar === null) throw Error();
return {
baz, bar
}
}
const input: FooUnvalidated = {baz: null, bar: 123};
const validated = validate(input);
const bar: number = validated.bar;
const doesntWork: number = input.bar; // does not compile, input.bar could be null