Node.js/TypeScript

Node.js/TypeScript

[TypeScript] union type + primitive type에서 type hint 보존

Problem위 Animal 의 type 처럼 union type 을 지정할 때, 캐스팅이 가능한 primitive types 를 같이 넣으면type property 의 type hint 가 string 으로 합쳐져 Cat 이나 Dog 에 대한 hint 를 받을 수 없음 Resolvedinterface Animal { type: Cat | Dog | (string & {});}위처럼 primitive type 을 {} 와 함께 intersection type 으로 만들면 된다

Node.js/TypeScript

[TypeScript] 함수 타입 표현(Function Type Expressions) vs 메소드 선언(Method Declaration)

서론 함수 선언문(Function Declarations)과 함수 표현식(Function Expressions) 에 대한 내용이 아닙니다.관련 내용은 검색 해보시면 많이 나와있다보니 이 글에서는 다루지 않습니다. 함수(Function) 와 메서드(Method) 의 차이점을 알고 있다는 점을 전제로 둡니다.  함수 타입 표현 (Function Type Expressions)(...args: ArgTypes[]) => ReturnType위와 같은 형태로 Call Signature 를 정의하는 데 사용. 주로 함수를 변수에 할당하거나, 다른 함수의 매개변수로 전달할 때 사용됨. [예시]interface 인터페이스_예시 { foo: () => void;}type 타입_예시 = { foo: () => ..

Node.js/TypeScript

[TypeScript] eslint-plugin-import - There should be no empty line between import groups

SettingsNode: 20.11.17@typescript-eslint/eslint-plugin: 7.0.1@typescript-eslint/parser: 7.0.1eslint: 8.56.0eslint-config-prettier: 9.1.0eslint-import-resolver-typescript: 3.6.1eslint-plugin-import: 2.29.1eslint-plugin-prettier: 5.1.3prettier: 3.2.5더보기// .eslintrc.jsmodule.exports = { parser: '@typescript-eslint/parser', parserOptions: { project: 'tsconfig.json', tsconfigRootD..

Node.js/TypeScript

[TypeScript] axios file upload/download

Settings Node.js v18.14.0 axios v1.3.5 file.service.ts import { AxiosInstance, AxiosRequestConfig } from 'axios'; import { createReadStream, createWriteStream } from 'fs'; import { dirname, join as joinPath } from 'path'; import FormData from 'form-data'; // import { randomString } from 'YOUR_RANDOM_STRING_GENERATOR'; export class FileService { constructor(private readonly axios: AxiosInstance) ..

Node.js/TypeScript

[TypeScript] mixin

comment - 저장용 global.d.ts type Constructor = new (...args: any[]) => T; type UnionToIntersection = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; mixin.ts function applyMixin(derived: Constructor, target: Constructor) { const proto = Object.getPrototypeOf(target); Object.getOwnPropertyNames(proto).forEach((name) => { Reflect.defineProperty( derived.prototype, ..

BlockHead
'Node.js/TypeScript' 카테고리의 글 목록