This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # get latest commit with passing tests | |
| while true; do npm test && break || git checkout HEAD~1; done | |
| # count how many commits in between | |
| echo $(($(git rev-list --count HEAD..master) - 1)) | |
| # checkout where things got broken | |
| while true; do npm test && break || git checkout HEAD~1; done; git checkout master~$(($(git rev-list --count HEAD..master) - 1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Swagger from 'swagger-client'; | |
| const url = 'https://petstore.swagger.io/v2/swagger.json'; // or where the spec is | |
| const requestInterceptor = req => { | |
| // Here you can add custom headers | |
| // ex: req.headers['Authorization'] = 'Bearer <YOUR TOKEN>'; | |
| return req; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // email.ts | |
| import { Email } from './mobile'; | |
| export type Email = string & { __phantom_1549009918142: never } | |
| export const createEmail = (input: string): Email | Error => { | |
| const pattern = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0–9]{1,3}\.[0–9]{1,3}\.[0–9]{1,3}\.[0–9]{1,3}])|(([a-zA-Z\-0–9]+\.)+[a-zA-Z]{2,}))$/ | |
| if(pattern.test(input)) return createOf<Email, string>(input) | |
| else return new Error(`invalid email format: ${input}`) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Foo = { | |
| value: string, | |
| __phantom: never | |
| } | |
| const f = () => { throw '' } | |
| const x: Foo = { value: 'foo', __phantom: f() } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type ConfirmedMobile = string & { __phantom_1548946378096: never } | |
| const createConfirmedMobile = (num: String) => createOf<ConfirmedMobile, String>(num) | |
| type CertifiedMobile = string & { __phantom_1548946391842: never } | |
| const createCertifiedMobile = (num: String) => createOf<CertifiedMobile, String>(num) | |
| const confirmed = createConfirmedMobile('2984829202') | |
| // ^^^ ConfirmedMobile | |
| const certified = createCertifiedMobile('2984829202') | |
| // ^^^ CertifiedMobile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // it's just a cast ;) | |
| const createOf = <T extends Base, Base>(original: Base) => <T>original |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type ConfirmedMobile = string & { __random_transparent_property: never } | |
| type UnconfirmedMobile = string & { __another_random_transparent_property: never } | |
| type CertifiedMobile = string & { __again_random_transparent_property: never } | |
| type Mobile = // ConfirmedMobile | UnconfirmedMobile | CertifiedMobile :) | |
| ConfirmedMobile | |
| | UnconfirmedMobile | |
| | CertifiedMobile | |
| const printString = (p: string) => console.log(`print String ${p}`) | |
| const printConfirmedMobile = (p: ConfirmedMobile) => console.log(`print ConfirmedMobile ${p}`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type ConfirmedMobile = string | |
| type UnconfirmedMobile = string | |
| type CertifiedMobile = string | |
| type Mobile = // just string :( | |
| ConfirmedMobile | |
| | UnconfirmedMobile | |
| | CertifiedMobile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const exhaustionCheck = (e: never) => console.log('this will never be executed') | |
| const printMobileSafe = (mobile: Mobile) => { | |
| if(mobile.status === 'confirmed') { | |
| console.log('Your mobile is confirmed!') | |
| } else if(mobile.status === 'unconfirmed') { | |
| console.warn('Your mobile is not confirmed!') | |
| } else { | |
| exhaustionCheck(mobile) // <-- you'll get a compilation error here! | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const certified: CertifiedMobile = { status: 'certified', value: '543234531' } | |
| printMobile(certified) // warn: Your mobile is not confirmed! | |
| printMobileAlternative(certified) // unhandled exception: illegal state! mobile.status=certified |