TypeScript ํ๋ก์ ํธ ์ค์ ํ๋ ๋ฒ
โ typechain npm run build && npm run start // ์นํฉ์์ build์ ์ค์ ๋์ด์๋ ์คํฌ๋ฆฝํธ๋ฅผ ์คํ && ์คํฌ๋ฆฝํธ ์คํ
โ typechain npm i -D ts-node // node์์ ts๋ฅผ ์ธ์ํ๊ธฐ ์ํด ์ค์น
โ typechain npm i nodemon // ๋ ธ๋๊ฐ ์คํํ๋ ํ์ผ์ด ์ํ ๋๋ ํฐ๋ฆฌ๋ฅผ ๊ฐ์ํ๊ณ ์๋ค๊ฐ ํ์ผ์ด ์์ ๋๋ฉด ์๋์ผ๋ก ๋ ธ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ฌ์์ํ๋ ํ์ฅ ๋ชจ๋ ์ค์น
โ typechain npm run dev // ์คํฌ๋ฆฝํธ ์ปดํ์ผ
package.json ์ฝ๋
{
"name": "typechain",
"version": "1.0.0",
"description": "",
"scripts": {
"build": "tsc",
"dev": "nodemon --exec ts-node src/index.ts",
"start": "node build/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"ts-node": "^10.8.2",
"typescript": "^4.7.4"
},
"dependencies": {
"nodemon": "^2.0.19"
}
}
tsconfig.json ์ฝ๋
- target์ JavaScript์ ๋ฒ์ ์ ๋งํ๋ ๊ฒ์ด๋ค. ์ค์ ์ ํ์ง ์๋๋ค๋ฉด, ES3๋ ES5๋ก ๋์ฌ ๊ฒ์ธ๋ฐ ES6๋ถํฐ ํ์ดํ ํจ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค. ๊ทธ๋ฆฌ๊ณ ๋ํ ES2022๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ฌด ์ต์ ๋ฒ์ ์ด๋ผ ๋ธ๋ผ์ฐ์ ์ ํธํ์ด ๋์ง ์์ ์ ์๋ค.
- DOM์ ๋ธ๋ผ์ฐ์ ์์ ์ฌ์ฉํ๋ API, DOM์ ์ฌ์ฉํ์ง ์์ผ๋ฉด, node.js๋ฅผ ์ด์ฉํ ๋ฐฑ์๋ ๊ฐ๋ฐ์ฒ๋ผ ๋ณด์ธ๋ค. DOM์ ์ฌ์ฉํ์ฌ TypeScript๊ฐ localStorage, Math, window ๋ฑ์ ํ์ ์ ์ดํดํ๊ณ ์ธ์งํ๋ค.
{
"include": ["src"],
"compilerOptions": {
"outDir": "build",
"target": "ES6",
"lib": ["ES6"],
// "lib": ["ES6", "dom"],
"strict": true,
"esModuleInterop": true,
"module": "CommonJS"
}
}
TypeScript ์ฝ๋
import * as crypto from "crypto";
interface BlockShape {
hash: string;
prevHash: string;
height: number;
data: string;
}
class Block implements BlockShape {
public hash: string;
constructor (
public prevHash: string,
public height: number,
public data: string
) {
this.hash = Block.calculateHash(prevHash, height, data);
}
static calculateHash(prevHash: string, height: number, data: string) {
const toHash = `${prevHash}${height}${data}`;
return crypto.createHash("sha256").update(toHash).digest("hex");
}
}
class Blockchain {
private blocks: Block[]
constructor(){
this.blocks = [];
}
private getPrevHash(){
if(this.blocks.length === 0) return "";
return this.blocks[this.blocks.length - 1].hash;
}
public addBlock(data: string) {
const newBlock = new Block(this.getPrevHash(), this.blocks.length + 1, data);
this.blocks.push(newBlock);
}
public getBlocks() {
return [...this.blocks] // ์ค๊ฐ์ ๋ฐฐ์ด ์ฝ์
์ ํจ๊ณผ X ์ถ๊ฐ๋ง ํ์ฉ
}
}
const blockchain = new Blockchain();
blockchain.addBlock("First one");
blockchain.addBlock("Second one");
blockchain.addBlock("Third one");
blockchain.addBlock("Fourth one");
blockchain.getBlocks().push(new Block("xxxxx", 12312312, "HACKED!@!@!@!@!@!!!@!@"));
console.log(blockchain.getBlocks());
์คํ ๊ฒฐ๊ณผ
[nodemon] clean exit - waiting for changes before restart
[nodemon] restarting due to changes...
[nodemon] starting `ts-node src/index.ts`
[
Block {
prevHash: '',
height: 1,
data: 'First one',
hash: 'd90f2cc6ecdb63760af30f041a06e85e9a4d3376cccc09ff807e08c749e81ca9'
},
Block {
prevHash: 'd90f2cc6ecdb63760af30f041a06e85e9a4d3376cccc09ff807e08c749e81ca9',
height: 2,
data: 'Second one',
hash: '21625d153b9a2ba0996ac8a8ce85b78d5512a4c0bbf647548b0befab3e9b3cfe'
},
Block {
prevHash: '21625d153b9a2ba0996ac8a8ce85b78d5512a4c0bbf647548b0befab3e9b3cfe',
height: 3,
data: 'Third one',
hash: '33090390c5b209e1796881ef8aa55128e9beab0941cbc6f4989260d6f5bd961b'
},
Block {
prevHash: '33090390c5b209e1796881ef8aa55128e9beab0941cbc6f4989260d6f5bd961b',
height: 4,
data: 'Fourth one',
hash: '7c4492b164c517213390ea641501e545be53e12d39fb94676948f8b75cf18a08'
}
]
[nodemon] clean exit - waiting for changes before restart
'๐ค Language > ๐ฆ TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
TS. ๋คํ์ฑ Polymorphism (0) | 2022.06.30 |
---|---|
TS. Interfaces (0) | 2022.06.29 |
TS. Classes (0) | 2022.06.29 |
TS. ํจ์ Functions (0) | 2022.06.27 |
TS. readonly type ์ฝ๊ธฐ ์ ์ฉ ํ์ (0) | 2022.06.21 |