본문 바로가기

개인공부/캡틴판교 TS11

11. 실습(전화번호부) //tsconfig.json { "compilerOptions": { "allowJs": true, "checkJs": true, "target": "es5", "lib": ["es2015", "dom", "dom.iterable"], "noImplicitAny": true, //false에서 true로 수정. "strict": true, //추가 "strictFunctionTypes": true, //추가 }, "include": ["./src/**/*"] } 기본셋팅 interface PhoneNumberDictionary { [phone: string]: { num: number; }; } interface Contact { name: string; address: string; phones: Ph.. 2023. 5. 7.
10. 제네릭 제네릭(Generics) - c#, java 등의 언어에서 재사용성이 높은 컴포넌트를 만들때 자주 활용되는 특징이다. // function logText(text){ // console.log(text); // return text; // } // logText(10); //숫자10 // logText('하이') // 문자열 하이 // logText(true); // 진위값 true function logText(text: T):T { console.log(text); return text; } logText('하이'); function logText(text: string){ console.log(text); return text.. 2023. 5. 7.
9. 클래스 //javascript class Person { constructor(name, age){ this.name = name; this.age = age; } } //TS class Person { private name: string; public age: number; constructor(name: string, age: number){ this.name = name; this.age = age; } } private, public, readonly : 변수의 취급범위 2023. 5. 7.
7. 유니언타입, 인터섹션타입 union type 유니언타입 : 파라미터나 변수에 하나이상의 타입을 쓰고싶을때 function logMessage(value: string | number){ console.log(value); } logMessage('hello'); logMessage(100); 타입 가드: 특정 타입으로 타입의 범위를 좁혀 나가는(필터링 하는)과정 var seho: string | number | boolean; function logMessage(value: string|number){ //console.log(value); if(typeof value === 'number'){ value.toLocaleString(); //유니언타입의 장점? } if(typeof value === 'string'){ value.. 2023. 5. 7.