• HOME
  • 型定義について - TypeScript
TomoyaOkada

型定義について - TypeScript

記事をシェアする

TypeScriptの学習開始にあたって型定義について記録します。

プリミティブ型

const t: undefined = undefined;
const u: null= null;
const v: bigint= 100n;
const w: symbol = Symbol();
const x: number = 100;
const y: string = y;
const z: boolean = true;



オブジェクト型

■ interface

interface object {
  fuga: string;
  bar: number;
}


■ type

type object = {
  fuga: string;
  bar: number;
}



querySelectorメソッドへの型引数

const fuga = document.querySelector<HTMLElement>('.fuga');



関数式

const fuga = function (hoge: string): string {
  return;
};



戻り値がない関数の戻り値を型注釈

function logTxt(comment: string): void {
  console.log(comment);
}



記事をシェアする