Relative Content

Tag Archive for typescripttypescript-generics

Improve my typescript code for fully typed map of functions for known keys

With reference to my code below, I am trying to create a Map<string, Function> (eventHandlerMap in the code below) such that each of its keys is a string from (PortalEvent in the code below) Union type. The value of each property should be a function that must return an object of specific type (in this example one of UserComment or Author).

Enforce return type of method referenced by its name

I have a function that takes as parameters a method name as a string and the object that contains that method. I can easily check that the class of the object contains the method name. But I also want to make sure that it is callable and the return type is of a certain type. How can I do that?

Typescript generic type read only in part of the type declaration

I have a sanitizer that may or may not receive a set of possible values. If it receives, it should enforce those to be the only possible values, if not, it should just check if it is string type, and if any of the checks failed, I want to return defaultValue. My code so far is the following:

Get type of value in a object with a string path

i am a free programmer and recently learning about Typescript, more specifically i am experimenting with writing some code to get the value/typeof of any value inside a json object based on the path string, supporting correct pathing. Here is mine

Aggregation function via config array Typescript type

I’ve been trying to get these types working for a while now. Don’t worry about the implementation. I only want to fix the types. The function takes and reduces over the config array using the fn property. Each config item should have the aggregated return type from the previous steps passed the fn function. The types work, but the autocomplete in the editor is broken when I create a new object in the array, TS Server thinks it’s an array type. Can this be fixed?

Aggregation function that is defined via config array

I’ve been trying to get these types working for a while now. Don’t worry about the implementation. I only want to fix the types. The function takes and reduces over the config array using the fn property. Each config item should have the aggregated return type from the previous steps passed the fn function. The types work, but the autocomplete in the editor is broken when I create a new object in the array, TS Server thinks it’s an array type. Can this be fixed?

How to set variable type when the variable has huge generic type in TypeScript?

I am a fan of script languages and was using javascript for long time. But for a new project I decided to see how ts may help my development or maintaining part of the project. My biggest problem atm is finding types. Let me give an example. I am using drizzle for orm. For some reason I need to store the findMany query config. To be more clear I am pasting the whole type of the findMany function and class here.