StudyForYou / ouahhan-typescript-with-react

우아한 타입스크립트 with 리액트 스터디 레포 🧵
4 stars 0 forks source link

#4 [2장_2] typeof 연산자가 값에서 쓰일 때와 타입에서 쓰일 때의 차이점을 설명해주세요! #16

Closed hyeyoonS closed 3 months ago

hyeyoonS commented 3 months ago

❓문제

typeof 연산자가 값에서 쓰일 때와 타입에서 쓰일 때의 차이점을 설명해주세요!

🎯 답변 요약

🎯답변

typeof 연산자는 값에서 쓰일 때와 타입에서 쓰일 때의 역할이 다릅니다.

자바스크립트에서의 typeof

자바스크립트에서는 typeof 연산자를 사용하여 값의 데이터 타입을 문자열 형태로 반환합니다.

console.log(typeof 42);           // "number"
console.log(typeof 'hello');      // "string"
console.log(typeof true);         // "boolean"
console.log(typeof undefined);    // "undefined"
console.log(typeof null);         // "object" (자바스크립트의 버그로 알려짐)
console.log(typeof {});           // "object"
console.log(typeof []);           // "object" (배열도 객체로 취급됨)
console.log(typeof function(){}); // "function"

타입스크립트에서의 typeof

(1) 값으로 사용될 때 : 자바스크립트와 동일하게 사용합니다.

typeof 2022; // "number"
typeof "woowahan"; // "string"
typeof true; // "boolean"
typeof {}; // "object"

(2) 타입으로 사용될 때: 값을 읽고 타입스크립트의 타입을 반환합니다.

let num = 42;
type NumType = typeof num; // NumType은 'number' 타입이 됩니다

let anotherNum: NumType = 100; // 오류발생X
let wrongType: NumType = "hello"; // 오류발생O Type 'string' is not assignable to type 'number'.

(3) 둘을 비교한 예제

interface Person {
  first: string;
  last: string;
}

const person: Person = { first: "zig", last: "song" };

function email(options: { person: Person; subject: string; body: string }) {}
// 값에서 사용될 때
const v1 = typeof person; // 값은 ‘object’ 
const v2 = typeof email; // 값은 ‘function’

// 타입에서 사용될 때
type T1 = typeof person; // 타입은 Person
type T2 = typeof email; // 타입은 (options: { person: Person; subject: string; body:string; }) = > void
hyeyoonS commented 3 months ago

typeof 연산자는 값에서 쓰일 때와 타입에서 쓰일 때의 역할이 다릅니다.

자바스크립트에서의 typeof

자바스크립트에서는 typeof 연산자를 사용하여 값의 데이터 타입을 문자열 형태로 반환합니다.

console.log(typeof 42);           // "number"
console.log(typeof 'hello');      // "string"
console.log(typeof true);         // "boolean"
console.log(typeof undefined);    // "undefined"
console.log(typeof null);         // "object" (자바스크립트의 버그로 알려짐)
console.log(typeof {});           // "object"
console.log(typeof []);           // "object" (배열도 객체로 취급됨)
console.log(typeof function(){}); // "function"

타입스크립트에서의 typeof

(1) 값으로 사용될 때 : 자바스크립트와 동일하게 사용합니다.

typeof 2022; // "number"
typeof "woowahan"; // "string"
typeof true; // "boolean"
typeof {}; // "object"

(2) 타입으로 사용될 때: 값을 읽고 타입스크립트의 타입을 반환합니다.

let num = 42;
type NumType = typeof num; // NumType은 'number' 타입이 됩니다

let anotherNum: NumType = 100; // 오류발생X
let wrongType: NumType = "hello"; // 오류발생O Type 'string' is not assignable to type 'number'.

(3) 둘을 비교한 예제

interface Person {
  first: string;
  last: string;
}

const person: Person = { first: "zig", last: "song" };

function email(options: { person: Person; subject: string; body: string }) {}
// 값에서 사용될 때
const v1 = typeof person; // 값은 ‘object’ 
const v2 = typeof email; // 값은 ‘function’

// 타입에서 사용될 때
type T1 = typeof person; // 타입은 Person
type T2 = typeof email; // 타입은 (options: { person: Person; subject: string; body:string; }) = > void
qooktree1 commented 3 months ago

두 가지 모두 어떤 타입인지 보여주지만 가장 큰 차이점은

한다고 말할 수 있습니다.