New Score :0
High Score :0
Run Best
NICE BUSINESS TYPE INDICATOR
3. ๊ธ์ ์ ์น๊ตฌ์๊ฒ ๋น๋ ธ๋๋ฐ ์ค๋์ด ๋์ ์ฃผ๊ธฐ๋ก ํ๋ .. ๊ทธ๋ฐ๋ฐ ์นด๋๊ฐ์ ๋ด์ผํ๋ ๋ ๋ ์ค๋์ธ๋ฐ... ์ด๊ฑธ ์ด์ฉ๋...
4. ์ฐ๋ฆฌ ํ์ฌ๋ ์ค์ํ ์์ฌ ๊ฒฐ์ ์ ํ ๋?
5. ์ด์ฌํ ์ผํ ๋๋ฅผ ์ํ ์ ๋ฌผ์ ์ฃผ๊ณ ์ถ๋ค. ์ด๋ค๊ฒ ์ข์๊น?
6. ์ํ์์ ํฌ์์ํ์ ์ถ์ฒ๋ฐ์๋ค. ์ด๋ค๊ฑธ ๊ฐ์ ํ์ง?
7. ํ์ฌ์์์ ๋๋?
8. ๊ฟ์์ ๊นจ์ด๋๋ 20๋ ์ ์ผ๋ก ๋์๊ฐ๋ค. ๋น์ ์ด ์ ์ผ ๋จผ์ ํ๋์ผ์?
9. ๋ด๊ฐ ์ธ์ฌ ๋ด๋น์๋ผ๋ฉด ์ ๊ท ์ ์ฌ์ ์ฑ์ฉ ์ ์ ์ผ ์ค์ํ๊ฒ ๋ณด๋๊ฒ์?
10. ํ์ฌ์ ์ ๋ง ์ซ์ดํ๋ ๋๋ฃ๊ฐ ์๋ค๋ฉด?
11. ๊ฐ๋ํ ์ง์ ๊ฐ์ฅ์ด ๋์๋ค.. ์๋ ์ ์์ผ ๋ ์ ๋ฌผ์?
12. ํ์ ํ์ฌ ์ถ๊ทผ ์คํ์ผ์?
13.ํ์ฌ ์ฒด์ก๋ํ ํ๋ ๋ ์ด๋ค. ์ค๋ ๋ญํ์ง?
14. ๋์ ์ ๋ฌด ์คํ์ผ์?
ํ์ ์คํฌ๋ฆฝํธ๋?
์๋ฐ ์คํฌ๋ฆฝํธ ๊ทธ ์ด์
TypeScript๋ ํธ์ง๊ธฐ์์ ๊ธด๋ฐํ ํตํฉ์ ์ง์ํ๊ธฐ ์ํด JavaScript์ ์ถ๊ฐ ๊ตฌ๋ฌธ์ ์ถ๊ฐํฉ๋๋ค. ํธ์ง๊ธฐ ์ด๊ธฐ์ ์ค๋ฅ๋ฅผ ํฌ์ฐฉํฉ๋๋ค.
์ ๋ขฐํ ์ ์๋ ๊ฒฐ๊ณผ
TypeScript ์ฝ๋๋ JavaScript๋ก ๋ณํ๋์ด JavaScript๊ฐ ์คํ๋๋ ๋ชจ๋ ๊ณณ์์ ์คํ๋ฉ๋๋ค : ๋ธ๋ผ์ฐ์ , Node.js ๋๋ Deno ๋ฐ ์ฑ์์.
๋๊ท๋ชจ์์ฑ์ ์์
TypeScript๋ JavaScript๋ฅผ ์ดํดํ๊ณ ์ ํ ์ถ๋ก ์ ์ฌ์ฉํ์ฌ ์ถ๊ฐ ์ฝ๋ ์์ด ํ๋ฅญํ ๋๊ตฌ๋ฅผ ์ ๊ณตํฉ๋๋ค.
์๋ฐ ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋จธ๋ฅผ์ํ ์ ํ ์คํฌ๋ฆฝํธ
TypeScript๋ JavaScript์ ํน์ดํ ๊ด๊ณ์ ์์ต๋๋ค. TypeScript๋ JavaScript์ ๋ชจ๋ ๊ธฐ๋ฅ๊ณผ TypeScript์ ์ ํ ์์คํ
์์ ์ถ๊ฐ ๋ ์ด์ด๋ฅผ ์ ๊ณตํฉ๋๋ค.
์๋ฅผ ๋ค์ด JavaScript๋ ๋ฌธ์์ด ๋ฐ ์ซ์์ ๊ฐ์ ์ธ์ด ๊ธฐ๋ณธ ํ์์ ์ ๊ณตํ์ง๋ง ์ด๋ฌํ ๊ธฐ๋ณธ ํ์์ ์ผ๊ด๋๊ฒ ํ ๋นํ๋์ง ํ์ธํ์ง ์์ต๋๋ค. ํ์
์คํฌ๋ฆฝํธ๊ฐ ๊ทธ๋ ์ต๋๋ค.
์ฆ, ๊ธฐ์กด ์์
์๋ฐ ์คํฌ๋ฆฝํธ ์ฝ๋๋ TypeScript ์ฝ๋์
๋๋ค. TypeScript์ ์ฃผ์ ์ด์ ์ ์ฝ๋์์ ์๊ธฐ์น ์์ ๋์์ ๊ฐ์กฐ ํ์ํ์ฌ ๋ฒ๊ทธ ๊ฐ๋ฅ์ฑ์ ๋ฎ์ถ ์ ์๋ค๋ ๊ฒ์
๋๋ค.
Types by Inference
TypeScript๋ ์๋ฐ ์คํฌ๋ฆฝํธ ์ธ์ด๋ฅผ ์๊ณ ์์ผ๋ฉฐ ๋ง์ ๊ฒฝ์ฐ์ ์ ํ์ ์์ฑํฉ๋๋ค. ์๋ฅผ ๋ค์ด ๋ณ์๋ฅผ ๋ง๋ค๊ณ ํน์ ๊ฐ์ ํ ๋นํ ๋ TypeScript๋ ๊ฐ์ ์ ํ์ผ๋ก ์ฌ์ฉํฉ๋๋ค.
let helloWorld = "Hello World";
// ^?
https://www.typescriptlang.org/play/#code/DYUwLgBAFizA9gdXgJ2AEwgXggIgBKwITJrq4DcAUAPQ0QQB6A-EA
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org
์๋ฐ ์คํฌ๋ฆฝํธ์ ์๋ ๋ฐฉ์์ ์ดํดํจ์ผ๋ก์จ TypeScript๋ ์๋ฐ ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ํ์ฉํ์ง๋ง ์ ํ์ด์๋ ์ ํ ์์คํ
์ ๊ตฌ์ถ ํ ์ ์์ต๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์ฝ๋์์ ํ์์ ๋ช
์์ ์ผ๋ก ๋ง๋ค๊ธฐ ์ํด ๋ฌธ์๋ฅผ ์ถ๊ฐํ ํ์ ์์ด ํ์ ์์คํ
์ ์ ๊ณตํฉ๋๋ค. ์ด๊ฒ์ด TypeScript๊ฐ helloWorld๊ฐ ์์ ์์์ ๋ฌธ์์ด์ด๋ผ๋ ๊ฒ์ ์๋ ๋ฐฉ๋ฒ์
๋๋ค.
Visual Studio Code์์ JavaScript๋ฅผ ์์ฑํ๊ณ ํธ์ง๊ธฐ ์๋ ์์ฑ ๊ธฐ๋ฅ์ด ์์ ์ ์์ต๋๋ค. Visual Studio Code๋ ๋ด๋ถ์ ์ผ๋ก TypeScript๋ฅผ ์ฌ์ฉํ์ฌ JavaScript๋ก ๋ ์ฝ๊ฒ ์์
ํ ์ ์๋๋ก ํฉ๋๋ค.
Defining Types
์๋ฐ์คํฌ๋ฆฝํธ์์ ๋ค์ํ ๋์์ธ ํจํด์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ผ๋ถ ๋์์ธ ํจํด์์๋ ๋์ ํ๋ก๊ทธ๋๋ฐ์ ์ฌ์ฉํ๋ ํจํด๊ณผ ๊ฐ์ด ํ์์ ์๋์ผ๋ก ์ ์ถํ๊ธฐ ์ด๋ ต์ต๋๋ค. ์ด๋ฌํ ๊ฒฝ์ฐ๋ฅผ ๋ค๋ฃจ๊ธฐ ์ํด TypeScript๋ TypeScript์ ์ ํ์ด ๋ฌด์์ธ์ง ์๋ ค์ค ์ ์๋ ์์น๋ฅผ ์ ๊ณตํ๋ JavaScript ์ธ์ด์ ํ์ฅ์ ์ง์ํฉ๋๋ค.
์๋ฅผ ๋ค์ด, name: string ๋ฐ id: number๋ฅผ ํฌํจํ๋ ์ ์ถ๋ ์ ํ์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ ์ ์์ต๋๋ค.
const user = {
name: "Hayes",
id: 0,
};
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org
์ธํฐํ์ด์ค ์ ์ธ์ ์ฌ์ฉํ์ฌ ์ด ๊ฐ์ฒด์ ๋ชจ์์ ๋ช ์์ ์ผ๋ก ์ค๋ช ํ ์ ์์ต๋๋ค.
interface User {
name: string;
id: number;
}
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org
๊ทธ๋ฐ ๋ค์ ๋ณ์ ์ ์ธ ๋ค์ ๋ค์๊ณผ ๊ฐ์ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ JavaScript ๊ฐ์ฒด๊ฐ ์ ์ธํฐํ์ด์ค์ ๋ชจ์์ ๋ฐ๋ฅด๋๋ก ์ ์ธํ ์ ์์ต๋๋ค.
const user: User = {
name: "Hayes",
id: 0,
};
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org
์ ๊ณตํ ์ธํฐํ์ด์ค์ ์ผ์นํ์ง ์๋ ๊ฐ์ฒด๋ฅผ ์ ๊ณตํ๋ฉด TypeScript์์ ๋ค์๊ณผ ๊ฐ์ด ๊ฒฝ๊ณ ํฉ๋๋ค.
interface User {
name: string;
id: number;
}
const user: User = {
username: "Hayes",
id: 0,
};
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org
JavaScript๋ ํด๋์ค์ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ์ง์ํ๊ธฐ ๋๋ฌธ์ TypeScript๋ ์ง์ํฉ๋๋ค. ํด๋์ค์ ํจ๊ป ์ธํฐํ์ด์ค ์ ์ธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
interface User {
name: string;
id: number;
}
class UserAccount {
name: string;
id: number;
constructor(name: string, id: number) {
this.name = name;
this.id = id;
}
}
const user: User = new UserAccount("Murphy", 1);
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org
์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ์ฌ ๋งค๊ฐ ๋ณ์์ ์ฃผ์์ ๋ฌ๊ณ ํจ์์ ๊ฐ์ ๋ฐํํ ์ ์์ต๋๋ค.
function getAdminUser(): User {
//...
}
function deleteUser(user: User) {
// ...
}
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org
JavaScript์๋ ์ด๋ฏธ ์ธํฐํ์ด์ค์์ ์ฌ์ฉํ ์์๋ ๋ถ์ธ, bigint, null, ์ซ์, ๋ฌธ์์ด, ๊ธฐํธ ๋ฐ ์ ์๋์ง ์์ ์์ ๊ธฐ๋ณธ ์ ํ ์งํฉ์ด ์์ต๋๋ค. TypeScript๋ any(๋ฌด์์ด๋ ํ์ฉ), ์ ์ ์์(์ด ์ ํ์ ์ฌ์ฉํ๋ ์ฌ๋์ด ์ ํ์ด ๋ฌด์์ธ์ง ์ ์ธํ๋๋ก ํจ), never (์ด ์ ํ์ด ๋ฐ์ํ ์ ์์) ๋ฐ void(์ ์๋์ง ์์ ๊ฐ์ ๋ฐํํ๊ฑฐ๋ ๋ฐํ ๊ฐ์ด ์๋ ํจ์)์ ๊ฐ์ ๋ช ๊ฐ์ง ๋ ๋ง์ ๊ฒ์ผ๋ก ์ด ๋ชฉ๋ก์ ํ์ฅํฉ๋๋ค.
๊ฑด๋ฌผ ํ์์ ๋ํ ๋ ๊ฐ์ง ๊ตฌ๋ฌธ์ด ์์์ ์ ์ ์์ต๋๋ค: ์ธํฐํ์ด์ค ๋ฐ ํ์. ์ธํฐํ์ด์ค๋ฅผ ์ ํธํด์ผํฉ๋๋ค. ํน์ ๊ธฐ๋ฅ์ด ํ์ํ ๊ฒฝ์ฐ type์ ์ฌ์ฉํฉ๋๋ค.
Composing Types
TypeScript๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ๋จํ ์ ํ์ ๊ฒฐํฉํ์ฌ ๋ณตํฉ ์ ํ์ ๋ง๋ค ์ ์์ต๋๋ค. ๊ทธ๋ ๊ฒ ํ๋ ๋ ๊ฐ์ง ์ธ๊ธฐ ์๋ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค: Unions ์ ๋ค๋ฆญ.
Unions
๊ณต์ฉ ๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด ํ์์ด ์ฌ๋ฌ ํ์ ์ค ํ๋๊ฐ ๋ ์ ์์์ ์ ์ธํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ๋ถ์ธ ํ์์ true ๋๋ false๋ก ์ค๋ช ํ ์ ์์ต๋๋ค.
type MyBool = true | false;
https://www.typescriptlang.org/play/#code/C4TwDgpgBAsiBCB7RAbKBeKwBOBXaAPlAGYCGKAzhANxA
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org
Note:
์์ MyBool ์๋ก ๋ง์ฐ์ค๋ฅผ ๊ฐ์ ธ๊ฐ๋ฉด ๋ถ์ธ๋ก ๋ถ๋ฅ๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค. ์ด๊ฒ์ด ๊ตฌ์กฐ ์ ํ ์์คํ
์ ์์ฑ์
๋๋ค. ์ด์ ๋ํ ์์ธํ ๋ด์ฉ์ ์๋๋ฅผ ์ฐธ์กฐํ์ญ์์ค.
๊ณต์ฉ ๊ตฌ์กฐ์ฒด ํ์์ ์ผ๋ฐ์ ์ธ ์ฌ์ฉ ์ฌ๋ก๋ ๊ฐ์ด ํ์ฉ๋๋ ๋ฌธ์์ด ๋๋ ์ซ์ ๋ฆฌํฐ๋ด ์งํฉ์ ์ค๋ช
ํ๋ ๊ฒ์
๋๋ค.
type WindowStates = "open" | "closed" | "minimized";
type LockStates = "locked" | "unlocked";
type PositiveOddNumbersUnderTen = 1 | 3 | 5 | 7 | 9;
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org
๊ณต์ฉ ๊ตฌ์กฐ์ฒด๋ ๋ค์ํ ์ ํ์ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ๋ ์ ๊ณตํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ๋ฐฐ์ด์ด๋ ๋ฌธ์์ด์ ์ฌ์ฉํ๋ ํจ์๊ฐ์์ ์ ์์ต๋๋ค.
function getLength(obj: string | string[]) {
return obj.length;
}
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org
๋ณ์์ ์ ํ์ ์์๋ณด๋ ค๋ฉด typeof๋ฅผ ์ฌ์ฉํ์ญ์์ค.
string : typeof s === "string"
number : typeof n === "number"
boolean : typeof b === "boolean"
undefined : typeof undefined === "undefined"
function : typeof f === "function"
array : Array.isArray(a)
์๋ฅผ ๋ค์ด, ํจ์๊ฐ ๋ฌธ์์ด ๋๋ ๋ฐฐ์ด๋ก ์ ๋ฌ๋๋์ง ์ฌ๋ถ์ ๋ฐ๋ผ ๋ค๋ฅธ ๊ฐ์ ๋ฐํํ๋๋ก ํ ์ ์์ต๋๋ค.
function wrapInArray(obj: string | string[]) {
if (typeof obj === "string") {
return [obj];
(parameter) obj: string
}
return obj;
}
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org
Generics
์ ๋ค๋ฆญ์ ํ์์ ๋ณ์๋ฅผ ์ ๊ณตํฉ๋๋ค. ์ผ๋ฐ์ ์ธ ์๋ ๋ฐฐ์ด์ ๋๋ค. ์ ๋ค๋ฆญ์ด ์๋ ๋ฐฐ์ด์ ๋ฌด์์ด๋ ํฌํจํ ์ ์์ต๋๋ค. ์ ๋ค๋ฆญ์ด ์๋ ๋ฐฐ์ด์ ๋ฐฐ์ด์ ํฌํจ๋ ๊ฐ์ ์ค๋ช ํ ์ ์์ต๋๋ค.
type StringArray = Array<string>;
type NumberArray = Array<number>;
type ObjectWithNameArray = Array<{ name: string }>;
์ ๋ค๋ฆญ์ ์ฌ์ฉํ๋ ๊ณ ์ ํ ํ์์ ์ ์ธํ ์ ์์ต๋๋ค.
interface Backpack<Type> {
add: (obj: Type) => void;
get: () => Type;
}
// This line is a shortcut to tell TypeScript there is a
// constant called `backpack`, and to not worry about where it came from.
declare const backpack: Backpack<string>;
// object is a string, because we declared it above as the variable part of Backpack.
const object = backpack.get();
// Since the backpack variable is a string, you can't pass a number to the add function.
backpack.add(23);
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org
Structural Type System
TypeScript์ ํต์ฌ ์์น ์ค ํ๋๋ ์ ํ ๊ฒ์ฌ๊ฐ ๊ฐ์ ๋ชจ์์ ์ด์ ์ ๋ง์ถ๋ ๊ฒ์
๋๋ค. ์ด๊ฒ์ ๋๋๋ก "์ค๋ฆฌ ํ์ดํ"๋๋ "๊ตฌ์กฐ์ ํ์ดํ"์ด๋ผ๊ณ ํฉ๋๋ค.
๊ตฌ์กฐ ์ ํ ์์คํ
์์ ๋ ๊ฐ์ฒด์ ๋ชจ์์ด ๊ฐ์ผ๋ฉด ๋์ผํ ์ ํ์ผ๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
interface Point {
x: number;
y: number;
}
function logPoint(p: Point) {
console.log(`${p.x}, ${p.y}`);
}
// logs "12, 26"
const point = { x: 12, y: 26 };
logPoint(point);
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org
point ๋ณ์๋ Point ํ์์ผ๋ก ์ ์ธ๋์ง ์์ต๋๋ค. ๊ทธ๋ฌ๋ TypeScript๋ ์ ํ ๊ฒ์ฌ์์ Point์ ๋ชจ์์ Point์ ๋ชจ์๊ณผ ๋น๊ตํฉ๋๋ค. ๋ชจ์์ด ๊ฐ์ผ๋ฏ๋ก ์ฝ๋๊ฐ ์ ๋ฌ๋ฉ๋๋ค.
์
ฐ์ดํ ์ผ์น์๋ ๊ฐ์ฒด ํ๋์ ํ์ ์งํฉ๋ง ์ผ์นํ๋ฉด ๋ฉ๋๋ค.
interface Point {
x: number;
y: number;
}
function logPoint(p: Point) {
console.log(`${p.x}, ${p.y}`);
}
// ---cut---
const point3 = { x: 12, y: 26, z: 89 };
logPoint(point3); // logs "12, 26"
const rect = { x: 33, y: 3, width: 30, height: 80 };
logPoint(rect); // logs "33, 3"
const color = { hex: "#187ABF" };
logPoint(color);
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org
ํด๋์ค์ ๊ฐ์ฒด๊ฐ ๋ฐ๋ฅด๋ ๋ฐฉ์์๋ ์ฐจ์ด๊ฐ ์์ต๋๋ค.
class VirtualPoint {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
const newVPoint = new VirtualPoint(13, 56);
logPoint(newVPoint); // logs "13, 56"
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org