Skip to content

Item 70: Mirror Types to Sever Dependencies

要点

  • Avoid transitive type dependencies in published npm modules.
  • Use structural typing to sever dependencies that are nonessential.
  • Don't force JavaScript users to depend on @types. Don't force web developers to depend on Node.js.
  • 避免在发布的 npm 模块中出现传递类型依赖。
  • 使用结构化类型来切断非必要的依赖。
  • 不要强迫 JavaScript 用户依赖 @types,也不要强迫 web 开发者依赖 Node.js。

正文

ts
// parse-csv.ts
import { Buffer } from 'node:buffer'

function parseCSV(contents: string | Buffer): { [column: string]: string }[] {
  if (typeof contents === 'object') {
    // It's a buffer
    return parseCSV(contents.toString('utf8'))
  }
  // ...
}

💻 playground


ts
// parse-csv.d.ts
import { Buffer } from 'node:buffer'
export declare function parseCSV(contents: string | Buffer): {
  [column: string]: string
}[]

💻 playground


ts
export interface CsvBuffer {
  toString(encoding?: string): string
}
export function parseCSV(
  contents: string | CsvBuffer
): { [column: string]: string }[] {
  // ...
}

💻 playground


ts
parseCSV(new Buffer('column1,column2\nval1,val2', 'utf-8')) // OK

💻 playground


ts
/** Anything convertible to a string with an encoding, e.g. a Node buffer. */
export interface StringEncodable {
  toString(encoding?: string): string
}

💻 playground


ts
import { Buffer } from 'node:buffer'
import { parseCSV } from './parse-csv'

test('parse CSV in a buffer', () => {
  expect(parseCSV(new Buffer('column1,column2\nval1,val2', 'utf-8'))).toEqual([
    { column1: 'val1', column2: 'val2' },
  ])
})

💻 playground

Released under the MIT License.