์„ค์น˜ํ•˜๊ธฐ

๐Ÿ“Œ ๋…ธ๋“œ ๋ฒ„์ „ ์—…๋ฐ์ดํŠธ ํ›„

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๋ฌธ ์ด์ƒํ•˜๋ฉด ์—๋Ÿฌ๋‚ด๊ธฐ 
     }
    }

void?

ํ•จ์ˆ˜๊ฐ€ ๋ฌด์–ธ๊ฐˆ returnํ•˜๋Š”๊ฑธ ์›ํ•˜์ง€ ์•Š์„๋•Œ void๋ฅผ ์‚ฌ์šฉํ•˜๊ณ 

void๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ return์„ ํ–ˆ๋‹ค๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

  • ex
    function void(x:number) : void{
    	x + 1
    }

์˜ต์…˜

?๋Š” undefined๋ž‘ ๋™์ผ

๋“ค์–ด์˜ฌ์ˆ˜๋„ ์žˆ๊ณ  ์•ˆ ๋“ค์–ด์˜ฌ์ˆ˜๋„ ์žˆ๋‹ค.

(x :number | undefined) === (x? :number)

ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์˜ต์…˜์ผ ๊ฒฝ์šฐ

  • ex
    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;
// ์œ„ ๋‘์ค„์„ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ(์˜ค๋ฒ„๋กœ๋“œ) ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์ง€ ์•Š๋Š”๋‹ค.

์ ‘๊ทผ ์ œํ•œ์ž - public, private, protected

  • public: ์ž์‹ ํด๋ž˜์Šค, ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค ๋ชจ๋‘ ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • protected: ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • privite: ํ•ด๋‹น ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ ( โ€˜#โ€™์œผ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ)

ํด๋ ˆ์Šค์—์„œ 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'

Partial -> ์ธํ„ฐํŽ˜์ด์Šค ์•ˆ์—์„œ ๋ชจ๋“  ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„๋•Œ

interface User {
	id: number;
	name: string;
	age: number;
	gender: 'm' | 'f'
}

let admin: Partial<User> = {
	id: 1,
	name: 'Bob'
}

Required -> ์ธํ„ฐํŽ˜์ด์Šค ์•ˆ์˜ ๋ชจ๋“  ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์•ผ ํ• ๋•Œ

interface User {
	id: number;
	name: string;
	age?: number;
}

let admin: Required<User> = {
	id: 1,
	name: 'Bob'
}

Readonly โ†’ ํ• ๋‹น๋งŒ๊ฐ€๋Šฅ ์ˆ˜์ •์€ ๋ถˆ๊ฐ€๋Šฅ

interface User {
	id: number;
	name: string;
	age?: number;
}

let admin: Readonly<User> = {
	id: 1,
	name: 'Bob'
}

Record<K, T>

Untitled

Untitled

Pick<T, K> โ†’ pick ์— ์ž‘์„ฑ๋œ ๊ฒƒ๋“ค๋งŒ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Untitled

Omit<T,K> โ†’ ํŠน์ • ํ”„๋กœํผํ‹ฐ๋ฅผ ์ƒ๋žตํ•˜์—ฌ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

Untitled

Exclude<T1, T2> โ†’ ํƒ€์ž…1์—์„œ ํƒ€์ž…2๋ฅผ ์ œ๊ฑฐํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํƒ€์ž…์œผ๋กœ์ œ๊ฑฐ

type T1 = string | number | boolean
type T2 = Exclude<T1,number | string> // boolean
type T2 = Exclude<T1,number> // string, boolean

NonNullable โ†’ null์„ ์ œ์™ธํ•œ ํƒ€์ž…์„ ์ƒ์„ฑํ•œ๋‹ค(undefinedํฌํ•จ)

type T1 = string | null | undefined | void;
type T2 = NonNullable<T1> // string | void

0๊ฐœ์˜ ๋Œ“๊ธ€

Powered by GraphCDN, the GraphQL CDN