24.06.30

강연주·2024년 6월 30일

📚 TIL

목록 보기
14/186

[TypeScript 초기 셋팅]


npx create로 새 프로젝트를 생성할 때마다 오류가 난다. 저번에는 GPT한테 물어봐서 force가 들어가는 명령어로 캐시를 삭제해 해결할 수 있었지만, 매번 이럴 수는 없다. yarn은 명령어만 잘 쓰면 문제 없는 듯? 우선 yarn 사용할게여..

문제 있읍니다.
run build 하고 start했을 때 module이 있고 없고
dist 폴더가 있고 없고 난리가 납니다.

  • mkdir 이름 (프로젝트 디렉토리 새로 만들기)
  • 해당 디렉토리로 이동 후 npm init -y
  • tsc --init --rootDir ./src --outDir ./dist --esModuleInterop --module commonjs --strict true --allowJS true --checkJS true
  • package.json의 내용 수정
    "scripts": {
    "start": "tsc && node ./dist/index.js",
    "build": "tsc --build",
    "clean": "tsc --build --clean"
    },
  • src 폴더 생성
  • code .으로 VS code 실행

또는

  • yarn create vite my-vue-app --template react-ts
  • VS Code 열고 폴더 열기
  • yarn install

[splice]

splice 안뇽, 오랜만이다.
배열의 기존 요소를 삭제하거나 대체한다.
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// Inserts at index 1
console.log(months);
// Expected output: Array ["Jan", "Feb", "March", "April", "June"]

  removeBook(user: User, book: Book): void {
    if (user.getRole() !== Role.LIBRARIAN) {
      console.log("사서만 도서를 삭제할 수 있습니다.");
      return;
    }

    const index = this.books.indexOf(book);
    if (index !== -1) {
      this.books.splice(index, 1);
    }
  }

splice를 보니, 기능 구현과 디자인과 워크플로우로 오염(?)되지 않은 순수한 알고리즘 코드카타의 세계로 도망치고 싶어진다.


[index signature]

https://www.typescriptlang.org/docs/handbook/2/objects.html

타입 속 속성들의 이름은 모르지만 그 값들의 형태는 알고 있을 때,
인덱스 시그니처를 사용해서 값들을 작성하세욥!

interface StringArray {
		[index: number]: string;
	} 
const myArray: StringArray = getStringArray();
const secondItem = myArray[1];          
const secondItem: string

위 예시의 StringArray 인터페이스가 인덱스 시그니처를 사용하고 있습니다.
StringArray의 index를 숫자로 매기고, 문자열을 리턴하죠.
인덱스 시그니처의 속성으로는 문자열, 숫자, 심볼, template string patterns, 그리고 상기 자료형으로만 이루어진 union type이 사용될 수 있습니다.

개인 과제에서 API로 나라 정보를 불러왔을 때, nativeName의 key값을 정확히 표기하기 애매했기 때문에 인덱스 시그니처가 사용되었다!

그런데 선생님 그 아래 내용이 이해가 가질 않습니다...
https://radlohead.gitbook.io/typescript-deep-dive/type-system/index-signatures

12시간 자고 일어났더니 조금 덜 죽은 상태가 되었다.

profile
아무튼, 개발자

0개의 댓글