[TypeScript] Typescript Overview
ํ๊ทธ: ts · typescript · IDE · CompilerOptions · Type Alias · Interface · Class
- ํ๋ก๊ทธ๋๋ฐ ์ธ์ด
- Complied Language
- Transpile
- JS โ Interpreted Language
Compiled | Interpreted |
---|---|
์ปดํ์ผ ํ์ O | ์ปดํ์ผ ํ์ X |
์ปดํ์ผ๋ฌ๊ฐ ํ์ O | ์ปดํ์ผ๋ฌ๊ฐ ํ์ X |
์ปดํ์ผํ๋ ์์ O | ์ปดํ์ผํ๋ ์์ X |
์ปดํ์ผ๋ ๊ฒฐ๊ณผ๋ฌผ ์คํ | ์ฝ๋ ์์ฒด๋ฅผ ์คํ |
์ปดํ์ผ๋ ๊ฒฐ๊ณผ๋ฌผ์ ์คํํ๋ ์์ | ์ฝ๋๋ฅผ ์คํํ๋ ์์ O = ๋ฐํ์ |
Traditional Compiled Language
- ์ปดํ์ผ ์ธ์ด
- C , C++ , Go, C#
- ํ๋ก๊ทธ๋๋จธ๊ฐ ์์ฑํ Source Code๋ฅผ ๊ธฐ๊ณ์ด๋ก ๋ณํํ๋ ๊ณผ์ ์ Compile์ด๋ผ๊ณ ํจ
- ๊ธฐ๊ณ์ด๋ก ๋ฐ๊พธ๋ ๋ฐฉ์(์ธํฐํ๋ฆฌํฐ ์ธ์ด) ๋ณด๋ค ๋น ๋ฆ
tscโinit โ tsconfig.json file ์์ฑ
"target": "es2016" => ์ปดํ์ผ ์ํฌ ES๋ฒ์
"module": "commonjs" => ๊ฒฐ๊ณผ์ ๋ชจ๋์ ๋ณํ
IDE ํ์ฉ
Compiler
- VS Code์ ์ปดํ์ผ๋ฌ ๋ด์ฅ
tslint โ Typescript code ๊ท์ฝ
- npm i typescript tslint(๋ก์ปฌ)
- tslint โint
- tslint ํ๋ฌ๊ทธ์ธ ์ค์น
Super Property
- compileOnSave - true/false(default false) โ
- extends - ํ์ผ ๊ฒฝ๋ก๋ช :string (์์์ฐ์)
- CompilerOptions
- files โ ์๋ ํน์ ์ ๋ ๊ฒฝ๋ก์ ๋ฆฌ์คํธ ๋ฐฐ์ด (์ค์ ์์ผ๋ฉด ๋ชจ๋ compile)
- globํจํด
- include โ (์ค์ ์์ผ๋ฉด ๋ชจ๋ compile)
- exclude โ (์ค์ ์์ผ๋ฉด ๋ชจ๋ compile)
CompilerOptions
@types //node_modules/@types ๋ผ๋ ๋ชจ๋ ๊ฒฝ๋ก๋ฅผ ์ฐพ์์ ์ฌ์ฉ / [] ๋น ๋ฐฐ์ด ์ค์ ์ ์์คํ
์ด์ฉ X
target : ๋น๋ ๊ฒฐ๊ณผ๋ฌผ์ ์ด๋ค ๋ฒ์ ์ผ๋ก ํ ๊ฒ์ธ์ง ์ค์ (์ง์ ์ํ ์ es3)
lib : type definition ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด๋ค ๊ฒ์ ์ฌ์ฉํ ์ง ์ค์
target : TypeScript๋ JS API(Math ์ ๊ฐ์)์ ๋ํ ๊ธฐ๋ณธ์ ์ธ ํ์
์ ์์ ๋ธ๋ผ์ฐ์ ํ๊ฒฝ
module : ์ปดํ์ผ ๋ ๋ชจ๋์ ๊ฒฐ๊ณผ๋ฌผ์ ์ด๋ค ๋ชจ๋ ์์คํ
์ผ๋ก ํ ์ง๋ฅผ ๊ฒฐ์ target์ด 'es6'๋ฉด es6๊ฐ ๋ํดํธ๊ฐ
Typescript Basic
Primitive Type : ์ค์ ๊ฐ์ ์ ์ฅํ๋ ์๋ฃํ
literal : ๊ฐ์์ฒด๊ฐ ๋ณํ์ง ์๋ ๊ฐ
Boolean : true/false
Number : ์ซ์
String : ๋ฌธ์
Template String : ํ์ ๊ฑธ์ณ ์๊ฑฐ๋ ํํ์์ ๋ฃ์ ์ ์๋ ๋ฌธ์์ด
Undefined & Null : ๊ณ ์ ๊ฐ
*Void : ํ์ ์ด ์๋ ์ํ , ์ฃผ๋ก ํจ์์ ๋ฆฌํด์ด ์์ ๋ ์ฌ์ฉ
*Any : ์ด๋ค ํ์ ์ด์ด๋ ์๊ด์๋ ํ์ , ์ต๋ํ ์ฐ์ง ์๋๊ฒ ํต์ฌ
Never : ๋ฆฌํด์ ์ฃผ๋ก ์ฌ์ฉ (์์ฃผ ์์ฐ์)
Array : ๋ฐฐ์ด ๊ฐ์ฒด
*Tuple : ๋ฐฐ์ด์ธ๋ฐ ํ์ ์ด ํ๊ฐ์ง๊ฐ ์๋ ๊ฒฝ์ฐ (๊ฐ์ฒด์)
*Enum : ๋น์ทํ ๊ฐ๋ค๋ผ๋ฆฌ ๋ฌถ์ด์ค ๋ ์์์ ๊ฐ์ ์์ฐจ์ ์ผ๋ก ํ ๋นํด์ค โ Enum ์์์ ๊ฒฐ๊ณผ๊ฐ์ string
*Symbol : ๊ณ ์ ํ๊ณ ์์ ๋ถ๊ฐ๋ฅํ ๊ฐ์ผ๋ก ๋ง๋ฌ
Type Assertion
ํ ๋ณํ๊ณผ ๋ค๋ฆ
โํ์ ์ด ์ด๊ฒ์ด๋คโ๋ผ๊ณ ์ปดํ์ผ๋ฌ์๊ฒ ์๋ ค์ฃผ๋ ๊ฒ์ ์๋ฏธํจ
- ๋ณ์ as ๊ฐ์ ํ ํ์ (์ฃผ๋ก์ฌ์ฉ)
- <๊ฐ์ ํ ํ์ =""> ๋ณ์ ๊ฐ์ ํ >
Type Alias
ํ์ ๋ณ์นญ์ ์ง์ด์ค
Interface
class implements interface
interface IPerson {
name: string;
hello(): void;
}
class Person implements IPerson {
name: string;
constructor(name: string) {
this.name = name;
}
hello(): void {
console.log(`hello ${this.name} ์
๋๋ค`);
}
public hi(): void {
console.log(`ํ์ง ${this.name} ์
๋๋ค`);
}
}
const person: IPerson = new Person("Mark");
person.hi();
interface extends interface
interface Person {
name: string;
age?: number;
}
interface Korean extends Person {
city: string;
}
// Person interface๋ฅผ ์์ ๋ฐ๊ณ Korean interface์ city์ ํ์
์ถ๊ฐ
const k: Korean = {
name: "Kdn",
city: "Seoul",
};
function interface
interface HelloPerson {
(name: string, age?: number): void;
}
let helloPerson: HelloPerson = function (name: string) {
console.log(name);
};
helloPerson("Mark");
//ํจ์์ ํ์
์ฒดํฌ๋ ํ ๋นํ ๋๊ฐ ์๋๋ผ ์ฌ์ฉํ ๋ ํฉ๋๋ค
Class
class Person {
name:string;
age:number;
constructor(name: string ){
this.name = name
}
}
const person = new Person("Mark");
console.log(person.name)
//1. ์์ฑ์ ํจ์๊ฐ ์์ผ๋ฉด ๋ํดํธ ์์ฑ์๊ฐ ๋ถ๋ฆผ
//2. ํด๋์ค์ ํ๋กํผํฐ ํน์ ๋ฉค๋ฒ ๋ณ์๊ฐ ์ ์๋์ด ์์ง๋ง
๊ฐ์ ๋์
ํ์ง ์์ผ๋ฉด undefined์
//3. ์ ๊ทผ์ ์ด์๋ public์ด ๋ํดํธ๊ฐ