mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Don't crash on property access with type (#25170)
* Don't crash on property access with type * Move test
This commit is contained in:
@@ -4650,15 +4650,15 @@ namespace ts {
|
||||
let jsDocType: Type | undefined;
|
||||
for (const declaration of symbol.declarations) {
|
||||
let declarationInConstructor = false;
|
||||
const expression = declaration.kind === SyntaxKind.BinaryExpression ? <BinaryExpression>declaration :
|
||||
declaration.kind === SyntaxKind.PropertyAccessExpression ? cast(declaration.parent, isBinaryExpression) :
|
||||
const expression = isBinaryExpression(declaration) ? declaration :
|
||||
isPropertyAccessExpression(declaration) ? isBinaryExpression(declaration.parent) ? declaration.parent : declaration :
|
||||
undefined;
|
||||
|
||||
if (!expression) {
|
||||
return errorType;
|
||||
}
|
||||
|
||||
const special = getSpecialPropertyAssignmentKind(expression);
|
||||
const special = isPropertyAccessExpression(expression) ? getSpecialPropertyAccessKind(expression) : getSpecialPropertyAssignmentKind(expression);
|
||||
if (special === SpecialPropertyAssignmentKind.ThisProperty) {
|
||||
const thisContainer = getThisContainer(expression, /*includeArrowFunctions*/ false);
|
||||
// Properties defined in a constructor (or base constructor, or javascript constructor function) don't get undefined added.
|
||||
@@ -4687,7 +4687,7 @@ namespace ts {
|
||||
errorNextVariableOrPropertyDeclarationMustHaveSameType(jsDocType, declaration, declarationType);
|
||||
}
|
||||
}
|
||||
else if (!jsDocType) {
|
||||
else if (!jsDocType && isBinaryExpression(expression)) {
|
||||
// If we don't have an explicit JSDoc type, get the type from the expression.
|
||||
let type = getWidenedLiteralType(checkExpressionCached(expression.right));
|
||||
|
||||
|
||||
@@ -1887,6 +1887,14 @@ namespace ts {
|
||||
return SpecialPropertyAssignmentKind.None;
|
||||
}
|
||||
const lhs = expr.left;
|
||||
if (isEntityNameExpression(lhs.expression) && lhs.name.escapedText === "prototype" && isObjectLiteralExpression(getInitializerOfBinaryExpression(expr))) {
|
||||
// F.prototype = { ... }
|
||||
return SpecialPropertyAssignmentKind.Prototype;
|
||||
}
|
||||
return getSpecialPropertyAccessKind(lhs);
|
||||
}
|
||||
|
||||
export function getSpecialPropertyAccessKind(lhs: PropertyAccessExpression): SpecialPropertyAssignmentKind {
|
||||
if (lhs.expression.kind === SyntaxKind.ThisKeyword) {
|
||||
return SpecialPropertyAssignmentKind.ThisProperty;
|
||||
}
|
||||
@@ -1895,11 +1903,7 @@ namespace ts {
|
||||
return SpecialPropertyAssignmentKind.ModuleExports;
|
||||
}
|
||||
else if (isEntityNameExpression(lhs.expression)) {
|
||||
if (lhs.name.escapedText === "prototype" && isObjectLiteralExpression(getInitializerOfBinaryExpression(expr))) {
|
||||
// F.prototype = { ... }
|
||||
return SpecialPropertyAssignmentKind.Prototype;
|
||||
}
|
||||
else if (isPrototypeAccess(lhs.expression)) {
|
||||
if (isPrototypeAccess(lhs.expression)) {
|
||||
// F.G....prototype.x = expr
|
||||
return SpecialPropertyAssignmentKind.PrototypeProperty;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user