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
{{ message }}
Repository navigation
Context typing doesn't work for object literals #3570
In a contextually typed object literal, each property value expression is contextually typed by
the type of the property with a matching name in the contextual type, if any, or otherwise
for a numerically named property, the numeric index type of the contextual type, if any, or otherwise
the string index type of the contextual type, if any.
Notice that it doesn't mention method declarations, so the following actually will work:
vara: {do1():(s:string)=>void}a={do1: ()=>{returns=>{s.//actually is the 'string' type}}}
The question is whether or not it should be. While methods are declarations, not expressions, this behavior leads to a pretty inconsistent experience. Anders Hejlsberg (@ahejlsberg) would be better suited to answer this.
Yes - actually I just noticed the following from the same section of the spec:
In the body of a function declaration, function expression, arrow function, method declaration, or get accessor declaration that has a return type annotation, return expressions are contextually typed by the type given in the return type annotation.
var a: {
do1(s:string) => void // <-- context typing works for the parameter s
}
var a:() => (s:string)=>void // <-- context typing works for the return type
var a: {
do1():() => (s:string)=>void // <-- context typing for the return type doesn't work
}
Activity
DanielRosenwasser commentedon Jun 19, 2015
Strangely enough, this might be per spec:
Notice that it doesn't mention method declarations, so the following actually will work:
The question is whether or not it should be. While methods are declarations, not expressions, this behavior leads to a pretty inconsistent experience. Anders Hejlsberg (@ahejlsberg) would be better suited to answer this.
anstarovoyt commentedon Jun 19, 2015
Daniel Rosenwasser (@DanielRosenwasser) anyway this example works fine:
so context typing works for parameter types and doesn't work for return type.
DanielRosenwasser commentedon Jun 19, 2015
Yes - actually I just noticed the following from the same section of the spec:
So this is still per spec, but for a different reason. I'd still like to get Anders Hejlsberg (@ahejlsberg)'s take.
anstarovoyt commentedon Jun 19, 2015
Daniel Rosenwasser (@DanielRosenwasser) I guess the main point is ' that has a return type annotation' in the part of spec.
Notice: for me it is just not consistently:
ahejlsberg commentedon Jun 19, 2015
Agreed, this is inconsistent. We already have it captured in #1610.
anstarovoyt commentedon Jun 19, 2015
Daniel Rosenwasser (@DanielRosenwasser) Anders Hejlsberg (@ahejlsberg) thank you!