TypeScript typeof演算子の使い方のサンプル

環境
Windows 11 Pro 64bit
TypeScript 4.4.4

構文
type Person = {
カラム名1: string;
カラム名2: number;
};
typeof person
typeof演算子を使って変数の型を取得できます。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
type Person = {
name: string;
age: number;
};
const person: Person = {
name: 'oosaki',
age: 33,
};
console.log(typeof '');
console.log(typeof person);
type Person = { name: string; age: number; }; const person: Person = { name: 'oosaki', age: 33, }; console.log(typeof ''); console.log(typeof person);
type Person = {
  name: string;
  age: number;
};

const person: Person = {
  name: 'oosaki',
  age: 33,
};

console.log(typeof ''); 
console.log(typeof person);

実行結果
string
object

IT

Posted by arkgame