let userError:object;
userError = {
name: "cloer",
age: 10
};
// console.log(user.name)
// Error: Property 'name' does not exist on type 'object'.
type Score = 'A'|'B'|'C'|'D';
interface User {
name: string;
age: number;
gender?: string; //optional
readonly birthYear: number;
[grade:number]: Score
};
let user:User = {
name:'cloer',
age:10,
birthYear: 2012
};
user.gender = 'male';
// user.birthYear = 2022 // Error
user[1]='A';
user[2]='B';
// user[3]='E' // Error
console.log(user);
// Function interface
interface Add{
(num1:number,num2:number):number
}
const add: Add = (x,y)=>{
return x+y
}
// Class interface
interface Vehicle{
color:string;
speed:number;
start():void;
}
// interface extends
interface Car extends Vehicle{
wheels:number;
brand:string;
}
// class implements
class Bmw implements Car{
color;
speed;
readonly brand = 'bmw';
wheels = 4;
constructor(c:string, s:number){
this.color = c;
this.speed = s;
}
start(){
console.log('start')
}
}
const myCar = new Bmw('green',200);
console.log(myCar)
myCar.start()
'Languages > JS∕TS' 카테고리의 다른 글
[TS] 타입이론과 고급 타입 추론 (7) | 2024.11.14 |
---|---|
[TS] How to get type from property of objects in array (0) | 2022.07.02 |
[TS] 기본 자료형 (0) | 2022.02.22 |
[코어 자바스크립트] 메모리 동작과 mutability, immutability (0) | 2021.12.25 |
[JS] 호이스팅과 TDZ (0) | 2021.10.05 |