6

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.

Any help ?

2 Answers 2

9

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. Note that the first sentence is the conclusion, so it will go last in this table:

this part of the error message... means this (using above example)...
'{ id: string; children: never[]; }' is assignable to the constraint of type 'T' '{ id: string; children: never[]; }' is consistent with the constraint T extends TreeItem
but 'T' could be instantiated with a different subtype of constraint 'TreeItem'. but T could be instantiated with a different subtype of TreeItem, for example BidiTreeItem.
TS2322: Type '{ id: string; children: never[]; }' is not assignable to type 'T'. The object your function is returning is not guaranteed to be assignable to type T, because T could be some other subtype such as BidiTreeItem .
9
  • 1
    Nice example. Here I have written an article about it Dec 17, 2021 at 11:01
  • 4
    @captain-yossarian, nice! BTW, what do you think about me asking the Typescript folks to make a better worded error message? I was thinking something along the lines of: "You've fallen for one of the two classic blunders! The first being never set noImplicitAny: false, but only slightly lesser known: never go against a parametrized return type constraint when type safety is on the line! HAHAHAHAHAHAHA *program dies*"
    – Inigo
    Dec 17, 2021 at 11:18
  • I'm not very well aware about error message strategy they are using. Probably worth creating some document with explanation of every error code. For instance: TS2322 - means that ..... Dec 17, 2021 at 11:20
  • 1
    you can remove :) And I will remove mine Dec 17, 2021 at 12:12
  • 1
    @sezanzeb: If Typescript allowed you to do that, yes, that would be a Typescript failure. The error message is Typescript telling you you are doing something illogical.
    – Inigo
    Oct 23, 2022 at 23:42
-3

You can add "as T after the return" :

  const createTreeItem = <T extends TreeItem>(): T => ({
      id: 'root',
      children: []
  } as T);
3
  • 4
    That doesn't answer the question, and only hides the problem. If you did this, then there would be no reason to use generics in the first place!
    – Inigo
    Dec 17, 2021 at 10:33
  • T is already a generic type that extends TreeItems so this is not just hiding the problem. Typescript just needs to know what type your object ison return because it is not infered because you use return while creating an object direclty with {} and as you write on your code T should be the type of the result of the function. Dec 17, 2021 at 10:42
  • See my answer. Your "fix" undermines the point of declaring the return type to be T. If the actual return type disregards the type parameter T, then it is lying. Your fix hides this lie. If the idea is that the compiled Javascript doesn't need to know about the lie, then why bother even having a generic parameter T at all? You would just declare this function as such (createTreeItem = (): { id: string; children: never[]; })
    – Inigo
    Dec 17, 2021 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.