Home > Mobile >  Typescript publish to NPM without JS
Typescript publish to NPM without JS

Time:11-13

How to publish my *.ts file to NPM that only contain type & interface definition?

There is no exported javascript function, object, etc.
Should I delete this? "main": "index.js", -or- replace with "main": "dist/types.ts",

Should I compile *.ts to *.d.ts?

The 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>>

CodePudding user response:

I would take a look at examples from DefinitelyTyped (e.g. @types/... packages).

Based on this, I believe:

  • "main" can be an empty string ("")
  • "types" should point to your "index.d.ts"
  • Your package should not include any *.ts files (it should contain *.d.ts files instead)

This might also be useful to read through: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html

  • Related