Typescript์ ํน์ง ์ค ํ๋๋ Typed ์ธ์ด๋ผ๋ ๊ฒ์ธ๋ฐ, ๋ด๊ฐ ์ด๋ค ์ข ๋ฅ์ ๋ณ์์ ๋ฐ์ดํฐ ์ธ์ง ์ค์ ์ ํด์ค์ผํ๋ค๋ ๊ฒ์ด๋ค. ์ด ์ฅ์ ์ ์ฝ๋๋ฅผ ์ฝ์ ๋ ์ด๋ป๊ฒ ์คํ์ด ๋๋์ง ์์ธก์ด ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ด๋ค.
์ด๋ ๊ฒ ํจ์๋ฅผ ๋ถ๋ฅผ ๋ ์ด๋ค ์ธ์๋ฅผ ์ฃผ์ด์ผํ ์ง ์ ์ ์๋ค.
const greeting = (name: string, age:number, gender: string)=>{
console.log(`Hello ${name} ${age} ${gender}`);
}
greeting("narang", 23, "female");
export {};
์๋์ฒ๋ผ return ๊ฐ์ ํ์ ์ ์ ํ ์๋ ์๋ค.
const greeting = (name: string, age:number, gender: string): string=>{
return `Hello ${name} ${age} ${gender}`;
}
greeting("narang", 23, "female");
export {};
watch๋ชจ๋์์ ์ปดํ์ผ์ ํ ์ ์๋๋ก tsc-watch ํจํค์ง๋ฅผ ๋ค์ด๋ฐ๋๋ค.
yarn add tsc-watch --dev
dist์ src ํด๋๋ฅผ ๋ง๋ค์ด์ฃผ๊ณ index.ts ํ์ผ์ src ์์ผ๋ก ๋ฃ์ด์ค๋ค.
package.json
{
"dependencies": {
"tsc": "^2.0.3",
"typescript": "^4.5.2"
},
"license": "MIT",
"scripts": {
"start" : "tsc-watch --onSuccess \" node dist/index.js\" "
},
"devDependencies": {
"tsc-watch": "^4.5.0"
}
}
sconfig.json
index.ts๋ฅผ src/**/*๋ก ๋ฐ๊ฟ์ค๋ค.
์ด์ ๋ชจ๋ ๊ฒ์ src์์ ์ปดํ์ผ ํด์ค๊ฑฐ๋ผ์ ์ธ๋ฑ์ค๋ฅผ src๋ก ์ฎ๊ฒจ์ค ๊ฒ์ด๋ค.
๊ทธ ์ธ์ ๋ชจ๋ typescript๋ src๋ก ๋ค์ด๊ฐ ๊ฒ์ด๋ค.
๊ทธ๋ฆฌ๊ณ outDir๋ฅผ dist๋ก ์ ํด์ค์ ์ปดํ์ผ ๋ ๊ฒ๋ค์ dist๋ก ๋ค์ด๊ฐ๋๋ก ํด์ค๋ค.
{
"compilerOptions":{
"module": "commonjs", // ๋ค์ํ ๊ฒ์ import, export ํ ์ ์๋๋ก
"target": "ES2015", // ์ด๋ค ๋ฒ์ ์ js๋ก ์ปดํ์ผ?
"sourceMap": true,
"outDir" : "dist"
},
// ์ด๋ค ํ์ผ๋ค์ด ์ปดํ์ผ ๊ณผ์ ์ ํฌํจ๋๋์ง
"include" : ["src/**/*"],
"exclude": ["node_modules"]
}
์ฌ์ง์์ ๋ณด์ด๋ ๊ฒ์ฒ๋ผ watch๋ชจ๋์์ ์ปดํ์ผ์ ์์ํ๋ค.
src๊ฐ ๋ฐ๋๋ฉด dist๊ฐ ๋ฐ๋๋ค๋ ๊ฒ์ด๋ค.
index.js indes.js.map์ ์ง์ฐ๊ณ ๋ค์ yarn start ๋ฅผ ํ๋ฉด ์ด๋ฒ์๋ dist์
js ํ์ผ๋ค์ด ๋ค์ด๊ฐ ๊ฒ์ ํ์ธํ ์ ์์๋ค.
index.ts
const greeting = (name: string, age:number, gender: string): string=>{
return `Hello ${name} ${age} ${gender}`;
}
console.log(greeting("narang", 23, "female"));
export {};
index.ts์์ ํจ์๋ฅผ ํธ์ถํ ๋ object๋ฅผ ๋๊ฒจ์ค ์๋ ์๋ค.
๊ทธ๋ฌ๊ธฐ ์ํด์๋ interface๋ฅผ ์ ์ํด์ค๋ค.
typescript๋ ์ด person์ด Human ์ธํฐํ์ด์ค์ ๊ฐ์ ๊ตฌ์กฐ๋ฅผ ๊ฐ๊ณ ์๋์ง ์ฒดํฌํ๋ค.
interface๋ ํ์ ์คํฌ๋ฆฝํธ์์๋ง ์๋ํ๋ค.
interface Human {
name: string,
age: number;
gender: string;
}
const person = {
name: "narang",
age: 22,
gender: "male"
};
const greeting = (person: Human): string=>{
return `Hello ${person.name} ${person.age} ${person.gender}`;
}
console.log(greeting(person));
export {};
'WEB > Front-End๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React Hooks] useInput, useTabs (0) | 2021.12.13 |
---|---|
[Typescript] ๋ธ๋ก์ฒด์ธ ๋ง๋ค๊ธฐ - predictable ํจ์๋ฆฌํด, ํจ์์ธ์ (0) | 2021.12.12 |
[Typescript] ๋ธ๋ก์ฒด์ธ ๋ฏธ๋ํ๋ก์ ํธ (0) | 2021.12.10 |
[React JS] Movie App 03 - React Route (0) | 2021.12.05 |
error:03000086:digital envelope routines::initialization error' (0) | 2021.12.04 |