๐พ
interface Greetable {
name: string;
age: number;
greet(phrase: string): void;
}
/*
type Greetable = {
name: string;
age: number;
greet(phrase: string): void;
}
*/
let user1: Greetable;
user1 = {
name: "Max",
age: 25,
greet(phrase: string) {
console.log(phrase + " " + this.name);
},
};
user1.greet("Hi there - I am");
interface Greetable {
name: string;
age: number;
greet(phrase: string): void;
}
class Person implements Greetable {
name: string;
age = 30;
constructor(n: string) {
this.name = n;
}
greet(phrase: string) {
console.log(phrase + " " + this.name);
}
}
let user1: Greetable;
// let user1: Person; ๋ ๊ฐ๋ฅ
user1 = new Peson("Max");
user1.greet("Hi there - I am");
interface Greetable {
readonly name: string;
greet(phrase: string): void;
}
class Person implements Greetable {
name: string;
constructor(n: string) {
this.name = n;
}
greet(phrase: string) {
console.log(phrase + " " + this.name);
}
}
let user1: Greetable;
user1 = new Peson("Max");
user1.name = 'MArk'; // error
interface Named {
readonly name: string;
}
interface Greetable extends Named{
greet(phrase: string): void;
}
class Person implements Greetable {
name: string;
constructor(n: string) {
this.name = n;
}
greet(phrase: string) {
console.log(phrase + " " + this.name);
}
}
let user1: Greetable;
user1 = new Peson("Max");
// type AddFn = (a: number, b: number) => number;
interface AddFn {
(a: number, b: number): number;
}
let addFn: AddFn;
addFn = (n1: number, n2: number) => n1 + n2;
interface Named {
readonly name: string;
outputName?: string; // ์ ํ์ ์์ฑ
greet?(phrase: string): void; // ์ ํ์ ๋ฉ์๋
}
class Person implements Named {
name?: string;
constructor(n?: string) { // ์ ํ์ ๋งค๊ฐ๋ณ์ ์ค์
// ๊ธฐ๋ณธ๊ฐ์ด ์ค์ ๋์ง ์์์ ์ ์๋์ง ์๋ ๊ฒฝ์ฐ์ ?๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ๊ธฐ๋ณธ๊ฐ์ ํ ๋นํ๋ค.
// (n: string = '') -> ๊ธฐ๋ณธ๊ฐ ํ ๋น
if(n) {
this.name = n;
}
}
}
let user1: Greetable;
user1 = new Peson(); // error๊ฐ ๋ฐ์ํ์ง ์์