본문 바로가기

전체 글

(41)
타입스크립트 Typescript 제네릭 Typescript // Generic function getSize(arr: T[]): number { return arr.length; } const arr1 = [1, 2, 3]; getSize(arr1); const arr2 = ["1", "2", "3"]; getSize(arr2); interface Mobile { name: string; price: number; option: T; } const m1: Mobile = { name: "s21", price: 1000, option: { color: "red", coupon: false, } } const m2: Mobile = { name: "s20", price: 900, option: "good" } interface User { nam..
타입스크립트 Typescript 클래스 Typescript // 접근 제한자(Access modifier) - public, private, protected /* public - 자식 클래스, 클래스 인스턴스 모두 접근 가능 protected - 자식 클래스에서 접근 가능 private - 해당 클래스 내부에서만 접근 가능 readonly - 읽기만 가능 수정 불가 수정하려면 constructor를 통해 변경해야함 static - 접근하려면 class. 을 사용해야함 */ class Car { color: string; // 접근 제한자를 선언하지 않으면 기본 public //private color: string // 클래스 내부에서만 사용가능 같은 표현 #color: string static wheels: number = 4; const..
타입스크립트 Typescript 함수 Typescript // 함수 function hello(name:string, age?:number): string { if (age !== undefined) { return `Hello, ${name}. You are ${age}.`; } else { return `Hello, ${name}.`; } } console.log(hello("Same", 30)); console.log(hello("Same")); //let test:number = hello("Same", 30); 에러발생 타입 불일치 let test2:string = hello("Same", 30); // 오버로드 interface User { name: string; age: number; } function join(name: s..