egoist / tsup

The simplest and fastest way to bundle your TypeScript libraries.
https://tsup.egoist.dev
MIT License
8.46k stars 209 forks source link

typescript unable to make inferrence after build #1143

Open FlynnHillier opened 2 weeks ago

FlynnHillier commented 2 weeks ago

After building my project, using the following script: tsup src/index.ts --format cjs,esm --dts and then importing my class from the generated dist folder - typescript is unable to make an inferrence which it is able to make if the class is used directly from its non-built src file.

It is hard to explain the behaviour without an example, so here is a simple one:

import { z } from "zod";
import { WsSchema } from "./dist";

const schema = new WsSchema({
  TEST: z.object({}),
  MESSAGE: z.string(),
});

// Typescript should throw up a fuss at this line, but it does not
schema.send("MESSAGE").data({})

Because we passed the string "MESSAGE" to the send method, typescript be able to infer what type of argument the data method should then expect. In this case, it should only accept a string, however the argument in the built version is instead of type unknown.

In the orginal src file (non-built) this behaviour is not present, and typescript is able to correctly infer what type should be passed to the data method.

Here is the project repo, this behaviour should be very easily re-creatable if you clone the project and run the build script, and then use the example provided above.

https://github.com/FlynnHillier/ws-schema/tree/lib

Upvote & Fund

Fund with Polar