'male' | 'female';
근데 상속받으면 못씀
그때는 다시 리터럴 써줘야함
리터럴 타입은 특정 값을 나타내는 타입으로 해당 값이 정확하게 일치해야한다. 타입스크립트에서 사용되는 리터럴 타입에는 다양한 종류가 있다.
let status : 'success' | 'error';
status = 'success'; // 유효
status = 'pending'; // 에러
let speed: 50 | 100 | 200 ;
speed = 100; // 유효
speed = 150; // 에러
let isTrue: true;
isTrue = true; // 유효
isTrue = false; //에러
let person: {name:'John', age: 30};
person = { name: 'John', age: 30}; // 유효
person = { name: 'Alice', age: 25}; // 에러
type cardinalDirection = 'North'|'East'|'South'|'West';
let directio: cardinalDirection;
direction = 'North'; // 유효
direction = 'Northeast'; // 에러
let anyValue: number | string;|기호를 사이에 두고 동시에 타입을 지정할 수 있다.타입스크립트 기반의 oop는
구조체, 공용체, 열거형, 인터페이스, 클래스등이 있다.
연관된 변수와 함수들을 한 덩어리로 묶음
클래스와 객체에서
클래스는 객체의 뼈대, 그래서 클래스는 셀계도, 생상틀로 생각하면 좋다.
그러면 객체는 이 클래스의 실체가 되는 것이다.
클래스가 붕어빵 틀이라면 객체가 붕어빵인것이다.
let employee1 = new Employee('kim',20,'개발자');
class Employee {
constructor(
private _empName:string,
private _age:number,
private _empJob:string
){
}
...
class Employee {
constructor(
private _empName:string,
private _age:number,
private _empJob:string
){
}
// get/set
get empName(){
return this._empName;
}
set empName(val : string){
this._empName = val;
}
printEmp = (): void => {
console.log(`${this._empName}의 나이는 ${this._age}이고 직업은 ${this._empJob}입니다.`);
}
}
let employee1 = new Employee('kim',20,'개발자');
employee1.empName = 'lee';
employee1.printEmp();