@@ -112,27 +112,23 @@ namespace ts.codefix {
112
112
113
113
export function createMethodFromCallExpression (
114
114
context : CodeFixContextBase ,
115
- { typeArguments , arguments : args , parent : parent } : CallExpression ,
115
+ call : CallExpression ,
116
116
methodName : string ,
117
117
inJs : boolean ,
118
118
makeStatic : boolean ,
119
119
preferences : UserPreferences ,
120
120
body : boolean ,
121
121
) : MethodDeclaration {
122
+ const { typeArguments, arguments : args , parent } = call ;
122
123
const checker = context . program . getTypeChecker ( ) ;
123
- const types = map ( args ,
124
- arg => {
125
- let type = checker . getTypeAtLocation ( arg ) ;
126
- if ( type === undefined ) {
127
- return undefined ;
128
- }
129
- // Widen the type so we don't emit nonsense annotations like "function fn(x: 3) {"
130
- type = checker . getBaseTypeOfLiteralType ( type ) ;
131
- return checker . typeToTypeNode ( type ) ;
132
- } ) ;
124
+ const types = map ( args , arg =>
125
+ // Widen the type so we don't emit nonsense annotations like "function fn(x: 3) {"
126
+ checker . typeToTypeNode ( checker . getBaseTypeOfLiteralType ( checker . getTypeAtLocation ( arg ) ) ) ) ;
133
127
const names = map ( args , arg =>
134
128
isIdentifier ( arg ) ? arg . text :
135
- isPropertyAccessExpression ( arg ) ? arg . name . text : undefined ) ;
129
+ isPropertyAccessExpression ( arg ) ? arg . name . text : undefined ) ;
130
+ const contextualType = checker . getContextualType ( call ) ;
131
+ const returnType = inJs ? undefined : contextualType && checker . typeToTypeNode ( contextualType , call ) || createKeywordTypeNode ( SyntaxKind . AnyKeyword ) ;
136
132
return createMethod (
137
133
/*decorators*/ undefined ,
138
134
/*modifiers*/ makeStatic ? [ createToken ( SyntaxKind . StaticKeyword ) ] : undefined ,
@@ -142,7 +138,7 @@ namespace ts.codefix {
142
138
/*typeParameters*/ inJs ? undefined : map ( typeArguments , ( _ , i ) =>
143
139
createTypeParameterDeclaration ( CharacterCodes . T + typeArguments ! . length - 1 <= CharacterCodes . Z ? String . fromCharCode ( CharacterCodes . T + i ) : `T${ i } ` ) ) ,
144
140
/*parameters*/ createDummyParameters ( args . length , names , types , /*minArgumentCount*/ undefined , inJs ) ,
145
- /*type*/ inJs ? undefined : createKeywordTypeNode ( SyntaxKind . AnyKeyword ) ,
141
+ /*type*/ returnType ,
146
142
body ? createStubbedMethodBody ( preferences ) : undefined ) ;
147
143
}
148
144
0 commit comments