Skip to content

Generics; ReturnType<Foo> != ReturnType<typeof foo> #24277

Open
@AnyhowStep

Description

@AnyhowStep

TypeScript Version: Version 2.9.0-dev.20180516

Search Terms: generic function returntype

I couldn't think of a good title for this,

Code

type Delegate<T> = () => T;

function executeGeneric<
    X,
    DelegateT extends Delegate<X> 
>(delegate: DelegateT): ReturnType<DelegateT> {
    //Type 'X' is not assignable to type 'ReturnType<DelegateT>'.
    const x: ReturnType<typeof delegate> = delegate();
    //Type 'X' is not assignable to type 'ReturnType<DelegateT>'.
    const y: ReturnType<DelegateT> = delegate();
    //Type 'X' is not assignable to type 'ReturnType<DelegateT>'.
    return delegate();
}

Expected behavior:

Return successfully.

Intuitively, to me,

  1. delegate is of typeDelegateT.
  2. ReturnType<DelegateT> and ReturnType<typeof delegate> should be the same

However,

Actual behavior:

Type 'X' is not assignable to type 'ReturnType<DelegateT>'.

Playground Link: Here

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions