![[Typescript] ν΄λμ€(1)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FdGqhdn%2FbtsJwKTqn3L%2FofDAKlwrWj5mJlxHKzj3nk%2Fimg.png)
πμλ‘
ES6μμ μλ‘κ² λμ λ ν΄λμ€λ κΈ°μ‘΄ νλ‘ν νμ κΈ°λ° κ°μ²΄μ§ν₯ μΈμ΄λ³΄λ€ ν΄λμ€ κΈ°λ° μΈμ΄μ μ΅μν κ°λ°μκ° λ³΄λ€ λΉ λ₯΄κ² νμ΅ν μ μλ λͺ λ£νκ³ μλ‘μ΄ λ¬Έλ²μ μ μνκ³ μλ€.
νμ§λ§ ν΄λμ€κ° μλ‘μ΄ κ°μ²΄μ§ν₯ λͺ¨λΈμ μ 곡νλ κ²μ μλλ€. μ¬μ€ ν΄λμ€λ ν¨μμ΄κ³ κΈ°μ‘΄ νλ‘ν νμ κΈ°λ° ν¨ν΄μ Syntactic sugarμΌ λΏμ΄λ€.
Typescriptκ° μ§μνλ ν΄λμ€λ ECMAScript 6μ ν΄λμ€μ μλΉν μ μ¬νμ§λ§ λͺ κ°μ§ Typescriptλ§μ κ³ μ ν νμ₯ κΈ°λ₯μ΄ μλ€.
μ΄ νμ₯κΈ°λ₯λ€μ μλμμ μ€λͺ νκ³ μλ€.
1οΈβ£ν΄λμ€ λ©μλ
TypeScriptλ λ
립ν¨μ(standalone function)μ μ΄ν΄νλ κ²κ³Ό λμΌν λ°©μμΌλ‘ λ©μλλ₯Ό μ΄ν΄νλ€.
맀κ°λ³μμ νμ
μ΄λ κΈ°λ³Έκ°μ μ§μ νμ§ μμΌλ©΄ anyνμ
μ κ°λλ€. λν λ©μλλ₯Ό νΈμΆν λλ λΉμ°ν λ©μλμ 맀κ°λ³μμ μμ λ§λ μΈμκ° νμνλ€.
class Greeter {
greet(name:string){
console.log(name);
}
}
/* OK */
new Greeter().greet("Kim");
/* Error: Expected 1 arguments, but got 0 */
new Greeter().greet();
ν΄λμ€ μμ±μ(constructor)λ μ νμ μΈ ν΄λμ€ λ©μλμ²λΌ μ·¨κΈλλ€. TypeScriptλ λ©μλ νΈμΆ μ μ¬λ°λ₯Έ νμ μ μΈμκ° μ¬λ°λ₯Έ μλ‘ μ 곡λλμ§ νμΈνκΈ° μν΄ νμ κ²μ¬λ₯Ό μννλ€.
class Greeter {
constructor(name:string){
console.log(name);
}
}
/*OK*/
new Greeter("kim");
/*Error: Expected 1 arguments, but got 0 */
new Greeter();
2οΈβ£ν΄λμ€ μμ±
TypeScriptμμ ν΄λμ€μ μμ±μ μ½κ±°λ μ°λ €λ©΄ ν΄λμ€μ λͺ
μμ μΌλ‘ μ μΈν΄μΌνλ€.
β ν΄λμ€ μμ±μ μΈν°νμ΄μ€μ λμΌν ꡬ문μ μ¬μ©ν΄μ μ μΈνλ€.
β‘ ν΄λμ€ μμ± μ΄λ¦ λ€μλ μ νμ μΌλ‘ νμ
μλν
μ΄μ
μ΄ λΆλλ€.
μ¦, ν΄λμ€ μμ±μ μ μΈν΄μΌλ§ ν΄λμ€ μΈμ€ν΄μ€μμ ν λΉλκ³ μ κ·Όνλ©° ν΄λΉ ν΄λμ€μμ±μ μ¬μ©ν μ μλ€.
class FieldTrip{
destination : string;
constructor(destination: string) {
this.destination = destination; /*OK*/
console.log(this.destination);
this.noexistence = destination;
/* Error: Property noexistence does not exist on type FieldTrip */
}
}
const trip = new FieldTrip("Paris");
console.log(trip.destination);
console.log(trip.noexistence);
/* Error: Property noexistence does not exist on type FieldTrip */
2οΈβ£- 1. ν¨μ μμ±
ν΄λμ€μ λ©€λ²λ₯Ό νΈμΆκ°λ₯νν¨μλ‘ μ μΈνλ λκ°μ§ ꡬ문
β μΌλ° λ©μλ μ μ
class WithMethod{
myMethod(): void{}
}
/* true */
console.log(new WithMethod().myMethod() === new WithMethod().myMethod())
β‘ ν¨μ μμ±(Function Property)
class WithMethod {
myProperty= () :void=> { }
}
console.log(new WithMethod().myProperty === new WithMethod().myProperty);
μ°¨μ΄μ μ λ κ°μ§κ° μλ€.
- νλ‘ν νμ : λ©μλ λ°©μμ ν΄λμ€μ νλ‘ν νμ μ μ μλκ³ , ν¨μ μμ± λ°©μμ μΈμ€ν΄μ€μ μ§μ μ μλ¨.
- this λ°μΈλ©: ν¨μ μμ±μμ νμ΄ν ν¨μλ₯Ό μ¬μ©ν κ²½μ° thisλ μλμΌλ‘ ν΄λΉ μΈμ€ν΄μ€λ₯Ό κ°λ¦¬ν€κ² λλ―λ‘ ν΄λμ€ λ΄μμ this κ΄λ ¨ λ¬Έμ λ₯Ό νΌν μ μλ€. μΌλ° λ©μλλ νΈμΆ λ°©μμ λ°λΌ thisκ° λ€λ₯Ό μ μμ΄μ μν©μ λ°λΌ this λ°μΈλ©μ λͺ μμ μΌλ‘ ν΄μΌ ν λκ° μλ€.
π£μΈμ€ν΄μ€μ νλ‘ν νμ
μΈμ€ν΄μ€λ ν΄λμ€λ₯Ό κΈ°λ°μΌλ‘ μμ±λ κ°λ³ κ°μ²΄μ΄λ€.
class Car {
brand: string;
constructor(brand: string) {
this.brand = brand;
}
}
const car1 = new Car("KIA");
const car2 = new Car("Ferrari");
/* car1κ³Ό car2λ Car ν΄λμ€μ κ°κ°μ μΈμ€ν΄μ€*/
console.log(car1.brand); /* "KIA"*/
console.log(car2.brand); /* "Ferrari"*/
car1κ³Ό car2κ° μΈμ€ν΄μ€μ΄λ€.
νλ‘ν νμ μ μλ°μ€ν¬λ¦½νΈμ μμ λ©μ»€λμ¦μ΄λ€.
βν΄λμ€μμ μ μλ λ©μλμ μμ±μ μΈμ€ν΄μ€κ° μλλΌ νλ‘ν νμ μ μ μ₯λκ³ μ μΈμ€ν΄μ€λ€μ νλ‘ν νμ λ΄μ λ©μλμ μμ±μ μ¬μ©ν μ μλ€. μ¦, νλ‘ν νμ μ λ©μλμ μμ±μ μ μ₯νλ©΄μ λ©λͺ¨λ¦¬ ν¨μ¨μ±μ λμΌ μ μλ κ²μ΄λ€β
class Car {
brand: string;
constructor(brand: string) {
this.brand = brand;
}
drive() {
console.log(`${this.brand} is driving.`);
}
}
const car1 = new Car("Toyota");
/* car1 μΈμ€ν΄μ€μλ brand μμ±λ§ μκ³ drive λ©μλλ μμ§λ§,
μλ°μ€ν¬λ¦½νΈλ car1μ νλ‘ν νμ
μμ drive λ©μλλ₯Ό μ°Ύμ΅λλ€.*/
car1.drive(); /* "Toyota is driving."*/
π£ this λ°μΈλ©κ³Ό νλ‘ν νμ μ μκ΄κ΄κ³
class Car {
brand: string;
constructor(brand: string) {
this.brand = brand;
}
/* νλ‘ν νμ
μ μ μ₯λλ λ©μλ*/
start() {
console.log(this.brand + " is starting.");
}
/* μΈμ€ν΄μ€μ μ μ₯λλ ν¨μ μμ±*/
stop = () => {
console.log(this.brand + " is stopping.");
}
}
const car = new Car("Toyota");
car.start(); /* "Toyota is starting."*/
car.stop(); /* "Toyota is stopping."*/
μΌλ° λ©μλλ νλ‘ν νμ μ μ μ₯λλ―λ‘ νΈμΆν λ thisκ° μ΄λ€ κ°μ²΄λ₯Ό κ°λ¦¬ν¬μ§ νΈμΆ λ°©μμ λ°λΌ λ€λ₯Ό μ μλ€.
λ°λ©΄, ν¨μ μμ±μ μΈμ€ν΄μ€ μ체μ μ§μ μ μ₯λκ³ , βνμ΄ν ν¨μλ₯Ό μ¬μ©νλ©΄ thisκ° νμ μΈμ€ν΄μ€λ₯Ό κ°λ¦¬ν¨λ€.
2οΈβ£- 2. μ΄κΈ°ν κ²μ¬
μ격ν μ΄κΈ°ν κ²μ¬
class Example{
now = 0; /*OK*/
later: number; /*OK(Constructorμμ ν λΉ)*/
mayBeUndefined: number | undefined /*OK(Undefinedνμ©)*/
unused: number;
/*Error : Property unused has no initializer and
is not definitely assigned in the constructor.*/
constructor() {
this.later = 12;
}
}
TypeScriptλ ν΄λμ€μμ undefinedνμ μΌλ‘ μ μΈλ μμ±λ€μ΄ μμ±μ(constructor)μμ ν λΉλμλμ§ νμΈνλ€.
μμ μλ¬λ μμ±μμμ unusedμμ±μ ν λΉνμ§ μμκΈ°μ νμ μ€λ₯λ₯Ό μΌμΌν¨ λͺ¨μ΅μ΄λ€.
μ격ν μ΄κΈ°ν κ²μ¬λ₯Ό ꡬμ±νλ λ°©λ²μ TypeScriptμ IDE κΈ°λ₯ μ¬μ©μμ βstrictPropertyInitialization μ»΄νμΌλ¬ μ΅μ μ μ¬μ©ν΄ ꡬμ±νλ€.
νμ€νκ² ν λΉλ μμ±
μ격ν μ΄κΈ°ν κ²μ¬λ λλΆλΆμ κ²½μ°μ μ μ©νμ§λ§ ν΄λμ€ μμ±μ λ€μμ ν΄λμ€ μμ±μ μλμ μΌλ‘ ν λΉνμ§ μλ κ²½μ°κ° μλ€.
μ΄ λ μ격ν μ΄κΈ°ν κ²μ¬λ₯Ό μ μ©νλ©΄ μλλ μμ±μλ μ΄λ¦λ€μ !λ₯Ό μΆκ°ν΄ κ²μ¬λ₯Ό λΉνμ±ννλλ‘ μ€μ ν μ μλ€.
class ActivitiesQueue{
pending! : string[]; /*OK*/
initialize(pending : string[]){
this.pending = pending;
}
next(){
return this.pending.pop();
}
}
const activities = new ActivitiesQueue();
activities.initialize(['eat','sleep','learn']);
activities.next();
2οΈβ£- 3. μ νμ μμ±
μΈν°νμ΄μ€μ λ§μ°¬κ°μ§λ‘ ν΄λμ€λ μ μΈλ μμ± μ΄λ¦ λ€μ ?λ₯Ό μΆκ°ν΄ μμ±μ μ΅μ μΌλ‘ μ μΈνλ€.
class Example{
property? : string;
}
new Example().property?.length; /*OK*/
new Example().property.length;
/*Error: Object is possibly undefined*/
2οΈβ£- 4. μ½κΈ° μ μ© μμ±
readonlyλ‘ μ μΈλ μμ±μ μ μΈλ μμΉ λλ μμ±μμμ μ΄κΉκ°λ§ ν λΉν μ μλ€. ν΄λμ€ λ΄μ λ©μλλ₯Ό ν¬ν¨ν λ€λ₯Έ λͺ¨λ μμΉμμ μμ±μ μ½μ μλ§ μκ³ μΈ μλ μλ€.
class Quote{
readonly text: string;
constructor(text: string) {
this.text = text;
}
emphasize(){
this.text += "!";
/*Error: Cannot assign to text because it is a read-only property.*/
}
}
npm ν¨ν€μ§λ‘ κ²μν μ½λλ₯Ό μ¬μ©νλ μΈλΆμΈμ readonlyλ₯Ό μ‘΄μ€νμ§ μμ μ μμΌλ μ½κΈ° μ μ© λ³΄νΈκ° μ λ§ νμνλ€λ©΄ private(#) νλλ get() ν¨μ μμ±μ μ΄μ©μ κ³ λ €νμ.
3οΈβ£ νμ μΌλ‘μμ ν΄λμ€
νμ μμ€ν μμμ ν΄λμ€λ ν΄λμ€ μ μΈμ΄ λ°νμ κ°κ³Ό νμ μ λν μ΄μ μμ μ¬μ©ν μ μλ νμ μ λͺ¨λ μμ±νλ€λ μ μμ λ νΉνλ€.
μ΄ λ§μΈ μ¦μ¨ νμ μ€ν¬λ¦½νΈμμ ν΄λμ€λ λ κ°μ§μ μν μ μνν μ μλ€λ λ»μ΄λ€.
- ν΄λμ€λ μ½λλ₯Ό μ€νν λ(λ°νμ) μ€μ κ°μ²΄λ₯Ό λ§λ€ μ μλ κ°μ΄λ€.
- ν΄λμ€κ° κ°μ²΄λ₯Ό μμ±νλ κ²λΏλ§ μλλΌ νμ μΌλ‘λ μ¬μ©λλ€.
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
const personInstance = new Person("Alice", 25); /* λ°νμ κ° */
let anotherPerson: Person; /* νμ
μ λν
μ΄μ
*/
TypeScriptλ ν΄λμ€μ λμΌν λ©€λ²λ₯Ό λͺ¨λ ν¬ν¨νλ λͺ¨λ κ°μ²΄ νμ μ ν΄λμ€μ ν λΉν μ μλ κ²μΌλ‘ κ°μ£Όνλ€.
μλνλ©΄ TSμ ꡬ쑰μ νμ΄νμ΄ μ μΈλλ λ°©μμ΄ μλλΌ κ°μ²΄μ ννλ§ κ³ λ €νκΈ° λλ¬Έμ΄λ€.
β μ½κ² νμ΄μ°λ©΄ νμ μ€ν¬λ¦½νΈλ "μ΄ κ°μ²΄κ° μ΄λ€ ν΄λμ€μμ λ§λ€μ΄μ‘λμ§"보λ€λ "κ°μ²΄μ μμ±κ³Ό νμ μ΄ λ§λμ§"λ§ νμΈνλ€.
μ¦, ν΄λμ€μ μ μΈλ λ©€λ²(μμ± λ° λ©μλ)λ€μ΄ λͺ¨λ λμΌνκ² μ‘΄μ¬νλ€λ©΄, κ·Έ κ°μ²΄λ ν΄λΉ ν΄λμ€ νμ κ³Ό νΈνλλ€κ³ κ°μ£Όνλ€.
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
let obj = { name: "Alice", age: 25 };
let person: Person = obj; /* OK */
μ¬κΈ°μ objλ Person ν΄λμ€μ μΈμ€ν΄μ€κ° μλμ§λ§ μμ±κ³Ό νμ μ΄ Personν΄λμ€μ λμΌνκΈ° λλ¬Έμ Personνμ μΌλ‘ ν λΉλ μ μλ€.
4οΈβ£ ν΄λμ€μ μΈν°νμ΄μ€
TypeScriptλν΄λμ€ μ΄λ¦ λ€μ implements ν€μλμ μΈν°νμ΄μ€ μ΄λ¦μ μΆκ°ν¨μΌλ‘μ¨ ν΄λμ€μ ν΄λΉ μΈμ€ν΄μ€κ° μΈν°νμ΄μ€λ₯Ό μ€μνλ€κ³ μ μΈν μ μλ€.
interface Animal {
name: string;
makeSound(): void;
}
class Dog implements Animal {
name: string;
constructor(name: string) {
this.name = name;
}
makeSound(): void {
console.log(`${this.name} says: Woof!`);
}
}
const myDog = new Dog("Buddy");
myDog.makeSound();
// "Buddy says: Woof!"
μ΄λ κ² νλ©΄ ν΄λμ€λ₯Ό κ° μΈν°νμ΄μ€μ ν λΉν μ μμ΄μΌ ν¨μ TypeScriptμ λνλΈλ€. λν, νμ κ²μ¬κΈ°μ μν΄ λͺ¨λ λΆμΌμΉμ λν΄μ νμ μ€λ₯κ° λ°μνλ€.
interface Learner{
name: string;
study(hours:number) : void;
}
class Student implements Learner{
name: string;
constructor(name:string){
this.name = name;
}
study(hours:number){
console.log(hours);
}
}
const kim = new Student("Kim");
kim.study(4);
class Slacker implements Learner{}
/*
Error : Class Slacker incorrectly implements interface Learner
Type Slacker is missing the following properties from type Learner: name, study
*/
βTypeScriptλ μΈν°νμ΄μ€μμ ν΄λμ€μ λ©μλ λλ μμ± νμ μ μ μΆνμ§ μλλ€.
class Slacker implements Learner{
study(hours)
/*Error : Parameter hours implicitly has an any type.*/
{}
}
μ΄λ¬ν λ°©μμΌλ‘ νμ μ λν μ΄μ μ μ§μ νμ§ μμμ± λ©μλλ₯Ό μΆκ°νλ€λ©΄ TypeScriptλ νμ μ λν μ΄μ μ μ§μ νμ§ μλ ν hours 맀κ°λ³μλ₯Ό μμμ anyλ‘ κ°μ£Όνλ€. β‘οΈ any νμ μ€λ₯κ° λ°μν¨
μΈν°νμ΄μ€λ₯Ό ꡬννλ κ²μ μμ ν μμ μ± κ²μ¬λ₯Ό μν΄μμ΄λ€.
4οΈβ£- 1. λ€μ€ μΈν°νμ΄μ€ ꡬν
β μΆ©λμμ΄ κ°λ₯ν λ
interface SoccerPlayer{
goals: number;
shoot(name:string): void;
}
interface BasketballPlayer{
dunkShot: number;
}
class Player implements BasketballPlayer,SoccerPlayer{
goals:number;
dunkShot:number;
constructor(goals:number,dunkShot:number){
this.goals = goals;
this.dunkShot = dunkShot;
}
shoot(name:string):void{
console.log(`${name} goals ${this.goals} goals in Soccer.`);
}
}
const Messi = new Player(500,0);
Messi.shoot("L.Messi");
βμΆ©λν΄μ μλ λ
interface SoccerPlayer{
goals: number;
shoot(name:string): void; /*μ΄ λΆλΆ μ£Όλͺ©*/
}
interface BasketballPlayer{
dunkShot: number;
shoot:string; /*μ΄ λΆλΆ μ£Όλͺ©*/
}
class Player implements BasketballPlayer,SoccerPlayer{
goals:number;
dunkShot:number;
constructor(goals:number,dunkShot:number){
this.goals = goals;
this.dunkShot = dunkShot;
}
shoot(name:string):void{
/*
Error:Property shoot in type Player is not assignable to the same property in base type BasketballPlayer
Type (name: string) => void is not assignable to type string
*/
console.log(`${name} goals ${this.goals} goals in Soccer.`);
}
}
const Messi = new Player(500,0);
Messi.shoot("L.Messi");
'FrontEnd > Typescript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Typescript] ν΄λμ€(2) (0) | 2025.03.07 |
---|---|
[Typescript] μΈν°νμ΄μ€(2) (0) | 2024.08.19 |
[Typescript] μΈν°νμ΄μ€(1) (0) | 2024.08.19 |
[Typescript] λ°°μ΄(2) (0) | 2024.08.11 |
[Typescript] λ°°μ΄(1) (0) | 2024.08.05 |
μλ νμΈμ? κ°λ°μμ λλ€.