Skip to content

Instantly share code, notes, and snippets.

@takashimamorino
Last active May 1, 2022 03:22
Show Gist options
  • Save takashimamorino/ac437222b0fa419657ea57314f486f03 to your computer and use it in GitHub Desktop.
Save takashimamorino/ac437222b0fa419657ea57314f486f03 to your computer and use it in GitHub Desktop.
TypeScript の Tips

typeof で値から型を作る

const commands = ['foo', 'bar', 'hoge'] as const;
type Command = typeof commands[number];

keof typeof 変数

const countryCode = {
  jp: 'japan',
  us: 'us',
};
type Code = keyof typeof countryCode;
const c: Code = 'jp';
// error
const c: Code = 'ch';

hoisting

関数宣言は hoisting できるが、関数式は hoisting できない

関数宣言 → プログラムの実行開始時から既に存在している

関数式  → ただの変数宣言なのでプログラムは上から下に実行される

foo();
function foo() {
  return 'foo';
}

// error
bar();
const bar = () => {
  return 'bar';
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment