인터페이스(Interface)
object 타입
type Score = "A" | "B" | "C" | "F";
interface User {
name : string;
age : number;
gender ? : string;
readonly birthYear : number;
[grade:number] : Score;
}
let user : User = {
name : "MM",
age : 25,
birthYear : 1998,
1 : "A",
2: "B",
3: "c",
}
user.age = 10;
user.gender = 'male'
user.birthYear = 1990;
함수 타입
interface Add {
(num1: number, num2: number): number;
}
const add : Add = function(x, y){
return x + y;
}
interface IsAdult {
(age:number): boolean;
}
const a: IsAdult = (age) => {
return age > 19;
}
a(33)
클래스 타입 (implements, extends)
interface Car {
color: string;
wheels: number;
start(): void;
}
interface Benz extends Car {
door: number;
stop(): void;
}
interface Toy {
name: string;
}
interface ToyCar extends Car, Toy {
price: number;
}
class Bmw implements Car {
color;
wheels = 4;
constructor(c: string){
this.color = c;
}
start(){
console.log("go...");
}
}
const b = new Bmw('green');
console.log(b)
b.start();
참고문헌