TIL 2021. 01. 06
const getExample = (): [ number, string ] => {
return [Math.random(), "example" ];
}
enum : 같은 '종류'의 요소(숫자 혹은 문자열) 여러개를 다룰때
enum Color {
red,
blue,
green = "green"
}
//{ '0': 'red', '1': 'blue', red: 0, blue: 1, green: 'green' }
// 값 할당하지 않으면 자동으로 0부터 숫자매김
// 리버스 매핑도 지원
let greenColor: Color = Color.green;
const validateAge = (age: number)=> {
if (age < 100) {
return `${age}`.
}
return false;
}
//Type Assertion
const myAge = validateAge(23) as number;
//Type Casting
const myAge = String(23)
class Person {
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
public name: string;
private age: number;
greet() {
return `Hello, I am ${this.name}. I'm ${this.age}.`;
}
}
const soo = new Person("Soo", 23);
console.log(soo.greet());
console.log(soo.name);
// soo.age;
interface Animal {
eat (something: string): void;
hobby? : string; //* Optional property
}
//Animal이란 객체가 eat메소드 가져야함을 명시
class Person implements Animal {
private readonly name: string;
public constructor(name: string) {
this.name = name;
}
public eat(something: string) {
console.log(`${this.name} eats ${something}`);
}
}
npm init -y
npm i -D typescript
// package.json에 이렇게 뜸
"devDependencies": {
"typescript": "^4.1.3"
}
npx tsc sample.ts
참고
devDependencies : 개발할 때만 필요한 패키지 (실행할 때 필요한 것 x)
npx: Package runner
tsc: typescript compiler
공식 홈페이지: https://www.typescriptlang.org/docs/handbook/basic-types.html
번역 홈페이지: https://typescript-kr.github.io/pages/basic-types.html
Nest.js를 사용할때 살짝 써보고 제대로 공부하는 것은 처음이다. 공식문서와 프론트엔드를 공부하며 React와 사용해볼 계획이다. 겨울방학이 끝나기 전에 JS와 Express로 했던 백엔드 공부에도 적용해봐야겠음.