mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 04:43:37 -05:00
Merge pull request #13903 from Microsoft/jsPropertyWidening
Widen special JS property declarations to match regular property declarations
This commit is contained in:
@@ -3259,7 +3259,7 @@ namespace ts {
|
||||
type;
|
||||
}
|
||||
|
||||
function getTypeForVariableLikeDeclarationFromJSDocComment(declaration: VariableLikeDeclaration) {
|
||||
function getTypeForDeclarationFromJSDocComment(declaration: Node ) {
|
||||
const jsdocType = getJSDocType(declaration);
|
||||
if (jsdocType) {
|
||||
return getTypeFromTypeNode(jsdocType);
|
||||
@@ -3287,7 +3287,7 @@ namespace ts {
|
||||
// If this is a variable in a JavaScript file, then use the JSDoc type (if it has
|
||||
// one as its type), otherwise fallback to the below standard TS codepaths to
|
||||
// try to figure it out.
|
||||
const type = getTypeForVariableLikeDeclarationFromJSDocComment(declaration);
|
||||
const type = getTypeForDeclarationFromJSDocComment(declaration);
|
||||
if (type && type !== unknownType) {
|
||||
return type;
|
||||
}
|
||||
@@ -3382,6 +3382,27 @@ namespace ts {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Return the inferred type for a variable, parameter, or property declaration
|
||||
function getTypeForJSSpecialPropertyDeclaration(declaration: Declaration): Type {
|
||||
const expression = declaration.kind === SyntaxKind.BinaryExpression ? <BinaryExpression>declaration :
|
||||
declaration.kind === SyntaxKind.PropertyAccessExpression ? <BinaryExpression>getAncestor(declaration, SyntaxKind.BinaryExpression) :
|
||||
undefined;
|
||||
|
||||
if (!expression) {
|
||||
return unknownType;
|
||||
}
|
||||
|
||||
if (expression.flags & NodeFlags.JavaScriptFile) {
|
||||
// If there is a JSDoc type, use it
|
||||
const type = getTypeForDeclarationFromJSDocComment(expression.parent);
|
||||
if (type && type !== unknownType) {
|
||||
return getWidenedType(type);
|
||||
}
|
||||
}
|
||||
|
||||
return getWidenedLiteralType(checkExpressionCached(expression.right));
|
||||
}
|
||||
|
||||
// Return the type implied by a binding pattern element. This is the type of the initializer of the element if
|
||||
// one is present. Otherwise, if the element is itself a binding pattern, it is the type implied by the binding
|
||||
// pattern. Otherwise, it is the type any.
|
||||
@@ -3536,18 +3557,7 @@ namespace ts {
|
||||
// * className.prototype.method = expr
|
||||
if (declaration.kind === SyntaxKind.BinaryExpression ||
|
||||
declaration.kind === SyntaxKind.PropertyAccessExpression && declaration.parent.kind === SyntaxKind.BinaryExpression) {
|
||||
// Use JS Doc type if present on parent expression statement
|
||||
if (declaration.flags & NodeFlags.JavaScriptFile) {
|
||||
const jsdocType = getJSDocType(declaration.parent);
|
||||
if (jsdocType) {
|
||||
return links.type = getTypeFromTypeNode(jsdocType);
|
||||
}
|
||||
}
|
||||
const declaredTypes = map(symbol.declarations,
|
||||
decl => decl.kind === SyntaxKind.BinaryExpression ?
|
||||
checkExpressionCached((<BinaryExpression>decl).right) :
|
||||
checkExpressionCached((<BinaryExpression>decl.parent).right));
|
||||
type = getUnionType(declaredTypes, /*subtypeReduction*/ true);
|
||||
type = getWidenedType(getUnionType(map(symbol.declarations, getTypeForJSSpecialPropertyDeclaration), /*subtypeReduction*/ true));
|
||||
}
|
||||
else {
|
||||
type = getWidenedTypeForVariableLikeDeclaration(<VariableLikeDeclaration>declaration, /*reportErrors*/ true);
|
||||
@@ -3590,7 +3600,7 @@ namespace ts {
|
||||
const setter = <AccessorDeclaration>getDeclarationOfKind(symbol, SyntaxKind.SetAccessor);
|
||||
|
||||
if (getter && getter.flags & NodeFlags.JavaScriptFile) {
|
||||
const jsDocType = getTypeForVariableLikeDeclarationFromJSDocComment(getter);
|
||||
const jsDocType = getTypeForDeclarationFromJSDocComment(getter);
|
||||
if (jsDocType) {
|
||||
return links.type = jsDocType;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user