logo
down
shadow

Typescript generic function so that type inference and code hints will work


Typescript generic function so that type inference and code hints will work

By : Saloni Sah
Date : November 22 2020, 04:01 AM
I think the issue was by ths following , Simplifying the types a little so that the compiler only has to infer the keys as string literal types (not as keys to another unknown object) lets the compiler help out with suggestions when writing the second and third parameters:
code :
interface ISettings<TModsKeys extends string, TStatesKeys extends string> {
    mods: Array<TModsKeys>;
    states: Array<TStatesKeys>;
    [other: string]: any;
}

function gen<
    TModsKeys extends string,
    TStatesKeys extends string
>(
    settings: string | ISettings<TModsKeys, TStatesKeys>,
    enabled: Partial<Record<TModsKeys | TStatesKeys, boolean>> = {},
    maps: Partial<Record<TModsKeys | TStatesKeys, string>> = {}
): string {
    //function doesn't return anything yet
    return '';
}

gen({
    mods: ['one', 'two', 'three'],
    states: ['disabled', 'fixed']
}, {
    one: true,
    disabled: true,
}, {
    one: 'first',
    two: 'second',
    fixed: 'static',
});


Share : facebook icon twitter icon
Typescript: Generic class type inference

Typescript: Generic class type inference


By : truth-hind
Date : March 29 2020, 07:55 AM
will be helpful for those in need This should never have worked. Whatever you write in the definition of a generic class or method has to be valid for all possible values of T for which the class or method might be used. The assignment property: T = 5 is not valid if someone does new MyClass() so that T is string. Inference only occurs when you use a generic class or method.
typescript generic type inference in higher order function

typescript generic type inference in higher order function


By : user2525520
Date : March 29 2020, 07:55 AM
Any of those help Typescript 3.4 makes huge improvements around the handling of generic type parameter forwarding. You can read the details here.
While your code will not work as is in 3.4 either, we can get it to work if we change the init function to take in a generic tuple argument. This will cause type parameter forwarding to kick in:
code :
function generate<A extends [any]>(init: (...p: A) => void) {
    return function (...p: A): A[0] {
        init(...p)
        return p
    }
}

const g = generate(function AAA<T>(p: T) {
    console.log(p)
})

const result = g({
    x: 1
})

result.x // ok now
Typescript generic type inference from default value

Typescript generic type inference from default value


By : Pedro Esquivel Zempo
Date : March 29 2020, 07:55 AM
With these it helps Consider the following function. , I think you could accomplish this with overloads:
code :
public static function convert<T>(t: T): T;
public static function convert<T, U>(t: T, conversion: (t: T) => U): U;
public static function convert<T, U>(t: T, conversion?: (t: T) => U) {
    return conversion ? conversion(t) : t;
}

..

const foo = convert(1)             // inferred type 1
const bar = convert(1, x => ({x})) // inferred type { x : number }
const bar = convert(1 as 1, x => ({x})) // inferred type { x: 1 }
Typescript generic of generic type inference

Typescript generic of generic type inference


By : Ero Senin
Date : March 29 2020, 07:55 AM
Any of those help I don't know exactly why is type checker not trying to infer val type, but it has workaround though. Just tell type checker to do the lookup to that generic type and find val type.
code :
type ValType<T extends Generic<any>> = T["val"];

class GenericWrap<U extends Generic<any>> {

    generic: U;

    getValue(): ValType<U> {
        return this.generic.val;
    }
}
TypeScript generic function param and return type inference

TypeScript generic function param and return type inference


By : Andrew Wayne Stengel
Date : October 01 2020, 08:00 PM
seems to work fine On the first one, K is explicitly defined. So, when you pass name, Typescript can infer the type. However on the second one, getProperty2 function expects any keyof T. So, the return type becomes string | number (if you add more key to obj, this will become more complex). That's why Typescript is not sure which one will you get.
You can fix this by using as as follows since you are sure that you will get a string in return.
code :
const name3 = getProperty2(obj, 'name') as string;
name3.length;
Related Posts Related Posts :
  • Using generators in Typescript tsconfig
  • Any class that extends class type
  • Generic function argument affecting the return type with default implementation
  • NullInjectorError: No provider for e! on Angular 7
  • TypeScript filter array by element name
  • TypeScript inheritance type of functions
  • Typescript overloading optional parameters non working?
  • Typescript unable to detect non-null type
  • Ionic InAppBrowser iOS opens new tab and in devapp 'loading...' screen
  • Named Type association with Typescript
  • Preact: sometimes blank page upon reload
  • Applying array as arguments to Date constructor
  • Possible to represent fixed-length arrays / "tuples" with @mojotech/json-type-validation?
  • Typescript compiler -disable or remove
  • Ionic 4 error with function for deleting from local storage
  • specify argument has to have keyof property
  • Function chaining in TypeScript with Omit types
  • VSCode TypeScript Auto Import Suggestions - Symbol Resolution Logic
  • TS2339 - 'Property doesn't exist on type' error in an apparently valid TS file
  • TypeScript: Partially "Partial" type
  • Union type using optional properties
  • Typescript definition(xxx.d.ts)
  • nativescript listview display nothing , it just show after leaving page and coming back
  • typescript generics limit number | string
  • Type 'string | AddressInfo' has no property 'port' and no string index signature
  • Multiple index with typescript generics, how?
  • Disabling console.log() in production
  • Accessing members of inherited class from parent
  • How do I type check a snippet of TypeScript code in memory?
  • use generic type in typescript as property
  • Type '{ name: string; } & Pick<TProps, Exclude<keyof TProps, "name">>' is not assignable to ty
  • IDE to recognize Cucumber steps in TypeScript format
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org