typescript - Type inference success depends on naming a value -
i'm still new typescript , find things find odd.
take code:
interface myinterface<t> { [i: string]: t; } function myfunc<t>(arg: myinterface<t>) { } var o = { 'val1': 42, 'val2': 43 }; myfunc(o);
the call fails complaint "index signature missing".
interestingly, it's not missing when write
myfunc({ 'val1': 42, 'val2': 43 });
that mean expression { 'val1': 42, 'val2': 43 }
's type inferred backwards call (which expected), not when bound name (which weird , not how know backward inference work f#, example).
but gets stranger.
if change interface to
interface myinterface<t> { [i: number]: t; }
then corresponding
var o = [42, 43]; myfunc(o);
also works (in addition passing array directly).
is design? rules explain this?
the reason you're seeing inconsistency because you're using typescript 1.8. if try either out in typescript playground, you'll see either 1 works.
the reason behavior in ts 1.8 because when placed object literal directly in call myfunc
, contextually typed arg
, type has index signature. result, inferred type gained index signature.
contextual typing doesn't modify variable's prior inferred type (the declared type), when tried pass o
myfunc
, got error o
missing index signature.
in typescript 2.0 (and above), these scenarios painful enough brought in concept of implicit index signatures. check out pull request here: https://github.com/microsoft/typescript/pull/7029.
to quote:
with pr object literal type assignable type index signature if known properties in object literal assignable index signature. makes possible pass variable initialized object literal parameter function expects map or dictionary:
function httpservice(path: string, headers: { [x: string]: string }) { } const headers = { "content-type": "application/x-www-form-urlencoded" }; httpservice("", { "content-type": "application/x-www-form-urlencoded" }); // ok httpservice("", headers); // ok, wasn't
great
ReplyDelete