I want to publish a typescript npm package, with types embedded. My folder structure is as follows
dist/
   [...see below]
src/
   global.d.ts
   index.ts 
   otherfile.ts
test/
examples/
For the sake of an example, my global.d.ts file includes typings relevant to the whole project. My index.d.ts file uses these types, and exports functions.
//global.d.ts
interface Dog {
 name: string,
 age: number
}
//more types...
//index.ts
import {func1, func2} from './otherfile.ts'
export default function getDogName(dog: Dog): string {
  return dog.name
}
export {func1, func2}
I can build this code, and it all runs correctly, so typescript knows about global.d.ts. Yet, when I run tsc (using the below config), it generates files into my dist/ folder, which doesnt include the global.d.ts file. ie: dist only contains index.d.ts and otherfile.d.ts. Shouldn't dist/ also contain global.d.ts? If not - how will people who install my package know the types for Dog?
My config looks like this for build time.
//tsconfig.json
{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "outDir": "dist/",
    "declaration": true
  },
  // Only point to typings and the start of your source, e.g. `src/index.ts`
  "include": [ "src/*"],
  "exclude": ["node_modules", "examples", "test"]
 }
For reference, the actual repo is here: https://github.com/bhaviksingh/lsystem
Alright :) After much experimentation, I wanted to share what I did. There are potentially better solutions (would love to find them), but the TLDR is that I changed it from a global declaration file, to a regular TS module, and exported/imported all the types.
So I renamed the global.d.ts file, to just be interfaces.ts, and then exported the type.
//interfaces.ts (renamed from global.d.ts)
export interface Dog {
 name: string,
 age: number
}
//more types...
I imported this type into index.d.ts and re-exported it
//index.ts
import Dog from "./interfaces"
export {Dog} 
TSConfig knows to compile these declarations
//tsconfig.global.json
{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "outDir": "dist/",
    "declaration": true
  },
  // Only point to typings and the start of your source, e.g. `src/index.ts`
  "include": [ "src/*"],
  "exclude": ["node_modules", "examples/", "test/"]
 }
and my package.json points to the compiled declaration file.
//package.json
{
 //...
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    //...
    "build": "tsc -p tsconfig.global.json",
  },
}
There are probably better solutions out there, potentially including /// <reference path="..." />  tags, but this is what I have for now and it works. When people install my library, they have access to the types in intellisense, and if they want to use the types they can be imported.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With