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 mean.

Jul 18, 2022 in TypeSript by Logan
• 2,140 points
2,102 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.

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