์ค์น์ ๋ณํ ํ์ค ์์ฝ
node.js ์ค์น → npm i -g typescript → tsconfig.json ์์ฑ → tsc -w
<์์ฝ>
ts๋ ๋ธ๋ผ์ฐ์ ์์๋ node์์๋ ์ฝํ์ง ์๋๋ค.
ts๋ js ์ฝ๋๋ก ๋ณํํด์ฃผ์ด์ผ ํ๋ค. index.ts๋ฅผ ๋ง๋ค์๋ค๋ฉด
tsc index.ts ๋ ๋ช
๋ น์ด๋ฅผ ์
๋ ฅ main.js๋ ํ์ผ์ด ์๋ก ์์ฑ๋๋ค.
์ด ๋ณํ๋ js๋ ์ด์ ๋ธ๋ผ์ฐ์ ๋ node์์ ์ฝ์ ์ ์๋ค.
ts๋ฅผ js๋ก ๋งค๋ฒ ์๋ ๋ณํ
tsc ํ์ผ๋ช
.ts
๋ ธ๋์์ ๊ฐํธํ๊ฒ ์คํ
ts-node ์ค์น ๋ช ๋ น์ด: ๋ด๋ถ์ ์ผ๋ก ํ์ ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ก ๋ณํํด์ ๋ ธ๋์์ ์คํํ๋ ๊ฒ์ ํ๋ฒ์ ํ ์ ์๋๋ก ๋์์ฃผ๋ ํด
npm install -g ts-node ์ค์น ํ
๊ฐ๋ น index.ts๋ฅผ ์คํํ๊ณ ์ถ๋ค๋ฉด
ts-node index.ts ๋ง ์จ์ฃผ๋ฉด ๋๋ค.
ts-node src/index.ts (ํด๋์ ์๋ ๊ฒฝ์ฐ)
๋ธ๋ผ์ฐ์ ์์ ๊ฐํธํ๊ฒ ์คํ
tsc ํ์ผ๋ช
.ts -w
Watch mode
๋งค๋ฒ ๋ณํ๊ฐ ์์ ๋๋ง๋ค tsc๋ช
๋ น์ด๋ก TypeScript ์ปดํ์ผ์ ํด์ฃผ๋ ๊ฒ ๋์ ์, watch mode๋ก ์ปดํ์ผ์ ์์ํ๋ฉด TypeScript ํ์ผ ์ ์ฅ ํ, ์ ์ฅ ์ ๊ณผ ๋ณํ๊ฐ ์์ ๋๋ง๋ค ์๋ ์ปดํ์ผ ํด์ค๋ค.
$ tsc -w
ํฐ๋ฏธ๋์ด ๊ณ์ watching… ts๋ฅผ ๋ฐ๋ผ๋ณธ๋ค๋ ์๋ฏธ์ด๋ค.
ํ์ ์คํฌ๋ฆฝํธ ํ๋ก์ ํธ ๋ง๋ค๊ธฐ : ํ์ ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐ(์ถํ์ฌ ํ๋ก๊ทธ๋๋ฐ ์ธ์ฌ์ดํธ)๋์ผํ๊ฒ ๊ตฌ์ฑ
์ฝ๋ ํธ์ง๊ธฐ ์ค์
์๋น๋จ๊ณ1. NodeJs
TSC ์์ฒด๋ ํ์
์คํฌ๋ฆฝํธ๋ก ๊ตฌํ๋ ๋ช
๋ นํ ๋๊ตฌ์ด๋ฏ๋ก TSC๋ฅผ ์คํํ๋ ค๋ฉด NodeJS๊ฐ ํ์ํ๋ค.(์ด๋ฐ ์ด์ ๋ก TSC๋ ์์ฒด ํธ์คํ
์ปดํ์ผ๋ฌ ๋๋ ์์ ์ ์ปดํ์ผํ๋ ์ปดํ์ผ๋ฌ๋ผ๋ ํน๋ณํ ์ข
๋ฅ์ ์ปดํ์ผ๋ฌ๊ฐ ๋๋ค.)
NodeJs๋ ํ๋ก์ ํธ์ ์์กด์ฑ์ด๋ ๋น๋๋ฅผ ๊ด๋ฆฌํ๋ ํจํค์ง ๊ด๋ฆฌ์ NPM์ ํฌํจํ๋ค. NPM์ ์ด์ฉํด TSC์ TSLint๋ฅผ ์ค์นํ ๊ฒ์ด๋ค.
1.
ํฐ๋ฏธ๋ ์ฐฝ์ ์ด์ด ์ ๋๋ ํฐ๋ฆฌ๋ฅผ ๋ง๋ค๊ณ ๊ทธ ์์ ์ npm ํ๋ก์ ํธ๋ฅผ ์ด๊ธฐํํ๋ค.
mkdir chapter-2
cd chapter-2
npm init -y : ์ NPM ํ๋ก์ ํธ ์ด๊ธฐํ
npm install --save-dev typescript tslint @types/node : TSC,TSLint, NodeJS์ฉ ํ์
์ ์ธ ์ค์น
$ npm install --save-dev typescript tslint @types/node
npm WARN deprecated tslint@6.1.3: TSLint has been deprecated in favor of ESLint. Please see https://github.com/palantir/tslint/issues/4534 for more information.
added 43 packages, and audited 44 packages in 8s
4 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
2.
๋ชจ๋ ํ์
์คํฌ๋ฆฝํธ ํ๋ก์ ํธ๋ ๋ฃจํธ ๋๋ ํฐ๋ฆฌ์ tsconfig.json ์ด๋ผ๋ ํ์ผ์ด ์กด์ฌํด์ผ ํ๋ค.
ํ์
์คํฌ๋ฆฝํธ ํ๋ก์ ํธ์์ ์ด๋ค ํ์ผ์ ์ปดํ์ผํ๊ณ , ์ด๋ค ์๋ฐ์คํฌ๋ฆฝํธ ๋ฒ์ ์ผ๋ก ๋ฐฉ์ถํ๋์ง ๋ฑ์ ์ ์ํ๋ค.
./node_modules/.bin/tsc --init
--init ํ๋๊ทธ์ ํจ๊ป tsc๋ฅผ ์คํํ ํ, tsconfig.json ํ์ผ์ด ๋ช ๊ฐ์ง ๊ธฐ๋ณธ ๊ฐ๋ค๊ณผ ํจ๊ป ํ๋ก์ ํธ ํด๋์ ์๊ฒผ๋ค.
tsconfig.json
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Projects */
// "incremental": true, /* Enable incremental compilation */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "resolveJsonModule": true, /* Enable importing .json files */
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
/* Emit */
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
// "outDir": "./", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
// "newLine": "crlf", /* Set the newline character for emitting files. */
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
/* Interop Constraints */
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
// "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
ํ์
์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐ(์ถํ์ฌ ํ๋ก๊ทธ๋๋ฐ ์ธ์ฌ์ดํธ)๊ณผ ๋์ผํ๊ฒ ๊ตฌ์ฑ(tsconfig.json ๋ฅผ ์ง์ ๋ง๋ค์ด์ ์๋์ ๊ฐ์ด ์์ฑํด๋ ๋๋ค)
{
"compilerOptions": {
"lib": ["es2015"],
"module": "commonjs",
"outDir": "dist",
"sourceMap": true,
"strict": true,
"target": "es2015"
},
"include": ["src"]
}
๊ฐ ์นดํ
๊ณ ๋ฆฌ ์ค๋ช
:
{
"compilerOptions": {
"lib": ["es2015"], TSC๊ฐ ์ฝ๋ ์คํ ํ๊ฒฝ์์ ์ด์ฉํ ์ ์๋ค๊ณ ๊ฐ์ ํ๋ API
"module": "commonjs", TSC๊ฐ ์ฝ๋๋ฅผ ์ปดํ์ผํ ๋์ ๋ชจ๋ ์์คํ
(CommonJS, SystemJS, ES2015 ๋ฑ)
"outDir": "dist", ์์ฑ๋ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ์ถ๋ ฅํ ๋๋ ํฐ๋ฆฌ
"sourceMap": true, ์์ค ๋งต(Source Map)์ด๋ ๋ฐฐํฌ์ฉ์ผ๋ก ๋น๋ํ ํ์ผ๊ณผ ์๋ณธ ํ์ผ์ ์๋ก ์ฐ๊ฒฐ์์ผ์ฃผ๋ ๊ธฐ๋ฅ
"strict": true, ๊ฐ๋ ฅ์ถ์ฒ ์ ํจํ์ง ์์ ์ฝ๋๋ฅผ ํ์ธํ ๋ ๊ฐ๋ฅํ ์๊ฒฉํ๊ฒ ๊ฒ์ฌ ์ฝ๋๊ฐ ์ ์ ํ์
์ ๊ฐ์ถ๋๋ก ๊ฐ์
"target": "es2015" ์ด๋ค ๋ฒ์ ์ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ์ปดํ์ผ ๋๊ณ ์ถ์ ์ง
},
"include": ["src"] TSC[]๊ฐ ํ์
์คํฌ๋ฆฝํธ ํ์ผ์ ์ฐพ์ ๋๋ ํฐ๋ฆฌ๋ก, ์ด๋ค ํ์ผ๋ค์ด ์ปดํ์ผ ๊ณผ์ ์ ํฌํจ๋๋์ง TSC์๊ฒ ์๋ ค์ฃผ๋ ๋ถ๋ถ, ์ปดํ์ผ ๊ณผ์ ์์ ํฌํจํ ํ์ผ์ ๋ฐฐ์ด []์ ์ ์ผ๋ฉด ๋๋ค.
}
3.
TSLint๋ ์ ํ์ฌํญ์ด์ง๋ง,
๋ณดํต ํ๋ก์ ํธ๋ TSLint ์ค์ ์ ์ ์ํ๋ tslint.json ํ์ผ๋ ํฌํจํ๋ค. ๋ชจ๋ TS ํ๋ก์ ํธ์ ์ผ๊ด๋ ์ฝ๋ฉ ์คํ์ผ(ํญ, ๊ณต๋ฐฑ ๋ฑ…)์ ์ฌ์ฉํ๋๋ก ๊ฐ๋ ฅ ๊ถ์ฅํด์ค๋ค. ์ฝ๋๋ฆฌ๋ทฐ ์๊ฐ์ด ์งง์์ง๋ค.
./node_modules/.bin/tslint --init : ๋ํดํธ tslint.json ํ์ผ ์์ฑ ๋ช
๋ น์ด
tslint.json
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": {},
"rules": {},
"rulesDirectory": []
}
๋ง๋ค์ด์ง ํ์ผ์ ์์ ์ ์ฝ๋ฉ ์คํ์ผ์ ๋ง๊ฒ ํธ์งํ ์ ์๋ค.
์ ์์ ๋๊ฐ์ด ํธ์ง
{
"defaultSeverity": "error",
"extends": ["tslint:recommended"],
"rules": {
"semicolon": false,
"trailing-comma": false
}
}
4.
์ด์ src ๋๋ ํฐ๋ฆฌ์ index.ts๋ฅผ ์ถ๊ฐํ๋ค.
index.ts
console.log("Hello Ts!");
tsc ๋ช
๋ น์ด๋ฅผ ์ด์ฉํด ์ปดํ์ผ
#TSC๋ก ํ์
์คํฌ๋ฆฝํธ ์ปดํ์ผ
./node_modules/.bin/tsc
#NodeJS๋ก ์ฝ๋ ์คํ
node ./dist/index.js
์ด์ JavaScript ํ์ผ๊ณผ .map ํ์ผ์ด ์์ฑ๋์๋ค.
index.js
"use strict";
console.log("Hello Ts!");
//# sourceMappingURL=index.js.map
index.js.map
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC"}
<์ฐ์ต๋ฌธ์ >
ํ์
์คํฌ๋ฆฝํธ ํ์
์ถ๋ก ํ์ธ :
let a = 1 + 2; //number
let b = a + 3; //number
let c = {
apple: a,
banana: b,
};
//let c = {
// apple: a,
// banana: b,
//}
let d = c.apple * 4; //number
๋์ฒด ๊ฐ์ธ ์ฐ์ต ์ค์
npm start ์
๋ ฅํ๋ฉด node๋ก ์คํํ๊ฒ๋
package.json
{
"name": "trash",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node dist/index.js",
"prestart": "tsc"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^17.0.23",
"tslint": "^6.1.3",
"typescript": "^4.6.3"
}
}
tsconfig.json
{
"compilerOptions": {
"lib": ["es2015"],
"module": "commonjs",
"outDir": "dist",
"sourceMap": true,
"strict": true,
"target": "es2015"
},
"include": ["src"],
"exclude": ["node_modules"]
}
'TypeScript > ํ์ ์คํฌ๋ฆฝํธ TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ ์คํฌ๋ฆฝํธ + ๋ฆฌ๋์ค (0) | 2022.05.01 |
---|---|
[ํ์ ์คํฌ๋ฆฝํธ] ํฉํ ๋ฆฌ ํจํด (0) | 2022.04.28 |
[ํ์ ์คํฌ๋ฆฝํธ] ํ์ ์คํฌ๋ฆฝํธ ์ปดํ์ผ๋ฌ(Typescript Compiler, TSC) (0) | 2022.04.27 |
[ํ์ ์คํฌ๋ฆฝํธ] ํ์ ๋ ๋คํ์ฑ, ํ์ ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐ ์ด์งํธ๋ฆฌ ์์ ๋ถ์ (0) | 2022.04.26 |
[ํ์ ์คํฌ๋ฆฝํธ] ํ์ ๋ณ์นญ ์ธํฐ์น์ ์ ์ํ ํ์ฅ (0) | 2022.04.26 |