Modern TypeScript

Modern TypeScript

Modern TypeScript coding techniques and tips.

Transcript

  1. 6.

    // ҎԼͷܗࣜͩͱ0΍ۭจࣈʹҾ͔͔ͬΔ value || orValue value? value: orValue // Null

    Coalescing͸ҎԼͷܗࣜͱಉ͡ޮՌ value != null? value: orValue
  2. 19.
  3. 23.

    const fooAction = (payload: {value: string}) => { return {

    type: "FOO" as const, payload } } const barAction = (payload: {value: string}) => { return { type: "BAR" as const, payload } } type ActionTypes = ReturnType<typeof fooAction> | ReturnType<typeof barAction>; // "FOO" | "BAR" const reducer = (state: State, action: ActionTypes) => { switch (action.type) { case "BAZ": // Error '"BAZ"' is not comparable to type '"FOO" | "BAR"' ... } }
  4. 26.

    const foo = (type: 'foo' | 'bar' | 'baz' |

    'qux') => { ... } foo('foo'); foo('bar');