functionarea(s:Shape) {if (s.kind ==="square") {// Now TypeScript *knows* that `s` must be a square ;)// So you can use its members safely :)returns.size *s.size; }else {// Wasn't a square? So TypeScript will figure out that it must be a Rectangle ;)// So you can use its members safely :)returns.width *s.height; }}
interfaceSquare { kind:"square"; size:number;}interfaceRectangle { kind:"rectangle"; width:number; height:number;}// Someone just added this new `Circle` Type// We would like to let TypeScript give an error at any place that *needs* to cater for thisinterfaceCircle { kind:"circle"; radius:number;}typeShape=Square|Rectangle|Circle;
Circleのインスタンスが渡された場合に悪いことが起きる例:
functionarea(s:Shape) {if (s.kind ==="square") {returns.size *s.size; }elseif (s.kind ==="rectangle") {returns.width *s.height; }// Would it be great if you could get TypeScript to give you an error?}
strictNullChecksを使用して網羅チェックを行っている場合、TypeScriptは"not all code paths return a value"というエラーを出すかもしれません。そのエラーを黙らせるには、シンプルに_exhaustiveCheck変数(never型)を返すだけです:
functionassertNever(x:never):never {thrownewError('Unexpected value. Should have been never.');}
以下に、area関数とともに使用する例を示します。
interfaceSquare { kind:"square"; size:number;}interfaceRectangle { kind:"rectangle"; width:number; height:number;}typeShape=Square|Rectangle;functionarea(s:Shape) {switch (s.kind) {case"square": returns.size *s.size;case"rectangle": returns.width *s.height;// If a new case is added at compile time you will get a compile error// If a new value appears at runtime you will get a runtime errordefault: returnassertNever(s); }}
typeDTO=| { version:undefined,// version 0 name:string, }| { version:1, firstName:string, lastName:string,}// Even later | { version:2, firstName:string, middleName:string, lastName:string,} // So on
import { createStore } from'redux'typeAction= { type:'INCREMENT' }| { type:'DECREMENT' }/** * This is a reducer, a pure function with (state, action) => state signature. * It describes how an action transforms the state into the next state. * * The shape of the state is up to you: it can be a primitive, an array, an object, * or even an Immutable.js data structure. The only important part is that you should * not mutate the state object, but return a new object if the state changes. * * In this example, we use a `switch` statement and strings, but you can use a helper that * follows a different convention (such as function maps) if it makes sense for your * project. */functioncounter(state =0, action:Action) {switch (action.type) {case'INCREMENT':return state +1case'DECREMENT':return state -1default:return state }}// Create a Redux store holding the state of your app.// Its API is { subscribe, dispatch, getState }.let store =createStore(counter)// You can use subscribe() to update the UI in response to state changes.// Normally you'd use a view binding library (e.g. React Redux) rather than subscribe() directly.// However, it can also be handy to persist the current state in the localStorage.store.subscribe(() =>console.log(store.getState()))// The only way to mutate the internal state is to dispatch an action.// The actions can be serialized, logged or stored and later replayed.store.dispatch({ type:'INCREMENT' })// 1store.dispatch({ type:'INCREMENT' })// 2store.dispatch({ type:'DECREMENT' })// 1