typescripttypescript-generics

What's the difference between <T extend any> and <T>?


I've seen some times <T extend any> in a any d.ts files. I don't know what's the difference between <T extend any> and .

Because T extend any never giving restriction to T therefore I think there is no difference to '' that allow anything could pass to the generics.


Solution

  • FYI: In conditional types T extends any can makes a sense

    type MakeMeUnionArray<T> = T[];
    
    let x: MakeMeUnionArray<string | number> = ['1', 2]; // the type is (string | number)[]
    
    type MakeMeSplitedArray<T> = T extends any ? T[] : never;
    
    let y: MakeMeSplitedArray<string | number> = ['1', 2]; // gives an error because the type is now string[] | number[]