This project is read-only.

Generic resolution, strange behavior

Topics: General, Language Specification
Jan 10, 2014 at 1:26 PM
Edited Jan 10, 2014 at 1:43 PM
I was trying to create a declarations file for React, and found a strange behavior :

interface MyInterface<A>{

declare function func<C extends MyInterface<A>, A>(c: C): A

class Impl<A> implements MyInterface<A>  {
    getA():A {
        return null;

var impl = new Impl<{ anything : string}>();
var a = func(impl);
the func call result in the following error :
Type '{}' does not satisfy the constraint 'MyInterface<Impl<{ anything: string; }>>' for type parameter 'C extends MyInterface<A>'.
<A, C extends MyInterface<A>>(c: C): A
if I define the function this way :
declare function func< C extends MyInterface<A>, A>(c: C): A
No errors , but a is seen as {} by the compiler

Is there something that I did not understood, or is it a bug ?
Jan 10, 2014 at 3:26 PM
I would just define the function as:
declare function func<A>(c: MyInterface<A>): A
The compiler can make type argument inferences from regular arguments, but it cannot infer one type argument from another.
Jan 10, 2014 at 5:56 PM
Thanks for the information,
In fact what I really wanted to obtains what more something like :
declare function func< C extends MyInterface<A>, A>(c: C):  (a: A) => C
I'll try to find a workaround.