TSUP advice - typescript behaving differently after build

I have little experience with building my typescript code into packages that I can then publish and subsequently then download from the npm registry - however that is exactly what I am trying to do right now. After doing some research it seems TSUP is a favourable tool to use to bundle ts into a package. I followed this video from Matt Pocock https://www.youtube.com/watch?v=eh89VE3Mk5g However, when I import my class from the generated dist folder, typescript is not able to infer types correctly. This occurs when I call one of my factory methods, which generates a method that accepts an argument of a type dependant on the argument provided to the factory method. It is hard to explain 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 infer here that .data() should expect a string argument.

// Typescript is able to do so when the WsSchema class is imported from its source file, but it expects an argument of type 'unkown' when imported from the generated dist

// this should not be valid and typescript should complain here, but it does not when importing from the dist folder
schema.send("MESSAGE").data({})
import { z } from "zod";
import { WsSchema } from "./dist";

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


// Typescript should infer here that .data() should expect a string argument.

// Typescript is able to do so when the WsSchema class is imported from its source file, but it expects an argument of type 'unkown' when imported from the generated dist

// this should not be valid and typescript should complain here, but it does not when importing from the dist folder
schema.send("MESSAGE").data({})
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 Could anyone advise on why typescript is behaving differently post build? I am nowhere near familiar enough with tsup or publishing in general to know where to start in resolving this - any suggestions are appreciated 🙂
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server