Skip to content

Commit 9bb38b7

Browse files
AndaristDanielRosenwasser
authored andcommitted
Fixed a crash when inferring return type of an accessor with errors in its return statement (#56258)
1 parent af8590b commit 9bb38b7

6 files changed

+98
-1
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7312,7 +7312,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
73127312

73137313
if (propertySymbol.flags & SymbolFlags.Accessor) {
73147314
const writeType = getWriteTypeOfSymbol(propertySymbol);
7315-
if (propertyType !== writeType) {
7315+
if (propertyType !== writeType && !isErrorType(propertyType) && !isErrorType(writeType)) {
73167316
const getterDeclaration = getDeclarationOfKind<GetAccessorDeclaration>(propertySymbol, SyntaxKind.GetAccessor)!;
73177317
const getterSignature = getSignatureFromDeclaration(getterDeclaration);
73187318
typeElements.push(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
accessorInferredReturnTypeErrorInReturnStatement.ts(2,7): error TS7023: 'primaryPath' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
2+
accessorInferredReturnTypeErrorInReturnStatement.ts(4,18): error TS2339: Property 'collection' does not exist on type '{ readonly primaryPath: any; }'.
3+
4+
5+
==== accessorInferredReturnTypeErrorInReturnStatement.ts (2 errors) ====
6+
export var basePrototype = {
7+
get primaryPath() {
8+
~~~~~~~~~~~
9+
!!! error TS7023: 'primaryPath' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
10+
var _this = this;
11+
return _this.collection.schema.primaryPath;
12+
~~~~~~~~~~
13+
!!! error TS2339: Property 'collection' does not exist on type '{ readonly primaryPath: any; }'.
14+
},
15+
};
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/accessorInferredReturnTypeErrorInReturnStatement.ts] ////
2+
3+
//// [accessorInferredReturnTypeErrorInReturnStatement.ts]
4+
export var basePrototype = {
5+
get primaryPath() {
6+
var _this = this;
7+
return _this.collection.schema.primaryPath;
8+
},
9+
};
10+
11+
12+
//// [accessorInferredReturnTypeErrorInReturnStatement.js]
13+
"use strict";
14+
Object.defineProperty(exports, "__esModule", { value: true });
15+
exports.basePrototype = void 0;
16+
exports.basePrototype = {
17+
get primaryPath() {
18+
var _this = this;
19+
return _this.collection.schema.primaryPath;
20+
},
21+
};
22+
23+
24+
//// [accessorInferredReturnTypeErrorInReturnStatement.d.ts]
25+
export declare var basePrototype: {
26+
readonly primaryPath: any;
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [tests/cases/compiler/accessorInferredReturnTypeErrorInReturnStatement.ts] ////
2+
3+
=== accessorInferredReturnTypeErrorInReturnStatement.ts ===
4+
export var basePrototype = {
5+
>basePrototype : Symbol(basePrototype, Decl(accessorInferredReturnTypeErrorInReturnStatement.ts, 0, 10))
6+
7+
get primaryPath() {
8+
>primaryPath : Symbol(primaryPath, Decl(accessorInferredReturnTypeErrorInReturnStatement.ts, 0, 28))
9+
10+
var _this = this;
11+
>_this : Symbol(_this, Decl(accessorInferredReturnTypeErrorInReturnStatement.ts, 2, 7))
12+
>this : Symbol(basePrototype, Decl(accessorInferredReturnTypeErrorInReturnStatement.ts, 0, 26))
13+
14+
return _this.collection.schema.primaryPath;
15+
>_this : Symbol(_this, Decl(accessorInferredReturnTypeErrorInReturnStatement.ts, 2, 7))
16+
17+
},
18+
};
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//// [tests/cases/compiler/accessorInferredReturnTypeErrorInReturnStatement.ts] ////
2+
3+
=== accessorInferredReturnTypeErrorInReturnStatement.ts ===
4+
export var basePrototype = {
5+
>basePrototype : { readonly primaryPath: any; }
6+
>{ get primaryPath() { var _this = this; return _this.collection.schema.primaryPath; }, } : { readonly primaryPath: any; }
7+
8+
get primaryPath() {
9+
>primaryPath : any
10+
11+
var _this = this;
12+
>_this : { readonly primaryPath: any; }
13+
>this : { readonly primaryPath: any; }
14+
15+
return _this.collection.schema.primaryPath;
16+
>_this.collection.schema.primaryPath : any
17+
>_this.collection.schema : any
18+
>_this.collection : any
19+
>_this : { readonly primaryPath: any; }
20+
>collection : any
21+
>schema : any
22+
>primaryPath : any
23+
24+
},
25+
};
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @strict: true
2+
// @declaration: true
3+
4+
export var basePrototype = {
5+
get primaryPath() {
6+
var _this = this;
7+
return _this.collection.schema.primaryPath;
8+
},
9+
};

0 commit comments

Comments
 (0)