Skip to content

Commit

Permalink
Fixed a crash when inferring return type of an accessor with errors i…
Browse files Browse the repository at this point in the history
…n its return statement (#56258)
  • Loading branch information
Andarist authored and DanielRosenwasser committed Nov 16, 2023
1 parent af8590b commit 9bb38b7
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7312,7 +7312,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

if (propertySymbol.flags & SymbolFlags.Accessor) {
const writeType = getWriteTypeOfSymbol(propertySymbol);
if (propertyType !== writeType) {
if (propertyType !== writeType && !isErrorType(propertyType) && !isErrorType(writeType)) {
const getterDeclaration = getDeclarationOfKind<GetAccessorDeclaration>(propertySymbol, SyntaxKind.GetAccessor)!;
const getterSignature = getSignatureFromDeclaration(getterDeclaration);
typeElements.push(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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.
accessorInferredReturnTypeErrorInReturnStatement.ts(4,18): error TS2339: Property 'collection' does not exist on type '{ readonly primaryPath: any; }'.


==== accessorInferredReturnTypeErrorInReturnStatement.ts (2 errors) ====
export var basePrototype = {
get primaryPath() {
~~~~~~~~~~~
!!! 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.
var _this = this;
return _this.collection.schema.primaryPath;
~~~~~~~~~~
!!! error TS2339: Property 'collection' does not exist on type '{ readonly primaryPath: any; }'.
},
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [tests/cases/compiler/accessorInferredReturnTypeErrorInReturnStatement.ts] ////

//// [accessorInferredReturnTypeErrorInReturnStatement.ts]
export var basePrototype = {
get primaryPath() {
var _this = this;
return _this.collection.schema.primaryPath;
},
};


//// [accessorInferredReturnTypeErrorInReturnStatement.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.basePrototype = void 0;
exports.basePrototype = {
get primaryPath() {
var _this = this;
return _this.collection.schema.primaryPath;
},
};


//// [accessorInferredReturnTypeErrorInReturnStatement.d.ts]
export declare var basePrototype: {
readonly primaryPath: any;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [tests/cases/compiler/accessorInferredReturnTypeErrorInReturnStatement.ts] ////

=== accessorInferredReturnTypeErrorInReturnStatement.ts ===
export var basePrototype = {
>basePrototype : Symbol(basePrototype, Decl(accessorInferredReturnTypeErrorInReturnStatement.ts, 0, 10))

get primaryPath() {
>primaryPath : Symbol(primaryPath, Decl(accessorInferredReturnTypeErrorInReturnStatement.ts, 0, 28))

var _this = this;
>_this : Symbol(_this, Decl(accessorInferredReturnTypeErrorInReturnStatement.ts, 2, 7))
>this : Symbol(basePrototype, Decl(accessorInferredReturnTypeErrorInReturnStatement.ts, 0, 26))

return _this.collection.schema.primaryPath;
>_this : Symbol(_this, Decl(accessorInferredReturnTypeErrorInReturnStatement.ts, 2, 7))

},
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [tests/cases/compiler/accessorInferredReturnTypeErrorInReturnStatement.ts] ////

=== accessorInferredReturnTypeErrorInReturnStatement.ts ===
export var basePrototype = {
>basePrototype : { readonly primaryPath: any; }
>{ get primaryPath() { var _this = this; return _this.collection.schema.primaryPath; }, } : { readonly primaryPath: any; }

get primaryPath() {
>primaryPath : any

var _this = this;
>_this : { readonly primaryPath: any; }
>this : { readonly primaryPath: any; }

return _this.collection.schema.primaryPath;
>_this.collection.schema.primaryPath : any
>_this.collection.schema : any
>_this.collection : any
>_this : { readonly primaryPath: any; }
>collection : any
>schema : any
>primaryPath : any

},
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @strict: true
// @declaration: true

export var basePrototype = {
get primaryPath() {
var _this = this;
return _this.collection.schema.primaryPath;
},
};

0 comments on commit 9bb38b7

Please sign in to comment.