TypeScript Generics Problem using "infer" when a type is deduced from function argument

In the process of learning TypeScript I’m trying to create a function that will deduce the return value from the input parameter as a dictionary with values defined as function objects with propper types. Originally this is taken from frida types library. I am attempting to extend with a feature to define an API of… Read More TypeScript Generics Problem using "infer" when a type is deduced from function argument

Given a generic object (T) and a key of that object (K), can I constrain K to only those keys with a string value?

Very basic example of what I’d like to do: function extractStringValue<T extends object, K extends keyof T>(obj: T, key: K): string { return obj[key]; // error: Type ‘T[K]’ is not assignable to type ‘string’ } // Desired usage const myObj = { stringKey: "hi", boolKey: false } const stringVal = extractStringValue(myObj, "stringKey"); // all OK… Read More Given a generic object (T) and a key of that object (K), can I constrain K to only those keys with a string value?

Generic Component in React – Type is not assignable to type 'ReactNode'. But no error with JSON.stringify()

I’m trying to create a generic component in React TypeScript. Here is the code interface Props<T> { name: T; } function CheckChild<T>(props: Props<T>) { return <div>My name is {props.name}</div>; } export default function Check() { const name = "Alex"; return ( <div> <CheckChild name={name} /> </div> ); } VS code gives me a type error… Read More Generic Component in React – Type is not assignable to type 'ReactNode'. But no error with JSON.stringify()

How to implement a TypeScript generic type for nested properties that resolves to their types?

How to create a generic type that resolves to the type of a nested property Say I have a configuration type type Config = { parent: { child: boolean } } I would like something similar to type ChildConfigType = NestedType<Config, ‘parent.child’> // boolean type ParentConfigType = NestedType<Config, ‘parent’> // {child: boolean} I have a… Read More How to implement a TypeScript generic type for nested properties that resolves to their types?

Function that returns the same object shape as it accepted in typescript

I’d like to constrain a function such that it accept an object as an argument, and returns an object of the exact same shape. That object’s keys can be any string, and I’d like to tell typescript that the return object has the same keys. I.e. interface ConstrainedObject { [key: string]: string[]; } function transformer(argument:… Read More Function that returns the same object shape as it accepted in typescript

How can I index a generic with an with the argument passed in to function in Typescript

Suppose I have an interface in typescript as follows: interface I1 { name: string, age: number } And I have another interface that accesses interface properties: interface I2<T> { getValue: (arg0: keyof T) => any } I want to be able to pass in another interface into I2 such that you can only pass in… Read More How can I index a generic with an with the argument passed in to function in Typescript

Typescript typing with keyof always has ambigious typing, even when specified

type StringTyped = {[key: string]: any}; type ShouldBeStringType = keyof StringTyped; const test:ShouldBeStringType = 2; // but this is allowed? I am trying to have a generic Type that relies on keyof actually returning the type which is specified, but keyof seems to always return the type string | number, even if I specify beforehand,… Read More Typescript typing with keyof always has ambigious typing, even when specified