arielnmz
arielnmz

Reputation: 9115

How to use exports in Typescript?

I created a TS library that builds to dist as follows:

dist/
  - main.js
  - main.d.ts
  - tools.js
  - tools.d.ts
src/
  - main.ts
  - tools.ts

And my package.json is defined as follows:

"name": "@ws/my-lib"
"exports": {
  "./main": "./dist/main.js",
  "./tools": "./dist/tools.js"
},
"typesVersions": {
  "*": {
    "main": ["./dist/main.d.ts"],
    "tools": ["./dist/tools.d.ts"]
  }
}

However my CRA app fails to import any of my libs:

import { aFunction } from '@ws/my-lib/tools'

Module not found: Can't resolve '@ws/my-lib/tools' in '/path/to/component/component'

What is the correct way to do this?

Upvotes: 2

Views: 2311

Answers (1)

Zou Jeff
Zou Jeff

Reputation: 883

You will need to specify the TS versions with the typesVersions field in package.json. See code below:

{
    "name": "@ws/my-lib"
    "exports": {
        "./main": "./dist/main.js",
        "./tools": "./dist/tools.js"
    },
    "typesVersions": {
        ">=4.1": { // put your desired ts version here
            "*": {
                "main": ["./dist/main.d.ts"],
                "tools": ["./dist/tools.d.ts"]
            }
        }
    }
}

check this document for more details.

Upvotes: 3

Related Questions