๐ ๋ ธ๋ ๋ฒ์ ์ ๋ฐ์ดํธ ํ
npm install -g typescript
npx create-react-app <your file> --template typescript
tsconfig.json ํ์ผ ์์ฑํ ๋ฐ์ ์ฝ๋ ์์ฑ & .ts ํ์ฅ์ ํ์ผ๋ช ์์ฑ ํ, ์ฝ๋ ์์ฑ์ ํ๋ฉด๋๋ค.
{
"compilerOptions": {
"target": "es6",
"module": "commonjs"
}
}
.ts ํ์ผ์ ๋ธ๋ผ์ฐ์ ์์ ์ฝ์ง ๋ชปํ๋ค. js ์ฝ๋๋ง ์ฝ์ ์ ์์! ๊ทธ๋ฌ๋ฏ๋ก tsํ์ผ์ js๋ก ๋ณํํด์ผํ๋ค.
๋ณํํ๊ธฐ : ํฐ๋ฏธ๋์ tsc -w ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅ ํ ์ข ๋ฃํ์ง ์๊ณ ์ฝ๋์์ฑ์ ํ๋ฉด js ํ์ผ์ด ์๋์ผ๋ก ์์ฑ๋๋ฉฐ ์ ์ฅํ ๋๋ง๋ค ๊ฐฑ์ ๋๋ค. (์ปดํ์ผ ๊ณผ์ )
tsconfig.jsonํ์ผ์ ts์์ js ์ปดํ์ผ์ ์ต์ ์ ์ค์ ํ๋ ํ์ผ์ด๋ค.
{
"compilerOptions": {
"target": "es5", // 'es3', 'es5', 'es2015', 'es2016', 'es2017','es2018', 'esnext' ๊ฐ๋ฅ
"module": "commonjs", //๋ฌด์จ import ๋ฌธ๋ฒ ์ธ๊ฑด์ง 'commonjs', 'amd', 'es2015', 'esnext'
"allowJs": true, // js ํ์ผ๋ค ts์์ importํด์ ์ธ ์ ์๋์ง
"checkJs": true, // ์ผ๋ฐ js ํ์ผ์์๋ ์๋ฌ์ฒดํฌ ์ฌ๋ถ
"jsx": "preserve", // tsx ํ์ผ์ jsx๋ก ์ด๋ป๊ฒ ์ปดํ์ผํ ๊ฒ์ธ์ง 'preserve', 'react-native', 'react'
"declaration": true, //์ปดํ์ผ์ .d.ts ํ์ผ๋ ์๋์ผ๋ก ํจ๊ป์์ฑ (ํ์ฌ์ฐ๋ ๋ชจ๋ ํ์
์ด ์ ์๋ ํ์ผ)
"outFile": "./", //๋ชจ๋ tsํ์ผ์ jsํ์ผ ํ๋๋ก ์ปดํ์ผํด์ค (module์ด none, amd, system์ผ ๋๋ง ๊ฐ๋ฅ)
"outDir": "./", //jsํ์ผ ์์ํ ๊ฒฝ๋ก๋ฐ๊พธ๊ธฐ
"rootDir": "./", //๋ฃจํธ๊ฒฝ๋ก ๋ฐ๊พธ๊ธฐ (js ํ์ผ ์์ํ ๊ฒฝ๋ก์ ์ํฅ์ค)
"removeComments": true, //์ปดํ์ผ์ ์ฃผ์์ ๊ฑฐ
"strict": true, //strict ๊ด๋ จ, noimplicit ์ด์ฉ๊ตฌ ๊ด๋ จ ๋ชจ๋ ์ ๋ถ ์ผ๊ธฐ
"noImplicitAny": true, //anyํ์
๊ธ์ง ์ฌ๋ถ
"strictNullChecks": true, //null, undefined ํ์
์ ์ด์ํ ์ง ํ ์ ์๋ฌ๋ด๊ธฐ
"strictFunctionTypes": true, //ํจ์ํ๋ผ๋ฏธํฐ ํ์
์ฒดํฌ ๊ฐํ๊ฒ
"strictPropertyInitialization": true, //class constructor ์์ฑ์ ํ์
์ฒดํฌ ๊ฐํ๊ฒ
"noImplicitThis": true, //this ํค์๋๊ฐ any ํ์
์ผ ๊ฒฝ์ฐ ์๋ฌ๋ด๊ธฐ
"alwaysStrict": true, //์๋ฐ์คํฌ๋ฆฝํธ "use strict" ๋ชจ๋ ์ผ๊ธฐ
"noUnusedLocals": true, //์ฐ์ง์๋ ์ง์ญ๋ณ์ ์์ผ๋ฉด ์๋ฌ๋ด๊ธฐ
"noUnusedParameters": true, //์ฐ์ง์๋ ํ๋ผ๋ฏธํฐ ์์ผ๋ฉด ์๋ฌ๋ด๊ธฐ
"noImplicitReturns": true, //ํจ์์์ return ๋นผ๋จน์ผ๋ฉด ์๋ฌ๋ด๊ธฐ
"noFallthroughCasesInSwitch": true, //switch๋ฌธ ์ด์ํ๋ฉด ์๋ฌ๋ด๊ธฐ
}
}
ํจ์๊ฐ ๋ฌด์ธ๊ฐ returnํ๋๊ฑธ ์ํ์ง ์์๋ void๋ฅผ ์ฌ์ฉํ๊ณ
void๋ฅผ ์ฌ์ฉํ๋๋ฐ return์ ํ๋ค๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
function void(x:number) : void{
x + 1
}
?๋ undefined๋ ๋์ผ
๋ค์ด์ฌ์๋ ์๊ณ ์ ๋ค์ด์ฌ์๋ ์๋ค.
(x :number | undefined) === (x? :number)
ํ๋ผ๋ฏธํฐ๊ฐ ์ต์ ์ผ ๊ฒฝ์ฐ
function optionParameter (x? :number) {
}
๊ฐ์ฒด์์๋ ์ฌ์ฉ๋จ
{age? : number}
type Score = 'A' | 'B' | 'C' | 'F'
interface User{
name : string;
age : number;
gender? : string; // ์์์๋์๊ณ ์์์๋์๊ณ
readonly birtYear : number; // ์ฝ๊ธฐ์ ์ฉ์ด๋ผ ์์ ์ด ๋ถ๊ฐ๋ฅํ๋ค.
[grade:number] : string; // grade์๋ ์๋ฏธ๊ฐ ์์. number ํ์
์ ํค๋ก string ๊ฐ์ด ๋ค์ด์จ๋ค.
[grade:number] : Score; // ํ์
์ผ๋ก ์ ํ Score์ธ์ ๊ฐ์ ๋ค์ด์ฌ ์ ์๋ค.
}
let user : User = {
name: 'dd',
age: 25,
birtYear: 2000,
1 : 'A'
}
interface Car {
color: string;
wheels: number;
start(): void;
}
class Bmw inplements Car {
color;
wheels = 4;
constructor(c:string){
this.color = c;
}
start(){
console.log('go...')
}
}
const b = new Bmw('green')
// ์ ํ์ ๋งค๊ฐ๋ณ์๋ณด๋ค ํ์์ ๋งค๊ฐ๋ณ์๊ฐ ์์ ์์ด์ผํ๋๋ฐ ์ ํ์ ๋งค๊ฐ๋ณ์๋ฅผ ์์ ๋๊ณ ์ถ๋ค๋ฉด
function hello(age: number | undefined, name: string): string {
return `${name}, ${age}`
}
console.log(hello(undefined, 'sam'))
// this๊ฐ type์ ํ๊ธฐ
interface User {
name: string;
}
const Sam: User = {name: 'Sam'}
// this์ค์ ์ดํ age๊ฐ ์ฒซ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ก gender๊ฐ ๋๋ฒ์งธ ๋งค๊ฐ๋ฉด์๋ก
function showName(this:User, age:number, gender:'m' | 'f'){
console.log(this.name, age, gender)
}
const a = showName.bind(Sam);
a(30, 'm');
// ์ค๋ฒ๋ก๋
// ๋ค์ด์ค๋ ๋งค๊ฐ๋ณ์ ๊ฐ์ ๋ฐ๋ผ์ ๊ฒฐ๊ณผ๊ฐ์ด ๋ฌ๋ผ์ ธ์ผํ ๋
interface User {
name: string;
age: number;
}
function join(name: string, age: number | string): User | string {
if(typeof age === number){
return {
name,
age,
};
} else {
return '์ด์ฉ๊ตฌ';
}
}
const sam: User = join('Sam', 30);
const jane: string = join('Jane', "30);
// ์ด๋ ๊ฒ๋ง ์์ฑํ๋ค๋ฉด ์ค๋ฅ๊ฐ๋๋ค.
function join (name: string, age: string): string;
function join (name: string, age: number): User;
// ์ ๋์ค์ ์ถ๊ฐํด์ฃผ์ด์ผ(์ค๋ฒ๋ก๋) ์ค๋ฅ๊ฐ ๋์ง ์๋๋ค.
ํด๋ ์ค์์ static์ ์์ ๋ถ์น๋ฉด ๋ถ๋ฌ์ฌ๋ this๊ฐ ์๋ ํด๋น ํด๋ ์ค ์ด๋ฆ์ ๋ถ์ณ์ผํ๋ค. (this.name X, ํด๋ ์ค์ด๋ฆ.name O)
Generic -> ๋ค์ด์ฌ ์ธ์์ ํ์ ์ด ์ ํด์ ธ์์ง ์์๋..? ์ ๋์จํ์ ํน์ ์ค๋ฒ๋ก๋๋ก ํด๊ฒฐํ ์ ์์ง๋ง ๋ง์๋..?
function getSize<T>(arr: T[]): number{
return arr.length;
}
// ์๋๊ฐ์
function getSize(arr: number | string | boolean...): number{
return arr.length;
}
interface Mobile<T> {
name: string;
price: number;
option: T;
}
const m1: Mobile<Object> = {
option: {
color: 'red';
}
}
const m1: Mobile<string> = {
option: 'good';
}
interface User {
name: sting;
age: number;
}
interface Car {
name: string;
color: string;
}
interface Book {
price: number;
}
const user: User = {name: 'a', age: 10};
const car: Car = {name: 'a', age: 10};
const book: Book = {price: 10};
function showName<T extends {name: string}>(data: T): string {
return data.name;
}
// keyof
interface User {
id: number;
name: string;
age: number;
gender: 'm' | 'f'
}
type UserKey = keyof User; // 'id' | 'name' |'age' | 'gender'
interface User {
id: number;
name: string;
age: number;
gender: 'm' | 'f'
}
let admin: Partial<User> = {
id: 1,
name: 'Bob'
}
interface User {
id: number;
name: string;
age?: number;
}
let admin: Required<User> = {
id: 1,
name: 'Bob'
}
interface User {
id: number;
name: string;
age?: number;
}
let admin: Readonly<User> = {
id: 1,
name: 'Bob'
}
type T1 = string | number | boolean
type T2 = Exclude<T1,number | string> // boolean
type T2 = Exclude<T1,number> // string, boolean
type T1 = string | null | undefined | void;
type T2 = NonNullable<T1> // string | void