constraint generic type T and K where K extends keyof T and T[K] is boolean?

Bryan Chen

I can't make following function type checked without using any somewhere.

export function makeToggleState<T, K extends keyof T>(obj: {new(): T}, prop: K) {
  return (state: T, show: boolean|null = null) => {
    if (show === null) {
      state[prop] = !state[prop]
    } else {
      state[prop] = show
    }
  }
}

class State {
    value = true
}

makeToggleState(State, 'value')

I am getting this error:

Type 'false' is not assignable to type 'T[K]'.
  Type 'false' is not assignable to type 'T[string]'.
(parameter) prop: K extends keyof T

What is the best way to tell compiler I want K extends keyof T and T[K] is boolean?

artem

There is no way to have this constraint in makeToggleState declaration, but you can declare it for the function it returns, using intersection of T and mapped type T & {[n in K]: boolean} for its state parameter.

export function makeToggleState<T, K extends keyof T>(obj: {new(): T}, prop: K) {
    return (state: T & {[n in K]: boolean}, show: boolean|null = null) => {
    if (show === null) {
      state[prop] = !state[prop]
    } else {
      state[prop] = show
    }
  }
}

class State {
    value = true;
    name = 'q';
}

const toggleValue = makeToggleState(State, 'value');

const s = new State();
toggleValue(s)


const toggleName = makeToggleState(State, 'name'); // ok

// but does not compile when you try to use it
toggleName(s);

//Argument of type 'State' is not assignable to parameter of type 'State & { name: boolean; }'.
//  Type 'State' is not assignable to type '{ name: boolean; }'.
//    Types of property 'name' are incompatible.
//      Type 'string' is not assignable to type 'boolean'.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Difference between of "K extends keyof T" vs. directly using "keyof T"?

From Dev

typescript generic constraint keyof T and string: ts2322

From Dev

typescript generic constraint keyof T and string: ts2322

From Dev

Why generic constraint T extends Comparable<T> is not sufficient to avoid a cast?

From Dev

C# generics: what's the point of the "X<T> where T: X<T>" generic type constraint?

From Dev

Generic methods T extends

From Dev

Making a generic type constraint on Func<T>

From Dev

How to make <T extends E> generic type argument inclusive?

From Dev

Can't use function type as generic type constraint in Swift

From Dev

Generic type where T can be anything

From Dev

Why can't I use a Guid as a generic type constraint?

From Dev

How to get the typeof(T) from a generic method with base type constraint?

From Dev

How to create an Action<T> with Reflection where T is a discovered generic Type

From Dev

Why does a Generic<T> method with a "where T : class" constraint accept an interface

From Dev

Generic <T extends List<E>> for:each print

From Dev

Java Generic issue: cannot cast Map<K,V> to M extends Map<K, V>

From Dev

Generic Method Definition contains <T extends Class> in-spite of return type is already Defined?

From Dev

Casting bounded wildcard to unbounded wildcard within a generic type is an error (X<Y<? extends T>> to X<Y<?>>

From Dev

Convert IEnumerable<IGrouping<T, K>> to IEnumerable<K>

From Dev

Generic type in generic constraint

From Dev

Generic type in generic constraint

From Dev

Why T in "K <: T" can't be covariance?

From Dev

Get the type of generic T

From Dev

where T : IEnumerable<T> method constraint

From Dev

Typescript: generic that extends a type with a generic

From Dev

k consecutive integers constraint

From Dev

Why doesn't swift infer the appropriate overload function with a generic return argument without a type constraint?

From Dev

How to specify in C# generics such T that is constructible from string? (generic type constraint)

From Dev

is it possible to write an extension method only for List<T> where T is a class that inherits from class K

Related Related

  1. 1

    Difference between of "K extends keyof T" vs. directly using "keyof T"?

  2. 2

    typescript generic constraint keyof T and string: ts2322

  3. 3

    typescript generic constraint keyof T and string: ts2322

  4. 4

    Why generic constraint T extends Comparable<T> is not sufficient to avoid a cast?

  5. 5

    C# generics: what's the point of the "X<T> where T: X<T>" generic type constraint?

  6. 6

    Generic methods T extends

  7. 7

    Making a generic type constraint on Func<T>

  8. 8

    How to make <T extends E> generic type argument inclusive?

  9. 9

    Can't use function type as generic type constraint in Swift

  10. 10

    Generic type where T can be anything

  11. 11

    Why can't I use a Guid as a generic type constraint?

  12. 12

    How to get the typeof(T) from a generic method with base type constraint?

  13. 13

    How to create an Action<T> with Reflection where T is a discovered generic Type

  14. 14

    Why does a Generic<T> method with a "where T : class" constraint accept an interface

  15. 15

    Generic <T extends List<E>> for:each print

  16. 16

    Java Generic issue: cannot cast Map<K,V> to M extends Map<K, V>

  17. 17

    Generic Method Definition contains <T extends Class> in-spite of return type is already Defined?

  18. 18

    Casting bounded wildcard to unbounded wildcard within a generic type is an error (X<Y<? extends T>> to X<Y<?>>

  19. 19

    Convert IEnumerable<IGrouping<T, K>> to IEnumerable<K>

  20. 20

    Generic type in generic constraint

  21. 21

    Generic type in generic constraint

  22. 22

    Why T in "K <: T" can't be covariance?

  23. 23

    Get the type of generic T

  24. 24

    where T : IEnumerable<T> method constraint

  25. 25

    Typescript: generic that extends a type with a generic

  26. 26

    k consecutive integers constraint

  27. 27

    Why doesn't swift infer the appropriate overload function with a generic return argument without a type constraint?

  28. 28

    How to specify in C# generics such T that is constructible from string? (generic type constraint)

  29. 29

    is it possible to write an extension method only for List<T> where T is a class that inherits from class K

HotTag

Archive