$30 off During Our Annual Pro Sale. View Details »

プロダクト開発とTypeScript

 プロダクト開発とTypeScript

TypeScriptによるプロダクト開発の知見

More Decks by Taketoshi Aono(青野健利 a.k.a brn)

Other Decks in Programming

Transcript

  1. enum Card { SPADE, HEART, DIAMOND, CLUB } // ࿈൪

    assert.equal(Card.SPADE, 0); assert.equal(Card.DIAMOND, 2); // ٯҾ͖ assert.equal(Card[Card.DIAMOND], 'DIAMOND');
  2. enum Card { SPADE, HEART, DIAMOND, CLUB } const fn

    = (card: Card) => { return card; } fn(Card.SPADE); // OK fn(0); // OK fn(10); // OK
  3. enum EventType { CLICK = 'CLICK', MOUSEOVER = 'MOUSEOVER'; }

    const fn2 = (event: EventType) => {return event;} fn2(EventType.CLICK); fn2(EventType.MOUSEOVER); fn2("MOUSEDOWN"); // NG
  4. enum EventType { CLICK = 'CLICK', MOUSEOVER = 'MOUSEOVER'; }

    interface ClickEvent { type: EventType.CLICK; } interface MouseDownEvent { type: EventType.MOUSEOVER; } declare const clickEvent: ClickEvent; declare const mouseDownEvent: MouseDownEvent; const fn3 = (event: ClickEvent) => { return event; } fn3(clickEvent); // OK fn3(mouseDownEvent); // NG
  5. type Optional<T> = T | undefined | null; declare const

    obj: Optional<{value?: string}>; // ·ͩಈ͖·ͤΜ // or obj?.value || ‘’ // force obj!.value!
  6. interface Todo { id: string; date: Date; title: string; done:

    boolean; } type Todos = Todo[]; interface State { todos: Todos; } type Payload = { type: "ADD", } | { type: "DELETE" payload: {id: string}; } const reducer = (payload: Payload, state: State): State => { return state; }
  7. class Test { private value: {[key: string]: string} | null;

    constructor() {} public setValue(value: {[key: string]: string}) { this.value = value; } public doSomething() { return this.value!['key'] // you need to through type check. } }
  8. function getValue() { let value: {[key: string]: number} | null

    = null; if (x) { value = {key: 1}; } else { value = {value: 1}; } return value; }