@@ -211,6 +211,7 @@ namespace ts {
211
211
return tryFindAmbientModule(moduleName, /*withAugmentations*/ false);
212
212
},
213
213
getApparentType,
214
+ getAllPossiblePropertiesOfType,
214
215
getSuggestionForNonexistentProperty,
215
216
getSuggestionForNonexistentSymbol,
216
217
getBaseConstraintOfType,
@@ -690,10 +691,6 @@ namespace ts {
690
691
return type.flags & TypeFlags.Object ? (<ObjectType>type).objectFlags : 0;
691
692
}
692
693
693
- function getCheckFlags(symbol: Symbol): CheckFlags {
694
- return symbol.flags & SymbolFlags.Transient ? (<TransientSymbol>symbol).checkFlags : 0;
695
- }
696
-
697
694
function isGlobalSourceFile(node: Node) {
698
695
return node.kind === SyntaxKind.SourceFile && !isExternalOrCommonJsModule(<SourceFile>node);
699
696
}
@@ -743,7 +740,8 @@ namespace ts {
743
740
const useFile = getSourceFileOfNode(usage);
744
741
if (declarationFile !== useFile) {
745
742
if ((modulekind && (declarationFile.externalModuleIndicator || useFile.externalModuleIndicator)) ||
746
- (!compilerOptions.outFile && !compilerOptions.out)) {
743
+ (!compilerOptions.outFile && !compilerOptions.out) ||
744
+ isInAmbientContext(declaration)) {
747
745
// nodes are in different files and order cannot be determined
748
746
return true;
749
747
}
@@ -1288,7 +1286,7 @@ namespace ts {
1288
1286
else if (result.flags & SymbolFlags.Class) {
1289
1287
error(errorLocation, Diagnostics.Class_0_used_before_its_declaration, declarationNameToString(getNameOfDeclaration(declaration)));
1290
1288
}
1291
- else if (result.flags & SymbolFlags.Enum ) {
1289
+ else if (result.flags & SymbolFlags.RegularEnum ) {
1292
1290
error(errorLocation, Diagnostics.Enum_0_used_before_its_declaration, declarationNameToString(getNameOfDeclaration(declaration)));
1293
1291
}
1294
1292
}
@@ -4163,6 +4161,7 @@ namespace ts {
4163
4161
const types: Type[] = [];
4164
4162
let definedInConstructor = false;
4165
4163
let definedInMethod = false;
4164
+ let jsDocType: Type;
4166
4165
for (const declaration of symbol.declarations) {
4167
4166
const expression = declaration.kind === SyntaxKind.BinaryExpression ? <BinaryExpression>declaration :
4168
4167
declaration.kind === SyntaxKind.PropertyAccessExpression ? <BinaryExpression>getAncestor(declaration, SyntaxKind.BinaryExpression) :
@@ -4181,19 +4180,26 @@ namespace ts {
4181
4180
}
4182
4181
}
4183
4182
4184
- if (expression.flags & NodeFlags.JavaScriptFile) {
4185
- // If there is a JSDoc type, use it
4186
- const type = getTypeForDeclarationFromJSDocComment(expression.parent);
4187
- if (type && type !== unknownType) {
4188
- types.push(getWidenedType(type));
4189
- continue;
4183
+ // If there is a JSDoc type, use it
4184
+ const type = getTypeForDeclarationFromJSDocComment(expression.parent);
4185
+ if (type) {
4186
+ const declarationType = getWidenedType(type);
4187
+ if (!jsDocType) {
4188
+ jsDocType = declarationType;
4189
+ }
4190
+ else if (jsDocType !== unknownType && declarationType !== unknownType && !isTypeIdenticalTo(jsDocType, declarationType)) {
4191
+ const name = getNameOfDeclaration(declaration);
4192
+ error(name, Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2, declarationNameToString(name), typeToString(jsDocType), typeToString(declarationType));
4190
4193
}
4191
4194
}
4192
-
4193
- types.push(getWidenedLiteralType(checkExpressionCached(expression.right)));
4195
+ else if (!jsDocType) {
4196
+ // If we don't have an explicit JSDoc type, get the type from the expression.
4197
+ types.push(getWidenedLiteralType(checkExpressionCached(expression.right)));
4198
+ }
4194
4199
}
4195
4200
4196
- return getWidenedType(addOptionality(getUnionType(types, /*subtypeReduction*/ true), definedInMethod && !definedInConstructor));
4201
+ const type = jsDocType || getUnionType(types, /*subtypeReduction*/ true);
4202
+ return getWidenedType(addOptionality(type, definedInMethod && !definedInConstructor));
4197
4203
}
4198
4204
4199
4205
// Return the type implied by a binding pattern element. This is the type of the initializer of the element if
@@ -5692,6 +5698,22 @@ namespace ts {
5692
5698
getPropertiesOfObjectType(type);
5693
5699
}
5694
5700
5701
+ function getAllPossiblePropertiesOfType(type: Type): Symbol[] {
5702
+ if (type.flags & TypeFlags.Union) {
5703
+ const props = createMap<Symbol>();
5704
+ for (const memberType of (type as UnionType).types) {
5705
+ for (const { name } of getPropertiesOfType(memberType)) {
5706
+ if (!props.has(name)) {
5707
+ props.set(name, createUnionOrIntersectionProperty(type as UnionType, name));
5708
+ }
5709
+ }
5710
+ }
5711
+ return arrayFrom(props.values());
5712
+ } else {
5713
+ return getPropertiesOfType(type);
5714
+ }
5715
+ }
5716
+
5695
5717
function getConstraintOfType(type: TypeVariable | UnionOrIntersectionType): Type {
5696
5718
return type.flags & TypeFlags.TypeParameter ? getConstraintOfTypeParameter(<TypeParameter>type) :
5697
5719
type.flags & TypeFlags.IndexedAccess ? getConstraintOfIndexedAccess(<IndexedAccessType>type) :
@@ -12760,7 +12782,7 @@ namespace ts {
12760
12782
* @param node the expression whose contextual type will be returned.
12761
12783
* @returns the contextual type of an expression.
12762
12784
*/
12763
- function getContextualType(node: Expression): Type {
12785
+ function getContextualType(node: Expression): Type | undefined {
12764
12786
if (isInsideWithStatementBody(node)) {
12765
12787
// We cannot answer semantic questions within a with block, do not proceed any further
12766
12788
return undefined;
@@ -13234,7 +13256,7 @@ namespace ts {
13234
13256
}
13235
13257
return result;
13236
13258
}
13237
- }
13259
+ }
13238
13260
13239
13261
function isValidSpreadType(type: Type): boolean {
13240
13262
return !!(type.flags & (TypeFlags.Any | TypeFlags.Null | TypeFlags.Undefined | TypeFlags.NonPrimitive) ||
@@ -13554,6 +13576,20 @@ namespace ts {
13554
13576
return _jsxElementChildrenPropertyName;
13555
13577
}
13556
13578
13579
+ function getApparentTypeOfJsxPropsType(propsType: Type): Type {
13580
+ if (!propsType) {
13581
+ return undefined;
13582
+ }
13583
+ if (propsType.flags & TypeFlags.Intersection) {
13584
+ const propsApparentType: Type[] = [];
13585
+ for (const t of (<UnionOrIntersectionType>propsType).types) {
13586
+ propsApparentType.push(getApparentType(t));
13587
+ }
13588
+ return getIntersectionType(propsApparentType);
13589
+ }
13590
+ return getApparentType(propsType);
13591
+ }
13592
+
13557
13593
/**
13558
13594
* Get JSX attributes type by trying to resolve openingLikeElement as a stateless function component.
13559
13595
* Return only attributes type of successfully resolved call signature.
@@ -13574,6 +13610,7 @@ namespace ts {
13574
13610
if (callSignature !== unknownSignature) {
13575
13611
const callReturnType = callSignature && getReturnTypeOfSignature(callSignature);
13576
13612
let paramType = callReturnType && (callSignature.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(callSignature.parameters[0]));
13613
+ paramType = getApparentTypeOfJsxPropsType(paramType);
13577
13614
if (callReturnType && isTypeAssignableTo(callReturnType, jsxStatelessElementType)) {
13578
13615
// Intersect in JSX.IntrinsicAttributes if it exists
13579
13616
const intrinsicAttributes = getJsxType(JsxNames.IntrinsicAttributes);
@@ -13611,7 +13648,8 @@ namespace ts {
13611
13648
let allMatchingAttributesType: Type;
13612
13649
for (const candidate of candidatesOutArray) {
13613
13650
const callReturnType = getReturnTypeOfSignature(candidate);
13614
- const paramType = callReturnType && (candidate.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(candidate.parameters[0]));
13651
+ let paramType = callReturnType && (candidate.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(candidate.parameters[0]));
13652
+ paramType = getApparentTypeOfJsxPropsType(paramType);
13615
13653
if (callReturnType && isTypeAssignableTo(callReturnType, jsxStatelessElementType)) {
13616
13654
let shouldBeCandidate = true;
13617
13655
for (const attribute of openingLikeElement.attributes.properties) {
@@ -14011,25 +14049,6 @@ namespace ts {
14011
14049
return s.valueDeclaration ? s.valueDeclaration.kind : SyntaxKind.PropertyDeclaration;
14012
14050
}
14013
14051
14014
- function getDeclarationModifierFlagsFromSymbol(s: Symbol): ModifierFlags {
14015
- if (s.valueDeclaration) {
14016
- const flags = getCombinedModifierFlags(s.valueDeclaration);
14017
- return s.parent && s.parent.flags & SymbolFlags.Class ? flags : flags & ~ModifierFlags.AccessibilityModifier;
14018
- }
14019
- if (getCheckFlags(s) & CheckFlags.Synthetic) {
14020
- const checkFlags = (<TransientSymbol>s).checkFlags;
14021
- const accessModifier = checkFlags & CheckFlags.ContainsPrivate ? ModifierFlags.Private :
14022
- checkFlags & CheckFlags.ContainsPublic ? ModifierFlags.Public :
14023
- ModifierFlags.Protected;
14024
- const staticModifier = checkFlags & CheckFlags.ContainsStatic ? ModifierFlags.Static : 0;
14025
- return accessModifier | staticModifier;
14026
- }
14027
- if (s.flags & SymbolFlags.Prototype) {
14028
- return ModifierFlags.Public | ModifierFlags.Static;
14029
- }
14030
- return 0;
14031
- }
14032
-
14033
14052
function getDeclarationNodeFlagsFromSymbol(s: Symbol): NodeFlags {
14034
14053
return s.valueDeclaration ? getCombinedNodeFlags(s.valueDeclaration) : 0;
14035
14054
}
0 commit comments