4 ms·
It would be nice to be able to use the annotations directly, we support this in flow-runtime[0] but I think it would be fairly straightforward to support in Typ
by phpnode 9y ago
It would be nice to be able to use the annotations directly, we support this in flow-runtime[0] but I think it would be fairly straightforward to support in TypeScript too,
It looks like this:
import t from 'flow-runtime';
const makeUpperCase = t.pattern(
(input: string) => input.toUpperCase(),
(input: number) => input
);
console.log(makeUpperCase("foo bar"));
console.log(makeUpperCase(123));
console.log(makeUpperCase(false)); // throws
[0] https://codemix.github.io/flow-runtime/#/docs/pattern-matching https://codemix.github.io/flow-runtime/#/docs/pattern-matchi...
- koolba 9y agoShouldn't the uppercase of a number be a string? const makeUpperCase = t.pattern( (input: string) => input.toUpperCase(), (input: number) => '' + input ); Otherwise you get odd behaviours like: makeUpperCase('1') + makeUpperCase('1') = '11' (probably what you want) makeUpperCase(1) + makeUpperCase(1) = 2 (probably not what you want)
- phpnode 9y agoYeah probably, but it's just an example
- rezistik 9y ago...wait. Why would the uppercase of '1' be '11'? It would just be '1'.
- livingparadox 9y agoI had to reread it a few times to parse it. Its result of "adding" the uppercase of 1 that should be 11, not the uppercase itself. In other words, the result of "makeUppercase" should be a string, not a number.
- rezistik 9y agoI see it now.