r/typescript • • Apr 18 '25

Do I have the wrong idea about how much TS will enforce types?

12 Upvotes

Hey y'all. I'm a bit of a TS noob, and I'm converting an older JS project of mine over to TS, mostly as a learning exercise. However I've encountered a situation where I'm scratching my head about the limitations of TS's type enforcement.

My class has a listeners property that's meant to be overridden in the subclass. TS enforces the basic structure of listeners and correctly complains if I try to define something that isn't a function inside, but it doesn't seem to care about the structure of the function itself unless I'm explicit about the type of listeners in the subclass. Why the heck not? Shouldn't it be inferring the signature of the listeners functions from the base class?

If there's a limitation to how much it "nests" the types, how do I tell what that limitation is? My only hint that I'm defining my listeners wrong is an "any" warning. Am I just supposed to sprinkle type hints everywhere? I'm trying to prevent footguns, and having to be explicit everywhere seems opposed to that.

Also I'm confused about why it still doesn't seem to care about the return type in the last example, even though I'm being explicit there.

TS Playground here

interface Event {
  name: string;
  origin: string;
}

type EventListeners = Record<string, (evt: Event, ...args: unknown[]) => void>;

class PluginBase {
    listeners: EventListeners = {};
}

class SomeValidPlugin extends PluginBase {
    override listeners = {
        "someEvent": (evt) => {            // evt is inferred to be type 'any' instead of Event
            console.log(evt.foo);          // No warning about wrong property!
        }
    }
}

class SomeInvalidPlugin extends PluginBase {
    override listeners = {                 // Correct warning about wrong type
        "someEvent": "A string isn't a function",
    }
}

class SomeIncorrectPlugin extends PluginBase {
    override listeners = {
        "someEvent": () => {               // TS doesn't care about function signature
            return "A string isn't void!";
        }
    }
}

class SomewhatCorrectlyEnforcedPlugin extends PluginBase {
    override listeners: EventListeners = { // Why do I have to be explicit about this?
        "someEvent": (evt) => {
            console.log(evt.foo);          // Correct warning about wrong property
            return "A string isn't void!"; // TS *still* doesn't care about return type for some reason
        }
    }
}

r/typescript • • Apr 18 '25

How does Omit work for union types?

8 Upvotes

I've noticed that when trying to Omit union types, it doesn't behave as expected. When I try to Omit 1 common key union, the resulting type only contains the common keys. Does anyone know why this happens? I found a utility type online of:

DeepOmit<T, K extends string | number | symbol> = T extends any ? Omit<T, K> : never 

And this works just fine for unions types but trying to use this utility type for native types like string, the resulting type does not result in type never. For example, declare const test: DistributiveOmit<string, 'key'>. Could this be because Omit is pretty loose in general with what can be removed, and that JavaScript allows for any keys in objects to be called?


r/typescript • • Apr 17 '25

n-params vs single param

Thumbnail carlos-menezes.com
9 Upvotes

r/typescript • • Apr 18 '25

What is the most convienient way to integrate code generation?

0 Upvotes

Hi! I'm building a library that requires calling a command for typescript code generation, and I'm thinking of improving the developer experience. I want to avoid making people call a separate command while keeping the library predictable. What are my options?

The library consists of a CLI and a SDK, so one way I can think of is to call the tool automatically inside the SDK if NODE_ENV is set to development, it's kinda hacky though :) Appreciate your advice here!


r/typescript • • Apr 16 '25

How in the world do people know how to add types to things like THIS?

26 Upvotes

Hey guys, your favorite confused TypeScript beginner is back!

Somebody please explain to me how the heck is one supposed to be able to create a type like this to satisfy the TS compiler when one has no clue how the (scarcely documented) IMaskMixin even works?! How did the person in the second link come up with their convoluted-looking type AND GET IT RIGHT?! Their proposed solution works, but I have no idea why nor how they had the brains to put it together.

See, this is my major blocker/difficulty with TS. How am I supposed to know enough about the above-mentioned IMaskMixin's internals or some other third-party library to provide correct types for it? In the case of the mixin the documentation is barely existent with regard to how to use it, and the API documentation seems complete but looks really scary and completely opaque to my beginner eyes.

I feel totally lost. Typing third-party stuff feels like a huge poke-in-the-dark chore and a major pain in the ass. I feel like TS requires me to know things about third-party libraries that I'd never care to know as a user of these libraries but I must know if I want to shut up my TS compiler.

Please shed some light.


r/typescript • • Apr 16 '25

Is there a way to generate a type based off a list of values dynamically?

17 Upvotes

I'm working on something where there are literally hundreds of order codes, and new ones added and old ones removed every month. I need a type to encompass all of the order codes to enforce in our code.

Type OrderCode = "VOC" | "MLC" | "ENF" ... etc, etc, etc.

Is there a way to generate the type dynamically since it's not practical to update a giant type every month? How would you approach this?


r/typescript • • Apr 15 '25

How does one type currying functions with generics properly?

6 Upvotes

I'm trying to add strong typescript validation to an existing codebase but getting stuck on one of the functions in the project. The function uses optional currying and the typing seems to get lost. Here is the Typescript Playground link:

TypeScript Playground

This is the closest i have gotten to typing it but seems like mapObj in the last line looses its typing.
it gets inferred as

function mapObj<object>(fn: (value: never, prop: never, obj: object) => never): <U>(obj: U) => U

but i Wish for it to infer as

function mapObj<SyncConflict>(fn: (value: DirtyRaw, prop: keyof SyncConflict, obj: SyncConflict) => DirtyRaw): <SyncConflict>(obj: SyncConflict) => SyncConflict

Is this possible?