Relative Content

Tag Archive for typescripttypescript-generics

Infer typings of Record

I’m currently using Typescript to strongly-type names and values for feature flags in our application. eg:

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: