목록JS, TS/[TS | 강의] TS·OOP (12)
개발일지
TSConfig:tsconfig.json: 타입스트립트를 컴파일할 때 필요한 설정들을 관리하는 파일 tsc cmd$ tsc --init: tsconfig.json 파일 생성 $ tsc $ tsc logging.ts: tsconfig.json 파일에 정의된 옵션으로 컴파일 진행 $ tsc -w $ tsc logging.ts -w: ts코드가 수정될 때마다 자동으로 컴파일한 후 js 실행 tsconfig.js 예시outDir : 컴파일 시 js파일이 생성될 경로rootDir : 시작루트 경로ts 파일이 처음 위치한 곳부터 상위 폴더 지정된다.{ "compilerOptions": { "outDir": "./build", ..
Proto:JS는 프로토타입 기반의 언어로 모든 객체들이 상속 받기 위한 템플릿으로 prototype object 를 가진다.상속되는 속성, 메소드들은 객체가 아닌 prototype 속성에 정의 ( __proto__ )즉, JS에서 상속, 코드 재사용 등 OOP을 위해 사용하는 것! Proto 속성JS에서 모든 객체는 Object type을 상속한다.Object.prototype 으로 접근 가능/** Proto */const x = {};const y = {};console.log(x); // __proto__: Objectconsole.log(y); // __proto__: Objectconsole.log(x.toString());console.log(x.__proto__ === y.__proto_..
Utility Types:TS에서는 타입 변환을 쉽게 하기 위해 유틸리티 타입을 제공해준다. Readonly모든 프로퍼티를 읽기전용(readonly)으로 설정해준다.type ToDo = { title: string; description: string;};function display(todo: Readonly) { todo.title = 'jaja'; // error} Partial모든 프로퍼티의 타입을 optional로 지정해준다.type ToDo = { title: string; description: string; label: string; priority: 'high' | 'low';};function updateTodo(todo: ToDo, fieldsToUpdate: Part..
Index Signatures:JS(TS)에서 Object에 들어있는 다른 객체를 참조할 때 문자열로 접근할 수 있다.const obj = { name: 'ellie',};obj.name; // ellieobj['name']; // ellie type Animal = { name: string; age: number; gender: 'male' | 'female';};// 예시1.type Name = Animal['name']; // stringconst text: Name = 'hello';const text2: Name = 1; // error// 예시2.type Gender = Animal['gender']; // 'male' | 'female'// 예시3.type Keys = keyof A..
22.09.19 월Type Alias와 Interface:Type vs Interface (기술 측면)type PositionType = { x: number; y: number;};interface PositionInterface { x: number; y: number;} 1. object (type) : 둘 다 가능// object ★const obj1: PositionType = { x: 1, y: 1,};const obj2: PositionInterface = { x: 1, y: 1, z: 1,}; 2. class (implements) : 둘 다 가능 // class ★class Pos1 implements PositionType { x: number; y: number;..
22.09.16 금Exception과 Error StateException: 예상할 수 없는 에러들js에서는 Errorex) const array = new Array(100000000000000000);컴파일 단계에서 확인할 수 없는 errorError State: 예상할 수 있는 에러들 Exception:function move(direction: 'up' | 'down' | 'left') { let x: number = 0; switch (direction) { case 'up': x += 1; break; case 'down': x -= 1; break; default: // throw new Error(`unknown directi..
22.09.08 목oop-quiz stack 제네릭으로 구현해보기 interface Stack { size: number; push(value: T): void; pop(): T;}type StackNode = { readonly value: T; readonly next?: StackNode;};class StackImpl implements Stack { private _size: number = 0; private head?: StackNode; constructor(private capacity: number) {} // getter get size() { return this._size; } push(value: T) { if (this.size ==..