【问题标题】:Typescript publish to NPM without JS打字稿在没有 JS 的情况下发布到 NPM
【发布时间】:2021-12-24 20:32:26
【问题描述】:

如何将我的*.ts 文件发布到仅包含类型和接口定义的 NPM?

没有导出的javascript函数、对象等
我应该删除这个吗? "main": "index.js", - 或 - 替换为 "main": "dist/types.ts",

我应该把*.ts编译成*.d.ts吗?

types.js:

export type Optional<T>                    = T|null|undefined
export type OptionalOrFalse<T>             = Optional<T|false>
export type SingleOrArray<T>               = T|T[]

export type DeepArray<T>                   = (T|DeepArray<T>)[] // ===       T[]  |  T[][]  |  T[][][]  |  ...
export type SingleOrDeepArray<T>           = T|DeepArray<T>     // === T  |  T[]  |  T[][]  |  T[][][]  |  ...

export type Factory<T>                     = () => T
export type ProductOrFactory<T>            = T|Factory<T>
export type ProductOrFactoryDeepArray<T>   = (ProductOrFactory<T> | ProductOrFactoryDeepArray<T>)[] // ===         T[]|F[]  |  T[][]|F[][]  |  T[][][]|F[][][]  |  ...
export type ProductOrFactoryOrDeepArray<T> =  ProductOrFactory<T> | ProductOrFactoryDeepArray<T>    // === T|F  |  T[]|F[]  |  T[][]|F[][]  |  T[][][]|F[][][]  |  ...

export type Dictionary<TValue>             = { [key: string]: TValue }
export type ValueOf<TDictionary>           = TDictionary[keyof TDictionary]
export type DictionaryOf<TDictionary>      = Dictionary<ValueOf<TDictionary>>

【问题讨论】:

  • 我认为您可能需要像命名空间一样导出,据我所知,该文件应称为“.d.ts”以用于类型定义

标签: typescript npm


【解决方案1】:

我会看一下来自 DefinitelyTyped 的示例(例如 @types/... 包)。

基于此,我相信:

  • "main" 可以是空字符串 ("")
  • "types" 应该指向你的 "index.d.ts"
  • 您的包不应包含任何*.ts 文件(它应包含*.d.ts 文件)

这也可能有助于阅读: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html

【讨论】:

  • 现在,我仍然包含一个由 TS 生成的空index.js。不是真的空,只是没用的代码export {};
猜你喜欢
  • 2018-02-04
  • 2017-12-02
  • 1970-01-01
  • 2013-05-23
  • 2018-09-26
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多