Typescript generic type check not working as expected

前端 未结 3 1060
孤独总比滥情好
孤独总比滥情好 2021-01-21 13:48

I have made a simple test fixture:

export interface ITest1 {}
export interface ITest2 {}
export interface ITestGeneric {}

export function test() {
  le         


        
相关标签:
3条回答
  • 2021-01-21 14:05

    This is because typescript uses structural compatibility to determine if two types are compatible. In your case since ITestGeneric has no members, it is basically compatible with anything. If you start adding properties, incompatibilities will quickly appear:

    export interface ITest1 { t1: string}
    export interface ITest2 { t2: number}
    export interface ITestGeneric<T> { value: T}
    
    export function test() {
        let p: ITestGeneric<ITest1> = {} // error
        let q: ITestGeneric<ITest2> = p; // error
    }
    

    You can read more about type compatibility in typescript here

    0 讨论(0)
  • 2021-01-21 14:08

    Thanks to @Titian Cernicova-Dragomir's advice I made a working solution.

    To make a type distinctive, just add property that equals the type's name:

    export interface ITest1 { test1: any }
    export interface ITest2 { test2: any }
    

    The type of the property doesn't matter as far as I've tested. Typescript seems to only look for the property's name, and if that is different, the type is different.

    When instantiating the type just set the property to anything, an empty object or an empty string, it doesn't really matter.

    let p: ITest1 = { test1: {}};
    
    0 讨论(0)
  • 2021-01-21 14:12

    I think since you don't restrict the

    ITestGeneric <T>
    

    it allows it. If you did

    ITestGeneric<T extends ITest1>
    

    It would be more restrictive.

    TypeScript generics are not CSharp generics and are not 100% the same.

    your interface just says it has to be a ITestGeneric of anything.

    0 讨论(0)
提交回复
热议问题