Spread operator with React Props in Typescript (error: …could be instantiated with a different subtype of constraint {})

Asked
Active3 hr before
Viewed126 times

6 Answers

propstypescripterrorreact
90%

However this fails with the error: 'Pick<LAP, Exclude<keyof T, "iS">>' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}', Why did the Buddha contradict about His teachings in these two different suttas? ,I am trying to write a Higher Order Component in React using Typescript that receives the props, 'consumes' one of them and then passes on the remainder to the Child component.

Actually, Typescript is catching a Problem with your component:

 const Component = testConnect(({ iS }) => <div>{iS}</div>);

 <Component iS={...} />

(a) pass on all props (instead of just rest) to the component.

   <Child   {...props} />

(b) Make sure that no prop named "iS" can be passed in by excluding the key iS from T:

 testConnect<T>(Child: React.ComponentType<Omit<T, "iS">>>): React.ComponentType<T> {
load more v
88%

Decida qué cookies quiere permitir. Puede cambiar estos ajustes en cualquier momento. Sin embargo, esto puede hacer que algunas funciones dejen de estar disponibles. Para obtener información sobre eliminar las cookies, por favor consulte la función de ayuda de su navegador.,Test inteligencia Steve Jobs – Con Contraseña coregistros – retro,Test inteligencia Steve Jobs – Con Contraseña coregistros

Pretag
 Pretag team - issue, fix, solve, resolve
72%

If no types are declared, TypeScript will automatically assign a type depending on the types of the Array values.,Arrays may also be destructured in TypeScript, keep in mind that all the assigned types to the array values won’t be lost when destructured.,In TypeScript 2.8, the Exclude type was added to the standard library, which allows an omission type to be written as shown directly below:

  const myName: string = 'Robert';
load more v
65%

<!-- Please try to reproduce the issue with the latest published version. It may have already been fixed. For npm: typescript@next This is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly --> TypeScript Version: 3.7.2, also tested in nightly (3.8.0-dev.20191224),I would like to expose a simpler example for which an unexpected error is emitted too, that perhaps helps in tracking down the issue here:,<!-- Search terms you tried before logging this (so others can find this issue more easily) --> Search Terms: "is assignable to the constraint of type", "could be instantiated with a different subtype of constraint"

Code

function test<T extends {accepted: boolean}>(cb: (value: T) => void) {
  return (data: Omit<T, 'accepted'>) => cb({...data, accepted: true});
}

Actual behavior:

Argument of type 'Pick<T, Exclude<keyof T, "accepted">> & { accepted: true; }' is not assignable to parameter of type 'T'.
  'Pick<T, Exclude<keyof T, "accepted">> & { accepted: true; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{ accepted: boolean; }'.
load more v
75%

This week I wanted to talk about TypeScript generics. With an incredible type system backing TypeScript, generics give you the power to easily reuse logic and give you the ability to dynamically define what type of data you're expecting back from that logic. Let's look at an example. ,Thanks for reading the first TypeScript tip of the week! Tell me what you think in the comments.,This is all well and good, but let's look at a more practical example, one I use often when writing TypeScript code.

function foo<T>(arg: T): T {
    return arg;
}


const bar = foo<string>('baz');
load more v
40%

Pretag
 Pretag team - issue, fix, solve, resolve

Other "props-typescript" queries related to "Spread operator with React Props in Typescript (error: …could be instantiated with a different subtype of constraint {})"