Why typescript complains that XXX is assignable to the constraint of type T but T could be instantiated with a different subtype of constraint X

0 votes

My code is this:

export interface TreeItem {
  id: string;
  children: this[];
  collapsed?: boolean;
}

const createTreeItem = <T extends TreeItem>(): T => {
  return {
    id: 'root',
    children: []
  }
}

But I get an error on the return type of createTreeItem which is the following:

TS2322: Type '{ id: string; children: never[]; }' is not assignable to type 'T'.   '{ id: string; children: never[]; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'TreeItem'.

I have absolutely no idea what does this means.

Jul 13, 2022 in TypeSript by Logan
• 2,140 points
2,225 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

So let's say you call your function as follows:

let node = createTreeItem<TreeItem>()

All good, right? The return type T is TreeItem, and the object your generic function is hardcoded to return does in fact have a type that qualifies it as a TreeItem:

{ id: string; children: never[]; }

But the point of giving your function a type parameter is to allow it to be called with other types of T, as long as they extend TreeItem. So the following should be a legal call:

export interface BidiTreeItem {
  id: string;
  children: this[];
  parent: this;
  collapsed?: boolean;
}

let node = createTreeItem<BidiTreeItem>()

The call is legal, since BidiTreeItem satisfies the constraint T extends TreeItem. The return type of this call, as declared in your function definition, is BidiTreeItem, but what your function returns is NOT a BidiTreeItem.

If you reread the error message again but with the above example in mind, it will now make sense to you. But just in case, below I will translate each piece of the error message.

answered Jul 14, 2022 by Nina
• 3,060 points

edited Mar 5, 2025

Related Questions In TypeSript

0 votes
1 answer

Argument of type 'any' is not assignable to parameter of type 'never'.

All you have to do is define ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
67,344 views
0 votes
1 answer

Only refers to a type, but is being used as a value here

To do type checking at runtime with ...READ MORE

answered Jun 8, 2022 in TypeSript by Nina
• 3,060 points
62,326 views
0 votes
0 answers

What is a type in Typescript for the Component class in Angular 2+?

I have a small problem, but big ...READ MORE

Jul 5, 2022 in TypeSript by Logan
• 2,140 points
1,348 views
0 votes
0 answers

What should the return type of Promise.race be? (Typescript)

For example in the code below, what ...READ MORE

Jul 5, 2022 in TypeSript by Logan
• 2,140 points
1,039 views
0 votes
1 answer

What is TypeScript and why would I use it in place of JavaScript?

TypeScript is a superset of JavaScript which primarily ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
1,338 views
0 votes
1 answer

Typescript Errors: How do I ignore the error "property does not exist on value of type"

to ignore it globally, in your tsconfig.json, ...READ MORE

answered Jul 26, 2023 in TypeSript by john

edited Mar 5, 2025 13,156 views
0 votes
1 answer

Interface type check with Typescript

You can achieve what you want without ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
7,962 views
0 votes
1 answer

How to set meta tags using Angular universal SSR and ngx-seo plug-in?

first Install the plug-in with npm i ngx-seo ...READ MORE

answered Feb 11, 2022 in Others by narikkadan
• 86,360 points
3,164 views
0 votes
1 answer

How to use next-seo for setting nextjs meta tag with multiple OGP images?

https://github.com/garmeeh/next-seo use this git repo that contains ...READ MORE

answered Feb 24, 2022 in Others by narikkadan
• 86,360 points
7,221 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP