Skip to content

Instantly share code, notes, and snippets.

@tkhk
Last active April 30, 2020 02:57
Show Gist options
  • Save tkhk/7a98bd28fee7a42380d685fe0ff8a0e3 to your computer and use it in GitHub Desktop.
Save tkhk/7a98bd28fee7a42380d685fe0ff8a0e3 to your computer and use it in GitHub Desktop.

TypeScript の型定義メモ

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-router/index.d.ts

export function useParams<Params extends { [K in keyof Params]?: string } = {}>(): { [p in keyof Params]: keyof Params[p] extends undefined ? string | undefined : string };

<Params extends { [K in keyof Params]?: string } = {}>

  • ジェネリック型パラメーター宣言
  • key は Params のプロパティ名、value は string
  • = {} はデフォルトパラメーター、この場合は空のオブジェクト

():

  • 関数の引数、この場合はなし

{ [p in keyof Params]: keyof Params[p] extends undefined ? string | undefined : string };

  • 関数の返り値
  • ジェネリック型パラメーター同様、key が Params のプロパティ名、value は三項演算子で定義
  • keyof Params[p] が undefined に代入可能な型なら、
    • string | undefined
  • 代入不可な型なら、
    • string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment