Skip to content

Latest commit

Β 

History

History
187 lines (146 loc) Β· 3.91 KB

2020-12-19-typescript.md

File metadata and controls

187 lines (146 loc) Β· 3.91 KB

TypeScript κ°œλ… 및 κΈ°λ³Έ μš©λ²•

let v1 = 123;
v1 = 'abc';

TypeScriptμ—μ„œ μœ„μ™€ 같이 μ„ μ–Έ μ‹œ 두 번째 v1은 νƒ€μž… μ—λŸ¬κ°€ λ°œμƒν•©λ‹ˆλ‹€.
μ΄λŠ” 첫 번째 ν• λ‹Ή μ‹œ λ¬Έμžμ—΄μ΄ μ•„λ‹Œ μˆ«μžκ°€ λ³€μˆ˜μ— λ‹΄κ²ΌκΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.
이λ₯Ό νƒ€μž… μΆ”λ‘ (type inference)이라고 ν•©λ‹ˆλ‹€.
두 가지 λͺ¨λ‘ λ³€μˆ˜μ— λ‹΄κΈ° μœ„ν•΄μ„œλŠ” 두 가지 νƒ€μž…μ„ λͺ…μ‹œν•΄ μ£Όμ–΄μ•Ό ν•©λ‹ˆλ‹€.

let v1: number | string = 123;
v1 = 'abc';

any νƒ€μž…μ€ λͺ¨λ“  μ’…λ₯˜μ˜ 값을 ν—ˆμš©ν•©λ‹ˆλ‹€.

const values: number[] = [1, 2, 3];
const values: Array<number> = [1, 2, 3];

const data: [string, number] = [msg, size];

let value: any;
value = 123;
value = '123';
value = () => {};

μ—΄κ±°ν˜• νƒ€μž…

enum Fruit {
	Apple,
	Banana,
	Orange,
}

const v1: Fruit = Fruit.Apple;
const v2: Fruit.Apple | Fruit.Banana = Fruit.Banana;

μ—΄κ±°ν˜• νƒ€μž…μ€ 객체이고, 각 μ›μ†ŒλŠ” κ°’μœΌλ‘œ μ‚¬μš©λ  수 있고, νƒ€μž…μœΌλ‘œ μ‚¬μš©λ  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.
μƒμˆ˜ μ—΄κ²¨ν˜• νƒ€μž…μ„ μ‚¬μš©ν•  경우 컴파일 후에도 남아 μžˆμ§€ μ•Šκ²Œ λ©λ‹ˆλ‹€.


ν•¨μˆ˜ νƒ€μž…

function getInfoText(name: string, age: number): string {

}

ν•¨μˆ˜ νƒ€μž…μ„ μ •μ˜ν•˜κΈ° μœ„ν•΄μ„œλŠ” λ§€κ°œλ³€μˆ˜ νƒ€μž…κ³Ό λ°˜ν™˜ νƒ€μž…μ΄ ν•„μš”ν•©λ‹ˆλ‹€.

μΈν„°νŽ˜μ΄μŠ€

μžλ°”μ—μ„œμ˜ μΈν„°νŽ˜μ΄μŠ€λŠ” 클래슀λ₯Ό κ΅¬ν˜„ν•˜κΈ° 전에 ν•„μš”ν•œ λ©”μ„œλ“œλ₯Ό μ •μ˜ν•˜λŠ” μš©λ„λ‘œ μ‚¬μš©λ©λ‹ˆλ‹€.
TypeScriptμ—μ„œλŠ” μ’€ 더 λ‹€μ–‘ν•œ κΈ°λŠ₯을 가지고 μžˆμŠ΅λ‹ˆλ‹€.
μ•„λž˜λŠ” μΈν„°νŽ˜μ΄μŠ€λ‘œ 객체 νƒ€μž…μ„ μ •μ˜ν•˜λŠ” 것을 μ•Œμ•„λ³΄λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€.


interface Person {
	name: string;
	age: number;
}

Person μΈν„°νŽ˜μ΄μŠ€μ—μ„œ ν•˜λ‚˜ μ΄μƒμ˜ 속성 νƒ€μž…μ„ λ§Œμ‘±ν•˜μ§€ λͺ»ν•˜λ©΄ νƒ€μž… μ—λŸ¬κ°€ λ°œμƒν•©λ‹ˆλ‹€.
선택 속성을 μΆ”κ°€ν•˜λ €λ©΄ ? 기호λ₯Ό μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€.
읽기 μ „μš©μ€ readonly λ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€.

interface Person {
    readonly name: string;
    age?: number;
}

const p1: Person = {
    name: 'mike',
    birthday: '222-22-22',
}

μ œλ„€λ¦­

function makeArray<T>(defaultValue: T, size: number): T[] {
    const arr: T[] = [];
    for(let i = 0; i < size; i++) {
        arr.push(defaultValue);
    }
    return arr;
}

const arr1 = makeArray<number>(1, 10);
const arr2 = makeArray<string>('abc', 10);
const arr3 = makeArray('abc', 10);

μ œλ„€λ¦­μ€ νƒ€μž… 정보가 λ™μ μœΌλ‘œ κ²°μ •λ˜λ©°, 같은 κ·œμΉ™μ„ μ—¬λŸ¬ νƒ€μž…μ— μ μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈμ˜ νƒ€μž… μ •μ˜

import React from 'react';

interface Props {
	name: string;
	age?: number;
}

const MyComponent: React.FunctionComponent<Props> = function({ name, age = 23 }) {
	return (
		<>
			<p>{name}</p>
			<p>{age.substr(0)}</p>
		</>
	);
};

ν΄λž˜μŠ€ν˜• μ»΄ν¬λ„ŒνŠΈμ˜ νƒ€μž… μ •μ˜

import React, { createRef } from 'react';

interface Props {
	containerStyle: React.CSSProperties;
	theme: string;
}

const defaultProps = {
	theme: 'dark',
}

interface State {
	name: string;
	age: number | undefined;
}

class MyComponent extends React.Component<Props, State> {
	state = {
		name: 'mike',
		age: undefined,
	};
	static defaultProps = defaultProps;
	pRef = createRef<HTMLParagraphElement>();
	onClick1 = (e: EventObject) => {
		console.log(e.currentTarget.dataset['food']);
	};
	onClick2 = (e: React.MouseEvent<HTMLButtonElement>) => {
		console.log(`${e.clientX}, ${e.clientY}`);
	};
	render() {
		const { containerStyle, theme } = this.props;
		const { name, age } = this.state;

		return (
			<div style={containerStyle}>
				<p ref={this.pRef}>{name}</p>
				<button data-foold="soup" onClick={this.onClick1}>λ²„νŠΌ1</button>
				<button onClick={this.onClick2}>λ²„νŠΌ2</button>
			</div>
		)
	}
}

Reference

https://overreacted.io/ko/how-are-function-components-different-from-classes/