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
| "scripts":{ | |
| "start": "http-server ./dist -p4000", | |
| "build": "node ./bin/build.js" | |
| } |
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
| # install dependencies the first time | |
| npm install less http-server handlebars | |
| # build the website | |
| npm run build | |
| # serve files via http | |
| npm start |
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
| pipelines: | |
| default: | |
| - step: | |
| name: Build | |
| image: node:10 | |
| script: | |
| - npm install | |
| - npm run build | |
| artifacts: | |
| - dist/** |
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 Mobile { | |
| confirmed: boolean, | |
| value: string | |
| } | |
| type User { | |
| /* name, surname, etc */ | |
| mobile: Mobile | |
| } |
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 Valued<T> = { value: T } | |
| type ConfirmedMobile = Valued<string> & { status: 'confirmed' } | |
| type UnconfirmedMobile = Valued<string> & { status: 'unconfirmed' } | |
| type Mobile = ConfirmedMobile | UnconfirmedMobile | |
| type User { | |
| /* name, surname, etc */ | |
| mobile: Mobile | |
| } | |
| // an user with confirmed mobile |
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
| // optimistic implementation: assume mobile.status will only have those two values | |
| const printMobile = (mobile: Mobile) => { | |
| if(mobile.status === 'confirmed') { | |
| console.log('Your mobile is confirmed!') | |
| } else { | |
| console.warn('Your mobile is not confirmed!') | |
| } | |
| } | |
| // defensive implementation: assume mobile.status can be whatever |
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
| // add this line | |
| type CertifiedMobile = Valued<string> & { status: 'certified' } | |
| // add CertifiedMobile to the unon type | |
| type Mobile = 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 certified: CertifiedMobile = { status: 'certified', value: '543234531' } | |
| printMobile(certified) // warn: Your mobile is not confirmed! | |
| printMobileAlternative(certified) // unhandled exception: illegal state! mobile.status=certified |
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
| type ConfirmedMobile = string | |
| type UnconfirmedMobile = string | |
| type CertifiedMobile = string | |
| type Mobile = // just string :( | |
| ConfirmedMobile | |
| | UnconfirmedMobile | |
| | CertifiedMobile |