mkdir (폴더명)
cd (폴더명)
npm init -y
명령어를 실행해 새로운 프로젝트를 초기화npm init -y
명령어를 실행해 새로운 프로젝트를 초기화한다.npm init -y
npm
을 사용할 준비가 되었으므로, 이제 TypeScript를 설치한다.npm install typescript --save-dev
//tsconfig.json
//compilerOptions 내의 속성은 자유롭게 커스텀 할 수 있다.
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"sourceMap": true,
"outDir": "./dist"
},
"include": [
"src/**/*"
]
}
src
폴더에 index.ts
파일을 만들어 TypeScript 코드를 작성src
폴더에 index.ts
파일을 만들어서 TypeScript 코드를 작성해보자.const sum = (a: number, b: number) => {
return a + b;
};
console.log(sum(10, 20));
ctrl
+ shift
+ p
를 눌러 사용자 설정에 들어간다.{
// ...
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.alwaysShowStatus": true,
"eslint.workingDirectories": [
{"mode": "auto"}
],
"eslint.validate": [
"javascript",
"typescript"
],
}
ctrl
+ ,
npm i -D @babel/core @babel/preset-env @babel/preset-typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint prettier eslint-plugin-prettier
프로젝트 폴더 밑에 .eslintrc.js 파일을 만들고 이하 내용을 붙여 넣는다.
module.exports = {
root: true,
env: {
browser: true,
node: true,
jest: true,
},
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
],
plugins: ['prettier', '@typescript-eslint'],
rules: {
'prettier/prettier': [
'error',
{
singleQuote: true,
tabWidth: 2,
printWidth: 80,
bracketSpacing: true,
arrowParens: 'avoid',
},
],
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'prefer-const': 'off',
},
parserOptions: {
parser: '@typescript-eslint/parser',
},
};
해당 파일 내부에는 prettier
와 typescript-eslint
에 대한 설정이 되어 있으며, 리액트를 위한 설정도 일부 첨가되어 있다.
rules
내에 작성이 되어 있는 내용들은 옵션이므로 전부 작성할 필요 없이 개발자의 취향에 따라 작성해도 되고, 작성하지 않아도 상관없다.
rlues
내에 singeQuote
의 옵션을 true
로 설정해놓았기 때문에 오류가 발생한 것이다.구글링을 통해 살펴보니 vscode의 사용자 설정을 바꿔주면 해결되는 문제였다.
"editor.defaultFormatter"
가 eslint
가 아닌 prettier
로 설정 되어 있었다.eslint
로 변경해주어 오류를 해결했다. ("dbaeumer.vscode-eslint"
)오류의 원인은 Prettier
에서는 CRLF
대신 LF
사용을 권고하고 있었기 때문이다.
OS별로 다른 줄바꿈 타입을 사용하는데,
LF
가 기본값CRLF
가 기본값이다.LF
를 사용을 권고하는 이유는 협업 때문인데 CRLF
와 LF
의 바이트 코드가 달라 형상관리 툴에서 다른 코드로 인식하기 때문이라고 한다. 따라서 Commit을 할 때 줄바꿈 타입이 다르면 파일을 변경하지 않더라도 변경된 것으로 인식하기 때문에 LF
로 통일하였다고 한다.CRLF
로 되어있었다. 😓LF
로만 변경해주면 오류가 해결된다.
좋은 글...잘 보았읍니다.. 앞으로도..좋은 글 부탁드립니다.... -김아무개 올림-