Closed
Description
The compiler gives an error on the last line:
function s(p: {[k:string]:string}) {
console.log(p);
}
let t: {[k:string]:string} = {thing: 'otherthing'};
s(t);
let t2 = {thing: 'otherthing'};
s(t2);
Index signature is missing in type '{thing: 'otherthing'}'
Which is very sad since I'm refactoring all of angular to use string index signatures instead of our hacky StringMap (angular/angular#4483)
Activity
danquirk commentedon Oct 3, 2015
The reason for this rule was because we could not guarantee that
t2
did not get additional properties after initialization that would make it invalid for use withs
. For example:The rule is that object literals that are contextually typed by a type with an indexer signature then get the index signature pushed into the literals type (this is what happens to
t
). However, now the code I posted above is an error under the new object literal strictness checks. But the strictness rules only extend so far:alexeagle commentedon Oct 3, 2015
Okay, thanks for explaining. I'm almost done adding explicit types.
What about this case, where I get an error, it seems like the type narrowing within the type guard should work?
RyanCavanaugh commentedon Oct 3, 2015
We don't yet narrow the
else
clause on account ofinstanceof
; see #1719alexeagle commentedon Oct 3, 2015
thanks, world-class support! 💐