Search code examples
typescriptpackage.jsontsconfig

How do you export TS for use without bundlers?


I have a tiny package with package.json like this:

{
  "name": "@nomatter/utils",
  "license": "MIT",
  "author": "Dave Stein",
  "version": "0.0.1",
  "scripts": {
    "tsc:watch": "tsc --watch --preserveWatchOutput"
  },
  "type": "module",
  "main": "out-tsc/src/index.js",
  "types": "out-tsc/src/index.d.ts",
  "dependencies": {
    "typescript": "^4.9.5",
    "yup": "^0.32.11"
  },
  "devDependencies": {},
  "description": ""
}

The TS Config in my exported package and the app importing it is the following (Except declaration is only in the exported package):

{
  "compilerOptions": {
    "target": "es2018",
    "module": "esnext",
    "moduleResolution": "node",
    "noEmitOnError": true,
    "lib": ["es2017", "dom"],
    "strict": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "outDir": "out-tsc",
    "sourceMap": true,
    "inlineSources": true,
    "rootDir": "./",
    "incremental": true,
    "declaration": true
  },
  "include": ["**/*.ts"],
}

I can't seem to figure the right configuration for main and types in package.json.

In my app that does import @nomatter/utils, the TSC compiler will complain it can't resolve my import.

I would think TSC can handle reading the imported js files that were generated from TSC. When I look at out-tsc/src/index.js it has one basic export that I expect to be there based on all the config. Not sure why it can't be found?

picture showing folder structure of out-tsc

index.ts is just export const signupSchema = {}; while index.d.ts is just export declare const signupSchema: {};


Solution

  • This is correct, as it was shown in my original question.

      "type": "module",
      "main": "out-tsc/src/index.js",
      "types": "out-tsc/src/index.d.ts",
    

    As @jsejckson hinted, my symlinking was bad. Using npm link properly resolved the issue.