You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This snippet is a based on a problem discovered when using my library, runtypes, with TypeScript 2.6 and --strictFunctionTypes. I've stripped it down to the following minimal repro:
constFoo=Obj({foo: Num})// ^^^^^^^^^^^^// error TS2345: Argument of type '{ foo: Num; }' is not assignable to parameter of type '{ [_: string]: Runtype<any>; }'.// Property 'foo' is incompatible with index signature.// Type 'Num' is not assignable to type 'Runtype<any>'.// Types of property 'constraint' are incompatible.// Type 'Constraint<Num>' is not assignable to type 'Constraint<Runtype<any>>'.// Types of property 'underlying' are incompatible.// Type 'Num' is not assignable to type 'Runtype<any>'.interfaceRuntype<A>{constraint: Constraint<this>witness: A}interfaceNumextendsRuntype<number>{tag: 'number'}declareconstNum: NuminterfaceObj<Oextends{[_instring]: Runtype<any>}>extendsRuntype<{[KinkeyofO]: O[K]['witness']}>{}declarefunctionObj<Oextends{[_: string]: Runtype<any>}>(fields: O): Obj<O>;interfaceConstraint<AextendsRuntype<any>>extendsRuntype<A['witness']>{underlying: A,check: (x: A['witness'])=>void,}
Expected behavior:
Should type check.
Actual behavior:
It fails with the error shown in the comment.
Commentary
The error is strange; it begins and ends with this:
Type 'Num' is not assignable to type 'Runtype<any>'
But that's clearly not true, as evidenced by the fact that this works:
constwat: Runtype<any>=Num
And bizarrely, if you make that declaration at the top of the file it will make the error go away! But only if you declare wat before Foo. If you declare watafterFoo, bothFoo and wat are flagged with Type 'Num' is not assignable to type 'Runtype<any>'.
There are a number of other ways to make the error go away, none of which I understand any better:
change any of Num, Obj or Constraint to be a type instead of an interface, e.g.
typeNum=Runtype<number>&{tag: 'number'}
delete any of the fields: tag, underlying or check
change the signature of check to
check(x: A['witness']): void,
The text was updated successfully, but these errors were encountered:
This appears to be an issue with the improved error elaboration logic for invariant generic types (discussed here #18654 (comment)), which apparently isn't so improved in this case. With --strictFunctionTypes, the expected behavior in your example is an error but we're not consistently reporting it. BTW, the error looks to be caused by Runtype<A> and Constraint<A> both being invariant for A because Constraint<A> is using A in both co- and contra-variant positions.
TypeScript Version: 2.6.1
Code
This snippet is a based on a problem discovered when using my library,
runtypes
, with TypeScript 2.6 and--strictFunctionTypes
. I've stripped it down to the following minimal repro:Expected behavior:
Should type check.
Actual behavior:
It fails with the error shown in the comment.
Commentary
The error is strange; it begins and ends with this:
But that's clearly not true, as evidenced by the fact that this works:
And bizarrely, if you make that declaration at the top of the file it will make the error go away! But only if you declare
wat
beforeFoo
. If you declarewat
afterFoo
, bothFoo
andwat
are flagged withType 'Num' is not assignable to type 'Runtype<any>'.
There are a number of other ways to make the error go away, none of which I understand any better:
Num
,Obj
orConstraint
to be atype
instead of aninterface
, e.g.tag
,underlying
orcheck
check
toThe text was updated successfully, but these errors were encountered: