@@ -72,11 +72,18 @@ namespace ts.codefix {
72
72
} ) ,
73
73
} ) ;
74
74
75
+ function createObjectTypeFromLabeledExpression ( checker : TypeChecker , label : Identifier , expression : Expression ) {
76
+ const member = checker . createSymbol ( SymbolFlags . Property , label . escapedText ) ;
77
+ member . type = checker . getTypeAtLocation ( expression ) ;
78
+ const members = createSymbolTable ( [ member ] ) ;
79
+ return checker . createAnonymousType ( /*symbol*/ undefined , members , [ ] , [ ] , /*stringIndexInfo*/ undefined , /*numberIndexInfo*/ undefined ) ;
80
+ }
81
+
75
82
function getFixInfo ( checker : TypeChecker , declaration : FunctionLikeDeclaration , expectType : Type , isFunctionType : boolean ) : Info | undefined {
76
83
if ( ! declaration . body || ! isBlock ( declaration . body ) || length ( declaration . body . statements ) !== 1 ) return undefined ;
77
84
78
85
const firstStatement = first ( declaration . body . statements ) ;
79
- if ( isExpressionStatement ( firstStatement ) && checkFixedAssignableTo ( checker , declaration , firstStatement . expression , expectType , isFunctionType ) ) {
86
+ if ( isExpressionStatement ( firstStatement ) && checkFixedAssignableTo ( checker , declaration , checker . getTypeAtLocation ( firstStatement . expression ) , expectType , isFunctionType ) ) {
80
87
return {
81
88
declaration,
82
89
kind : ProblemKind . MissingReturnStatement ,
@@ -87,7 +94,8 @@ namespace ts.codefix {
87
94
}
88
95
else if ( isLabeledStatement ( firstStatement ) && isExpressionStatement ( firstStatement . statement ) ) {
89
96
const node = createObjectLiteral ( [ createPropertyAssignment ( firstStatement . label , firstStatement . statement . expression ) ] ) ;
90
- if ( checkFixedAssignableTo ( checker , declaration , node , expectType , isFunctionType ) ) {
97
+ const nodeType = createObjectTypeFromLabeledExpression ( checker , firstStatement . label , firstStatement . statement . expression ) ;
98
+ if ( checkFixedAssignableTo ( checker , declaration , nodeType , expectType , isFunctionType ) ) {
91
99
return isArrowFunction ( declaration ) ? {
92
100
declaration,
93
101
kind : ProblemKind . MissingParentheses ,
@@ -107,7 +115,8 @@ namespace ts.codefix {
107
115
const firstBlockStatement = first ( firstStatement . statements ) ;
108
116
if ( isLabeledStatement ( firstBlockStatement ) && isExpressionStatement ( firstBlockStatement . statement ) ) {
109
117
const node = createObjectLiteral ( [ createPropertyAssignment ( firstBlockStatement . label , firstBlockStatement . statement . expression ) ] ) ;
110
- if ( checkFixedAssignableTo ( checker , declaration , node , expectType , isFunctionType ) ) {
118
+ const nodeType = createObjectTypeFromLabeledExpression ( checker , firstBlockStatement . label , firstBlockStatement . statement . expression ) ;
119
+ if ( checkFixedAssignableTo ( checker , declaration , nodeType , expectType , isFunctionType ) ) {
111
120
return {
112
121
declaration,
113
122
kind : ProblemKind . MissingReturnStatement ,
@@ -122,8 +131,35 @@ namespace ts.codefix {
122
131
return undefined ;
123
132
}
124
133
125
- function checkFixedAssignableTo ( checker : TypeChecker , declaration : FunctionLikeDeclaration , expr : Expression , type : Type , isFunctionType : boolean ) {
126
- return checker . isTypeAssignableTo ( checker . getTypeAtLocation ( isFunctionType ? updateFunctionLikeBody ( declaration , createBlock ( [ createReturn ( expr ) ] ) ) : expr ) , type ) ;
134
+ function checkFixedAssignableTo ( checker : TypeChecker , declaration : FunctionLikeDeclaration , exprType : Type , type : Type , isFunctionType : boolean ) {
135
+ if ( isFunctionType ) {
136
+ const sig = checker . getSignatureFromDeclaration ( declaration ) ;
137
+ if ( sig ) {
138
+ if ( hasModifier ( declaration , ModifierFlags . Async ) ) {
139
+ exprType = checker . createPromiseType ( exprType ) ;
140
+ }
141
+ const newSig = checker . createSignature (
142
+ declaration ,
143
+ sig . typeParameters ,
144
+ sig . thisParameter ,
145
+ sig . parameters ,
146
+ exprType ,
147
+ /*typePredicate*/ undefined ,
148
+ sig . minArgumentCount ,
149
+ sig . flags ) ;
150
+ exprType = checker . createAnonymousType (
151
+ /*symbol*/ undefined ,
152
+ createSymbolTable ( ) ,
153
+ [ newSig ] ,
154
+ [ ] ,
155
+ /*stringIndexInfo*/ undefined ,
156
+ /*numberIndexInfo*/ undefined ) ;
157
+ }
158
+ else {
159
+ exprType = checker . getAnyType ( ) ;
160
+ }
161
+ }
162
+ return checker . isTypeAssignableTo ( exprType , type ) ;
127
163
}
128
164
129
165
function getInfo ( checker : TypeChecker , sourceFile : SourceFile , position : number , errorCode : number ) : Info | undefined {
0 commit comments