본문 바로가기

IT/typescript31

[typescript] subtype 타입 = 집합 number type ≥ number literal type - number는 number literal의 슈퍼타입 - number literal은 number의 서브 타입 타입 호환성 - number  - number literal const b:42 = 42const d:number = bconst e:number = 42const f: 42 = e //Type 'number' is not assignable to type '42'.(2322)  정리작은거는 큰거에 넣을 수 있고 큰거는 작은거에 못 넣는다. 2024. 8. 14.
[typescript] pnpm monorepo에서 paths 설정하기 코드아래의 설명만으로는 부족하다.https://github.com/insidedw/monorepo-pnpm-ts GitHub - insidedw/monorepo-pnpm-tsContribute to insidedw/monorepo-pnpm-ts development by creating an account on GitHub.github.com packages 구조project - apps - calculator - src - libs - shared - src root에 tsconfig.json 추가baseUrl 기준으로 path를 지정{ "compilerOptions": { "baseUrl": ".", "paths": { "@S_shared/*": ["libs.. 2024. 7. 15.
[typescript] is 개념TypeScript의 "is" 연산자는 주로 사용자 정의 타입 가드(User-defined Type Guards)에서 사용됩니다. 타입스크립트는 정적 타입 언어로, 컴파일 시점에 타입을 확인합니다. 하지만 런타임에는 여전히 자바스크립트처럼 동적으로 동작합니다. 이로 인해 변수의 타입을 런타임에 확인할 필요가 있을 때가 있습니다. "is" 연산자는 이럴 때 매우 유용합니다. 사용자 정의 타입 가드 (User-defined Type Guards)"Is" 연산자는 함수가 특정 타입의 값을 반환하는지 여부를 명시적으로 컴파일러에게 알려주는 역할을 합니다. 이는 특히 복잡한 타입 검사나 커스텀 타입 검사가 필요한 경우에 유용합니다. 예시아래는 사용자 정의 타입 가드가 어떻게 사용되는지 보여주는 예제입니다. i.. 2024. 6. 12.
[typescript] in 개념TypeScript에서 in 연산자는 객체에 특정 속성이 존재하는지 확인하는 데 사용됩니다. 이는 자바스크립트의 in 연산자와 동일한 방식으로 작동하며, 객체의 프로토타입 체인에서도 속성을 확인합니다.예시in 연산자의 사용을 이해하기 위해 간단한 예제를 살펴보겠습니다.interface Person { name: string; age: number;}const person: Person = { name: "John", age: 30};if ('name' in person) { console.log("Name exists in person");}if ('address' in person) { console.log("Address exists in person");} els.. 2024. 6. 12.
[typescript] as const https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions 를 읽고 정리한 글입니다. as const를 선언하면, - string에서 'hello'로 추론됨 (아래 코드 확인) - obejct는 readonly로 설정 - array는 readonly tuples로 변경 let x = ‘hello’ // string으로 추론let x = ‘hello’ as const // hello으로 추론let y = [10, 20] // number[]으로 추론let y = [10, 20] as const // readonly [10, 20]으로 추론let z = { text: 'hello' } // { t.. 2024. 6. 3.
[clean code] 놓치기 쉬운 것들 Use explanatory variables // bad const address = "One Infinite Loop, Cupertino 95014"; const cityZipCodeRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/; saveCityZipCode( address.match(cityZipCodeRegex)[1], address.match(cityZipCodeRegex)[2] ); // good const address = "One Infinite Loop, Cupertino 95014"; const cityZipCodeRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/; const [_, city, zipCode] = address.. 2024. 4. 22.