Last active
February 6, 2020 16:26
-
-
Save paolobueno/594ad96866469ff918162e5c7fabbe3e to your computer and use it in GitHub Desktop.
TS new features showcase
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
| /node_modules | |
| /dist | |
| /*-error.log | |
| /.cache | |
| /.yarn.lock |
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
| { | |
| "singleQuote": true, | |
| "trailingComma": "all", | |
| "bracketSpacing": false, | |
| } |
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 guards | |
| class Animal {} | |
| class Dog extends Animal { | |
| woof() { | |
| console.log('wan, wan!'); | |
| } | |
| } | |
| function isDog(d: Animal): d is Dog { | |
| return !!(d as Dog).woof; | |
| } | |
| const d: Animal = new Dog(); | |
| if (isDog(d)) { | |
| d.woof(); | |
| } | |
| // ## Assertions and never return value | |
| function fail(): never { | |
| throw new Error('bah'); | |
| } | |
| // # pattern-matching keyof | |
| const prop = <K extends keyof any>(k: K) => <T extends Record<K, V>, V>(o: T) => | |
| o[k]; | |
| // # Const type qualifier | |
| const foo = {foo: 'bar', zoo: 'banana'} as const; | |
| const z = prop('foo')(foo); | |
| // # Tuples and function rest params | |
| const props = <K extends keyof any>(...ks: K[]) => <T extends Record<K, any>>( | |
| o: T, | |
| ) => ks.map(k => o[k]) as [T[K]]; | |
| const pr = props('zoo', 'foo')(foo); | |
| const call = <A extends any[], R>(fn: (...args: A) => R, ...args: A) => | |
| fn(...args); | |
| function sum(a: number, b: number) { | |
| return a + b; | |
| } | |
| const three = call(sum, 1, 2); | |
| const pipe = <A1 extends any[], A2, R>( | |
| f: (...args: A1) => A2, | |
| g: (arg: A2) => R, | |
| ) => (...args: A1) => g(f(...args)); | |
| const add = (x: number) => (y: number) => x + y; | |
| const s = pipe( | |
| (...s: string[]) => String.prototype.concat(...s), | |
| s => s.toUpperCase(), | |
| ); | |
| const assoc = <K extends keyof any, V, O>(key: K, v: V, o: O) => ({ | |
| ...o, | |
| ...({[key]: v} as {[P in K]: V}), | |
| }); | |
| const x = assoc('a', 42, {foo: 'bar'}); |
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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
| <title>TS Demo</title> | |
| </head> | |
| <body> | |
| <div id="root"></div> | |
| <script src="./index.tsx"></script> | |
| </body> | |
| </html> |
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 ReactDOM from 'react-dom'; | |
| import React from 'react'; | |
| import {App} from './live'; | |
| ReactDOM.render(<App />, document.getElementById('root')); |
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 React, {useState} from 'react'; | |
| // times(n) | |
| // usePersistedState | |
| export const App = () => { | |
| const [count, setCount] = useState(0); | |
| const inc = () => setCount(count + 1); | |
| const dec = () => setCount(count - 1); | |
| return ( | |
| <div> | |
| <p>{count}</p> | |
| <button onClick={inc}>+</button> | |
| <button onClick={dec}>-</button> | |
| </div> | |
| ); | |
| }; |
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
| { | |
| "name": "ts-showcase", | |
| "version": "1.0.0", | |
| "main": "index.js", | |
| "repository": "gist:594ad96866469ff918162e5c7fabbe3e", | |
| "license": "MIT", | |
| "scripts": { | |
| "start": "parcel index.html" | |
| }, | |
| "devDependencies": { | |
| "@types/react": "^16.9.19", | |
| "@types/react-dom": "^16.9.5", | |
| "parcel-bundler": "^1.12.4", | |
| "react": "^16.12.0", | |
| "react-dom": "^16.12.0", | |
| "typescript": "^3.7.5" | |
| } | |
| } |
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
Show hidden characters
| { | |
| "compilerOptions": { | |
| "lib": ["DOM", "ES2015"], | |
| "strict": true, | |
| "esModuleInterop": true, | |
| "jsx": "react" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment