4 ms·
Here are some examples: TS: https://www.typescriptlang.org/play/#src=type%20TA%20%3D%20%7B%0D%0A%20%20%20%20valA%3A%20string%0D%0A%7D%0D%0A%0D%0Atype%20TB%20%3D
by vanwalj 9y ago
Here are some examples:
TS: https://www.typescriptlang.org/play/#src=type%20TA%20%3D%20%7B%0D%0A%20%20%20%20valA%3A%20string%0D%0A%7D%0D%0A%0D%0Atype%20TB%20%3D%20%7B%0D%0A%20%20%20%20valB%3A%20string%0D%0A%7D%0D%0A%0D%0Atype%20TAorTB%20%3D%20TA%20%7C%20TB%0D%0A%0D%0Afunction%20isTA(x%3A%20any)%3A%20x%20is%20TA%20%7B%0D%0A%20%20%20%20return%20x.valA%0D%0A%7D%0D%0A%0D%0Afunction%20func%20(param%3A%20TAorTB)%20%7B%0D%0A%20%20%20%20if%20(isTA(param))%20%7B%0D%0A%20%20%20%20%20%20%20%20console.log('This%20is%20IA')%0D%0A%20%20%20%20%7D%20else%20%7B%0D%0A%20%20%20%20%20%20%20%20console.log('This%20is%20IB')%0D%0A%20%20%20%20%7D%0D%0A%7D https://www.typescriptlang.org/play/#src=type%20TA%20%3D%20%...
Flow: https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgCSAgmALxgDeqYtYAbgIYzEBcYAzhgE4CWAdgHNUAX3TZ8RAELkqNOkxhT2XPkNHjcBYnG4yKJMAB9p6KAFd+AYwy84-MBetgAFDkbdGAW3Y69ASjk6MF4oV3dPLwA6RWJA6mDgq3sOOBg8KPhBFwByABUAC14OEJKSHP95WhEwPBgOAgTE2mT+VPTMuGz8opLi6QqqsDExIA https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgCSAgmA...
With Typescript, you have to write custom Type Guards functions, whereas Flow is smart enough to infer type
- johnfn 9y agoIf you use discriminated union types - e.g. give them both a type key which is a unique string, then you don't need type guards as typescript is smart enough to understand that checking the type key does the type discrimination.
- deleted 9y ago[deleted]
- deleted 9y ago[deleted]