Skip to content

No type checking when generic class/interface do not have any properties and '{}' is used as generic type variable #14320

Closed
@alhugone

Description

@alhugone

TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)
with strictNullChecks

Code

//example 1:
interface MyInterface<T> {
    someMethod: (param1: T) => MyInterface<T>;
}

class MyInterfaceImpl<T> implements MyInterface<T>{
    someMethod = function (param1: T) {
        return new MyInterfaceImpl<T>();
    }
}

let getMyInterface: () => MyInterface<{}> = () => { return new MyInterfaceImpl<{}>(); };

let getString: () => MyInterface<string> = getMyInterface;
let getDate: () => MyInterface<Date> = getMyInterface;
let getNumber: () => MyInterface<number> = getMyInterface;


//example 2:
let getPromise1: () => Promise<{} | number> = () => { return Promise.resolve({}) };

let getStringP1: () => Promise<string> = getPromise1
let getDateP1: () => Promise<Date> = getPromise1
let getNumberP1: () => Promise<number> = getPromise1


//example 3:
let getPromise2: () => Promise<{} | number| undefined | null> = () => { return Promise.resolve({}) };

let getStringP2: () => Promise<string> = getPromise2
let getDateP2: () => Promise<Date> = getPromise2
let getNumberP2: () => Promise<number> = getPromise2

Expected behavior:
Should not compile with error like 'Type () =>Promise<X> is not assignable to type '() => Promise<Y>'.
Actual behavior:
Compiles.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions