Inferred type in generic with multiple type parameters

Arjes

I am trying to setup a generic where the parameter is the key of another object. I can accomplish this using extends keyof when both types are parameters are to the function.

However when the type providing the list of keys is not an argument, just a generic type, typescript requires both generic types to be set.

Consider the following code:

interface Bar {
  z: string;
  t: number;
}

declare function foo1<T extends keyof Bar>(x: T)
let t1 = foo1('z');

declare function foo2<K, T extends keyof K>(x: T)
let t2 = foo2<Bar>('t');

declare function foo3<T>(x: T)
let t3 = foo3<keyof Bar>('t');

Playground link

Function foo2 fails since the second type T is not specified. However I feel like TS should be able to infer the correct typings without an explicit second type.

foo3 is my workaround, but isn't as nice to work with, is it possible to have typescript perform this inference, or will this be a feature request / bug report to the TS team?

Shane

You are close with foo2. Here is what it's supposed to be to make it work.

declare function foo2<T, K = keyof T>(x: K)
let t2 = foo2<Bar>('t');

Instead of expecting a new generic type parameter, you can assign one to a type (in this case the type of T).

It's also possible to directly assign the paramaters type to the keyof the given generic type T.

declare function foo4<T>(x: keyof T);
let t4 = foo4<Bar>('t');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is a generic type unable to be inferred?

From Dev

Automatically inferred generic type in trait

From Dev

Scala method Inferred generic type

From Dev

Dependent type constraints in generic class with multiple parameters

From Dev

Why can the type not be inferred for this generic Clamp method?

From Dev

Chained generic type inferred closures in swift

From Dev

How do I create a Type with multiple generic type parameters

From Dev

Get generic parameters of a generic type

From Dev

Get generic parameters of a generic type

From Dev

Nested Generic Type Parameters Parameters

From Dev

Why is it impossible to implement a generic interface multiple times with different type parameters?

From Dev

Can you enforce the same constraint on multiple generic type parameters?

From Dev

Open generic type arguments cannot be inferred from the usage

From Dev

Generic method with property expression "type arguments cannot be inferred from useage"

From Dev

Incorrectly Inferred Type in Generic Closure with Protocol Extension Constrained by Self

From Dev

Multiple type parameters in type class?

From Dev

Multiple type parameters in type class

From Dev

Get number of generic type parameters

From Dev

Infering generic type parameters in Scala

From Dev

Play Writes with generic type parameters

From Dev

Return a Generic Type without specifying type parameters

From Dev

Are parameters of generic type or type object, thread safe?

From Dev

GenericTypeDefinition for type with multiple parameters

From Dev

Inferred Type and Dynamic typing

From Dev

Type incorrectly inferred

From Dev

Nothing inferred for type parameter

From Dev

Inferred type is not general enough

From Dev

Inferred type is ambiguous

From Dev

Why is this type not inferred by compiler?

Related Related

  1. 1

    Why is a generic type unable to be inferred?

  2. 2

    Automatically inferred generic type in trait

  3. 3

    Scala method Inferred generic type

  4. 4

    Dependent type constraints in generic class with multiple parameters

  5. 5

    Why can the type not be inferred for this generic Clamp method?

  6. 6

    Chained generic type inferred closures in swift

  7. 7

    How do I create a Type with multiple generic type parameters

  8. 8

    Get generic parameters of a generic type

  9. 9

    Get generic parameters of a generic type

  10. 10

    Nested Generic Type Parameters Parameters

  11. 11

    Why is it impossible to implement a generic interface multiple times with different type parameters?

  12. 12

    Can you enforce the same constraint on multiple generic type parameters?

  13. 13

    Open generic type arguments cannot be inferred from the usage

  14. 14

    Generic method with property expression "type arguments cannot be inferred from useage"

  15. 15

    Incorrectly Inferred Type in Generic Closure with Protocol Extension Constrained by Self

  16. 16

    Multiple type parameters in type class?

  17. 17

    Multiple type parameters in type class

  18. 18

    Get number of generic type parameters

  19. 19

    Infering generic type parameters in Scala

  20. 20

    Play Writes with generic type parameters

  21. 21

    Return a Generic Type without specifying type parameters

  22. 22

    Are parameters of generic type or type object, thread safe?

  23. 23

    GenericTypeDefinition for type with multiple parameters

  24. 24

    Inferred Type and Dynamic typing

  25. 25

    Type incorrectly inferred

  26. 26

    Nothing inferred for type parameter

  27. 27

    Inferred type is not general enough

  28. 28

    Inferred type is ambiguous

  29. 29

    Why is this type not inferred by compiler?

HotTag

Archive