= T & { next: List }" cannot be reduced during its declaration.
+ // Also, unlike union types, the order of the constituent types is preserved in order that overload resolution
+ // for intersections of types with signatures can be deterministic.
+ function getIntersectionType(types, aliasSymbol, aliasTypeArguments) {
+ var typeMembershipMap = new ts.Map();
+ var includes = addTypesToIntersection(typeMembershipMap, 0, types);
+ var typeSet = ts.arrayFrom(typeMembershipMap.values());
+ // An intersection type is considered empty if it contains
+ // the type never, or
+ // more than one unit type or,
+ // an object type and a nullable type (null or undefined), or
+ // a string-like type and a type known to be non-string-like, or
+ // a number-like type and a type known to be non-number-like, or
+ // a symbol-like type and a type known to be non-symbol-like, or
+ // a void-like type and a type known to be non-void-like, or
+ // a non-primitive type and a type known to be primitive.
+ if (includes & 131072 /* Never */ ||
+ strictNullChecks && includes & 98304 /* Nullable */ && includes & (524288 /* Object */ | 67108864 /* NonPrimitive */ | 16777216 /* IncludesEmptyObject */) ||
+ includes & 67108864 /* NonPrimitive */ && includes & (469892092 /* DisjointDomains */ & ~67108864 /* NonPrimitive */) ||
+ includes & 402653316 /* StringLike */ && includes & (469892092 /* DisjointDomains */ & ~402653316 /* StringLike */) ||
+ includes & 296 /* NumberLike */ && includes & (469892092 /* DisjointDomains */ & ~296 /* NumberLike */) ||
+ includes & 2112 /* BigIntLike */ && includes & (469892092 /* DisjointDomains */ & ~2112 /* BigIntLike */) ||
+ includes & 12288 /* ESSymbolLike */ && includes & (469892092 /* DisjointDomains */ & ~12288 /* ESSymbolLike */) ||
+ includes & 49152 /* VoidLike */ && includes & (469892092 /* DisjointDomains */ & ~49152 /* VoidLike */)) {
+ return neverType;
+ }
+ if (includes & 134217728 /* TemplateLiteral */ && includes & 128 /* StringLiteral */ && extractRedundantTemplateLiterals(typeSet)) {
+ return neverType;
+ }
+ if (includes & 1 /* Any */) {
+ return includes & 8388608 /* IncludesWildcard */ ? wildcardType : anyType;
+ }
+ if (!strictNullChecks && includes & 98304 /* Nullable */) {
+ return includes & 32768 /* Undefined */ ? undefinedType : nullType;
+ }
+ if (includes & 4 /* String */ && includes & 128 /* StringLiteral */ ||
+ includes & 8 /* Number */ && includes & 256 /* NumberLiteral */ ||
+ includes & 64 /* BigInt */ && includes & 2048 /* BigIntLiteral */ ||
+ includes & 4096 /* ESSymbol */ && includes & 8192 /* UniqueESSymbol */) {
+ removeRedundantPrimitiveTypes(typeSet, includes);
+ }
+ if (includes & 16777216 /* IncludesEmptyObject */ && includes & 524288 /* Object */) {
+ ts.orderedRemoveItemAt(typeSet, ts.findIndex(typeSet, isEmptyAnonymousObjectType));
+ }
+ if (typeSet.length === 0) {
+ return unknownType;
+ }
+ if (typeSet.length === 1) {
+ return typeSet[0];
+ }
+ var id = getTypeListId(typeSet);
+ var result = intersectionTypes.get(id);
+ if (!result) {
+ if (includes & 1048576 /* Union */) {
+ if (intersectUnionsOfPrimitiveTypes(typeSet)) {
+ // When the intersection creates a reduced set (which might mean that *all* union types have
+ // disappeared), we restart the operation to get a new set of combined flags. Once we have
+ // reduced we'll never reduce again, so this occurs at most once.
+ result = getIntersectionType(typeSet, aliasSymbol, aliasTypeArguments);
}
- // Consider a fresh empty object literal type "closed" under the subtype relationship - this way `{} <- {[idx: string]: any} <- fresh({})`
- // and not `{} <- fresh({}) <- {[idx: string]: any}`
- else if ((relation === subtypeRelation || relation === strictSubtypeRelation) && isEmptyObjectType(target) && ts.getObjectFlags(target) & 32768 /* FreshLiteral */ && !isEmptyObjectType(source)) {
- return 0 /* False */;
+ else if (extractIrreducible(typeSet, 32768 /* Undefined */)) {
+ result = getUnionType([getIntersectionType(typeSet), undefinedType], 1 /* Literal */, aliasSymbol, aliasTypeArguments);
}
- // Even if relationship doesn't hold for unions, intersections, or generic type references,
- // it may hold in a structural comparison.
- // In a check of the form X = A & B, we will have previously checked if A relates to X or B relates
- // to X. Failing both of those we want to check if the aggregation of A and B's members structurally
- // relates to X. Thus, we include intersection types on the source side here.
- if (source.flags & (524288 /* Object */ | 2097152 /* Intersection */) && target.flags & 524288 /* Object */) {
- // Report structural errors only if we haven't reported any errors yet
- var reportStructuralErrors = reportErrors && errorInfo === saveErrorInfo.errorInfo && !sourceIsPrimitive;
- result = propertiesRelatedTo(source, target, reportStructuralErrors, /*excludedProperties*/ undefined, intersectionState);
- if (result) {
- result &= signaturesRelatedTo(source, target, 0 /* Call */, reportStructuralErrors);
- if (result) {
- result &= signaturesRelatedTo(source, target, 1 /* Construct */, reportStructuralErrors);
- if (result) {
- result &= indexTypesRelatedTo(source, target, 0 /* String */, sourceIsPrimitive, reportStructuralErrors, intersectionState);
- if (result) {
- result &= indexTypesRelatedTo(source, target, 1 /* Number */, sourceIsPrimitive, reportStructuralErrors, intersectionState);
- }
- }
- }
- }
- if (varianceCheckFailed && result) {
- errorInfo = originalErrorInfo || errorInfo || saveErrorInfo.errorInfo; // Use variance error (there is no structural one) and return false
- }
- else if (result) {
- return result;
- }
+ else if (extractIrreducible(typeSet, 65536 /* Null */)) {
+ result = getUnionType([getIntersectionType(typeSet), nullType], 1 /* Literal */, aliasSymbol, aliasTypeArguments);
}
- // If S is an object type and T is a discriminated union, S may be related to T if
- // there exists a constituent of T for every combination of the discriminants of S
- // with respect to T. We do not report errors here, as we will use the existing
- // error result from checking each constituent of the union.
- if (source.flags & (524288 /* Object */ | 2097152 /* Intersection */) && target.flags & 1048576 /* Union */) {
- var objectOnlyTarget = extractTypesOfKind(target, 524288 /* Object */ | 2097152 /* Intersection */ | 33554432 /* Substitution */);
- if (objectOnlyTarget.flags & 1048576 /* Union */) {
- var result_7 = typeRelatedToDiscriminatedType(source, objectOnlyTarget);
- if (result_7) {
- return result_7;
- }
+ else {
+ // We are attempting to construct a type of the form X & (A | B) & Y. Transform this into a type of
+ // the form X & A & Y | X & B & Y and recursively reduce until no union type constituents remain.
+ // If the estimated size of the resulting union type exceeds 100000 constituents, report an error.
+ if (!checkCrossProductUnion(typeSet)) {
+ return errorType;
}
+ var unionIndex_1 = ts.findIndex(typeSet, function (t) { return (t.flags & 1048576 /* Union */) !== 0; });
+ var unionType = typeSet[unionIndex_1];
+ result = getUnionType(ts.map(unionType.types, function (t) { return getIntersectionType(ts.replaceElement(typeSet, unionIndex_1, t)); }), 1 /* Literal */, aliasSymbol, aliasTypeArguments);
}
}
- return 0 /* False */;
- function relateVariances(sourceTypeArguments, targetTypeArguments, variances, intersectionState) {
- if (result = typeArgumentsRelatedTo(sourceTypeArguments, targetTypeArguments, variances, reportErrors, intersectionState)) {
- return result;
- }
- if (ts.some(variances, function (v) { return !!(v & 24 /* AllowsStructuralFallback */); })) {
- // If some type parameter was `Unmeasurable` or `Unreliable`, and we couldn't pass by assuming it was identical, then we
- // have to allow a structural fallback check
- // We elide the variance-based error elaborations, since those might not be too helpful, since we'll potentially
- // be assuming identity of the type parameter.
- originalErrorInfo = undefined;
- resetErrorInfo(saveErrorInfo);
- return undefined;
+ else {
+ result = createIntersectionType(typeSet, aliasSymbol, aliasTypeArguments);
+ }
+ intersectionTypes.set(id, result);
+ }
+ return result;
+ }
+ function checkCrossProductUnion(types) {
+ var size = ts.reduceLeft(types, function (n, t) { return n * (t.flags & 1048576 /* Union */ ? t.types.length : t.flags & 131072 /* Never */ ? 0 : 1); }, 1);
+ if (size >= 100000) {
+ ts.tracing.instant("check" /* Check */, "checkCrossProductUnion_DepthLimit", { typeIds: types.map(function (t) { return t.id; }), size: size });
+ error(currentNode, ts.Diagnostics.Expression_produces_a_union_type_that_is_too_complex_to_represent);
+ return false;
+ }
+ return true;
+ }
+ function getTypeFromIntersectionTypeNode(node) {
+ var links = getNodeLinks(node);
+ if (!links.resolvedType) {
+ var aliasSymbol = getAliasSymbolForTypeNode(node);
+ links.resolvedType = getIntersectionType(ts.map(node.types, getTypeFromTypeNode), aliasSymbol, getTypeArgumentsForAliasSymbol(aliasSymbol));
+ }
+ return links.resolvedType;
+ }
+ function createIndexType(type, stringsOnly) {
+ var result = createType(4194304 /* Index */);
+ result.type = type;
+ result.stringsOnly = stringsOnly;
+ return result;
+ }
+ function getIndexTypeForGenericType(type, stringsOnly) {
+ return stringsOnly ?
+ type.resolvedStringIndexType || (type.resolvedStringIndexType = createIndexType(type, /*stringsOnly*/ true)) :
+ type.resolvedIndexType || (type.resolvedIndexType = createIndexType(type, /*stringsOnly*/ false));
+ }
+ function getIndexTypeForMappedType(type, noIndexSignatures) {
+ var constraint = filterType(getConstraintTypeFromMappedType(type), function (t) { return !(noIndexSignatures && t.flags & (1 /* Any */ | 4 /* String */)); });
+ var nameType = type.declaration.nameType && getTypeFromTypeNode(type.declaration.nameType);
+ return nameType ?
+ mapType(constraint, function (t) { return instantiateType(nameType, appendTypeMapping(type.mapper, getTypeParameterFromMappedType(type), t)); }) :
+ constraint;
+ }
+ // Ordinarily we reduce a keyof M where M is a mapped type { [P in K as N]: X } to simply N. This however presumes
+ // that N distributes over union types, i.e. that N is equivalent to N | N | N. That presumption is
+ // generally true, except when N is a non-distributive conditional type or an instantiable type with non-distributive
+ // conditional type as a constituent. In those cases, we cannot reduce keyof M and need to preserve it as is.
+ function isNonDistributiveNameType(type) {
+ return !!(type && (type.flags & 16777216 /* Conditional */ && !type.root.isDistributive ||
+ type.flags & (3145728 /* UnionOrIntersection */ | 134217728 /* TemplateLiteral */) && ts.some(type.types, isNonDistributiveNameType) ||
+ type.flags & (4194304 /* Index */ | 268435456 /* StringMapping */) && isNonDistributiveNameType(type.type) ||
+ type.flags & 8388608 /* IndexedAccess */ && isNonDistributiveNameType(type.indexType) ||
+ type.flags & 33554432 /* Substitution */ && isNonDistributiveNameType(type.substitute)));
+ }
+ function getLiteralTypeFromPropertyName(name) {
+ if (ts.isPrivateIdentifier(name)) {
+ return neverType;
+ }
+ return ts.isIdentifier(name) ? getLiteralType(ts.unescapeLeadingUnderscores(name.escapedText)) :
+ getRegularTypeOfLiteralType(ts.isComputedPropertyName(name) ? checkComputedPropertyName(name) : checkExpression(name));
+ }
+ function getBigIntLiteralType(node) {
+ return getLiteralType({
+ negative: false,
+ base10Value: ts.parsePseudoBigInt(node.text)
+ });
+ }
+ function getLiteralTypeFromProperty(prop, include) {
+ if (!(ts.getDeclarationModifierFlagsFromSymbol(prop) & 24 /* NonPublicAccessibilityModifier */)) {
+ var type = getSymbolLinks(getLateBoundSymbol(prop)).nameType;
+ if (!type && !ts.isKnownSymbol(prop)) {
+ if (prop.escapedName === "default" /* Default */) {
+ type = getLiteralType("default");
}
- var allowStructuralFallback = targetTypeArguments && hasCovariantVoidArgument(targetTypeArguments, variances);
- varianceCheckFailed = !allowStructuralFallback;
- // The type arguments did not relate appropriately, but it may be because we have no variance
- // information (in which case typeArgumentsRelatedTo defaulted to covariance for all type
- // arguments). It might also be the case that the target type has a 'void' type argument for
- // a covariant type parameter that is only used in return positions within the generic type
- // (in which case any type argument is permitted on the source side). In those cases we proceed
- // with a structural comparison. Otherwise, we know for certain the instantiations aren't
- // related and we can return here.
- if (variances !== ts.emptyArray && !allowStructuralFallback) {
- // In some cases generic types that are covariant in regular type checking mode become
- // invariant in --strictFunctionTypes mode because one or more type parameters are used in
- // both co- and contravariant positions. In order to make it easier to diagnose *why* such
- // types are invariant, if any of the type parameters are invariant we reset the reported
- // errors and instead force a structural comparison (which will include elaborations that
- // reveal the reason).
- // We can switch on `reportErrors` here, since varianceCheckFailed guarantees we return `False`,
- // we can return `False` early here to skip calculating the structural error message we don't need.
- if (varianceCheckFailed && !(reportErrors && ts.some(variances, function (v) { return (v & 7 /* VarianceMask */) === 0 /* Invariant */; }))) {
- return 0 /* False */;
- }
- // We remember the original error information so we can restore it in case the structural
- // comparison unexpectedly succeeds. This can happen when the structural comparison result
- // is a Ternary.Maybe for example caused by the recursion depth limiter.
- originalErrorInfo = errorInfo;
- resetErrorInfo(saveErrorInfo);
+ else {
+ var name = prop.valueDeclaration && ts.getNameOfDeclaration(prop.valueDeclaration);
+ type = name && getLiteralTypeFromPropertyName(name) || getLiteralType(ts.symbolName(prop));
}
}
- }
- function reportUnmeasurableMarkers(p) {
- if (outofbandVarianceMarkerHandler && (p === markerSuperType || p === markerSubType || p === markerOtherType)) {
- outofbandVarianceMarkerHandler(/*onlyUnreliable*/ false);
+ if (type && type.flags & include) {
+ return type;
}
- return p;
}
- function reportUnreliableMarkers(p) {
- if (outofbandVarianceMarkerHandler && (p === markerSuperType || p === markerSubType || p === markerOtherType)) {
- outofbandVarianceMarkerHandler(/*onlyUnreliable*/ true);
+ return neverType;
+ }
+ function getLiteralTypeFromProperties(type, include) {
+ return getUnionType(ts.map(getPropertiesOfType(type), function (p) { return getLiteralTypeFromProperty(p, include); }));
+ }
+ function getNonEnumNumberIndexInfo(type) {
+ var numberIndexInfo = getIndexInfoOfType(type, 1 /* Number */);
+ return numberIndexInfo !== enumNumberIndexInfo ? numberIndexInfo : undefined;
+ }
+ function getIndexType(type, stringsOnly, noIndexSignatures) {
+ if (stringsOnly === void 0) { stringsOnly = keyofStringsOnly; }
+ type = getReducedType(type);
+ return type.flags & 1048576 /* Union */ ? getIntersectionType(ts.map(type.types, function (t) { return getIndexType(t, stringsOnly, noIndexSignatures); })) :
+ type.flags & 2097152 /* Intersection */ ? getUnionType(ts.map(type.types, function (t) { return getIndexType(t, stringsOnly, noIndexSignatures); })) :
+ type.flags & 58982400 /* InstantiableNonPrimitive */ || isGenericTupleType(type) || isGenericMappedType(type) && isNonDistributiveNameType(getNameTypeFromMappedType(type)) ? getIndexTypeForGenericType(type, stringsOnly) :
+ ts.getObjectFlags(type) & 32 /* Mapped */ ? getIndexTypeForMappedType(type, noIndexSignatures) :
+ type === wildcardType ? wildcardType :
+ type.flags & 2 /* Unknown */ ? neverType :
+ type.flags & (1 /* Any */ | 131072 /* Never */) ? keyofConstraintType :
+ stringsOnly ? !noIndexSignatures && getIndexInfoOfType(type, 0 /* String */) ? stringType : getLiteralTypeFromProperties(type, 128 /* StringLiteral */) :
+ !noIndexSignatures && getIndexInfoOfType(type, 0 /* String */) ? getUnionType([stringType, numberType, getLiteralTypeFromProperties(type, 8192 /* UniqueESSymbol */)]) :
+ getNonEnumNumberIndexInfo(type) ? getUnionType([numberType, getLiteralTypeFromProperties(type, 128 /* StringLiteral */ | 8192 /* UniqueESSymbol */)]) :
+ getLiteralTypeFromProperties(type, 8576 /* StringOrNumberLiteralOrUnique */);
+ }
+ function getExtractStringType(type) {
+ if (keyofStringsOnly) {
+ return type;
+ }
+ var extractTypeAlias = getGlobalExtractSymbol();
+ return extractTypeAlias ? getTypeAliasInstantiation(extractTypeAlias, [type, stringType]) : stringType;
+ }
+ function getIndexTypeOrString(type) {
+ var indexType = getExtractStringType(getIndexType(type));
+ return indexType.flags & 131072 /* Never */ ? stringType : indexType;
+ }
+ function getTypeFromTypeOperatorNode(node) {
+ var links = getNodeLinks(node);
+ if (!links.resolvedType) {
+ switch (node.operator) {
+ case 138 /* KeyOfKeyword */:
+ links.resolvedType = getIndexType(getTypeFromTypeNode(node.type));
+ break;
+ case 151 /* UniqueKeyword */:
+ links.resolvedType = node.type.kind === 148 /* SymbolKeyword */
+ ? getESSymbolLikeTypeForNode(ts.walkUpParenthesizedTypes(node.parent))
+ : errorType;
+ break;
+ case 142 /* ReadonlyKeyword */:
+ links.resolvedType = getTypeFromTypeNode(node.type);
+ break;
+ default:
+ throw ts.Debug.assertNever(node.operator);
}
- return p;
}
- // A type [P in S]: X is related to a type [Q in T]: Y if T is related to S and X' is
- // related to Y, where X' is an instantiation of X in which P is replaced with Q. Notice
- // that S and T are contra-variant whereas X and Y are co-variant.
- function mappedTypeRelatedTo(source, target, reportErrors) {
- var modifiersRelated = relation === comparableRelation || (relation === identityRelation ? getMappedTypeModifiers(source) === getMappedTypeModifiers(target) :
- getCombinedMappedTypeOptionality(source) <= getCombinedMappedTypeOptionality(target));
- if (modifiersRelated) {
- var result_8;
- var targetConstraint = getConstraintTypeFromMappedType(target);
- var sourceConstraint = instantiateType(getConstraintTypeFromMappedType(source), makeFunctionTypeMapper(getCombinedMappedTypeOptionality(source) < 0 ? reportUnmeasurableMarkers : reportUnreliableMarkers));
- if (result_8 = isRelatedTo(targetConstraint, sourceConstraint, reportErrors)) {
- var mapper = createTypeMapper([getTypeParameterFromMappedType(source)], [getTypeParameterFromMappedType(target)]);
- return result_8 & isRelatedTo(instantiateType(getTemplateTypeFromMappedType(source), mapper), getTemplateTypeFromMappedType(target), reportErrors);
+ return links.resolvedType;
+ }
+ function getTypeFromTemplateTypeNode(node) {
+ var links = getNodeLinks(node);
+ if (!links.resolvedType) {
+ links.resolvedType = getTemplateLiteralType(__spreadArrays([node.head.text], ts.map(node.templateSpans, function (span) { return span.literal.text; })), ts.map(node.templateSpans, function (span) { return getTypeFromTypeNode(span.type); }));
+ }
+ return links.resolvedType;
+ }
+ function getTemplateLiteralType(texts, types) {
+ var unionIndex = ts.findIndex(types, function (t) { return !!(t.flags & (131072 /* Never */ | 1048576 /* Union */)); });
+ if (unionIndex >= 0) {
+ return checkCrossProductUnion(types) ?
+ mapType(types[unionIndex], function (t) { return getTemplateLiteralType(texts, ts.replaceElement(types, unionIndex, t)); }) :
+ errorType;
+ }
+ if (ts.contains(types, wildcardType)) {
+ return wildcardType;
+ }
+ var newTypes = [];
+ var newTexts = [];
+ var text = texts[0];
+ if (!addSpans(texts, types)) {
+ return stringType;
+ }
+ if (newTypes.length === 0) {
+ return getLiteralType(text);
+ }
+ newTexts.push(text);
+ var id = getTypeListId(newTypes) + "|" + ts.map(newTexts, function (t) { return t.length; }).join(",") + "|" + newTexts.join("");
+ var type = templateLiteralTypes.get(id);
+ if (!type) {
+ templateLiteralTypes.set(id, type = createTemplateLiteralType(newTexts, newTypes));
+ }
+ return type;
+ function addSpans(texts, types) {
+ for (var i = 0; i < types.length; i++) {
+ var t = types[i];
+ if (t.flags & (2944 /* Literal */ | 65536 /* Null */ | 32768 /* Undefined */)) {
+ text += getTemplateStringForType(t) || "";
+ text += texts[i + 1];
+ }
+ else if (t.flags & 134217728 /* TemplateLiteral */) {
+ text += t.texts[0];
+ if (!addSpans(t.texts, t.types))
+ return false;
+ text += texts[i + 1];
+ }
+ else if (isGenericIndexType(t) || isPatternLiteralPlaceholderType(t)) {
+ newTypes.push(t);
+ newTexts.push(text);
+ text = texts[i + 1];
+ }
+ else {
+ return false;
}
}
- return 0 /* False */;
+ return true;
}
- function typeRelatedToDiscriminatedType(source, target) {
- // 1. Generate the combinations of discriminant properties & types 'source' can satisfy.
- // a. If the number of combinations is above a set limit, the comparison is too complex.
- // 2. Filter 'target' to the subset of types whose discriminants exist in the matrix.
- // a. If 'target' does not satisfy all discriminants in the matrix, 'source' is not related.
- // 3. For each type in the filtered 'target', determine if all non-discriminant properties of
- // 'target' are related to a property in 'source'.
- //
- // NOTE: See ~/tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithDiscriminatedUnion.ts
- // for examples.
- var sourceProperties = getPropertiesOfType(source);
- var sourcePropertiesFiltered = findDiscriminantProperties(sourceProperties, target);
- if (!sourcePropertiesFiltered)
- return 0 /* False */;
- // Though we could compute the number of combinations as we generate
- // the matrix, this would incur additional memory overhead due to
- // array allocations. To reduce this overhead, we first compute
- // the number of combinations to ensure we will not surpass our
- // fixed limit before incurring the cost of any allocations:
- var numCombinations = 1;
- for (var _i = 0, sourcePropertiesFiltered_1 = sourcePropertiesFiltered; _i < sourcePropertiesFiltered_1.length; _i++) {
- var sourceProperty = sourcePropertiesFiltered_1[_i];
- numCombinations *= countTypes(getTypeOfSymbol(sourceProperty));
- if (numCombinations > 25) {
- // We've reached the complexity limit.
- return 0 /* False */;
+ }
+ function getTemplateStringForType(type) {
+ return type.flags & 128 /* StringLiteral */ ? type.value :
+ type.flags & 256 /* NumberLiteral */ ? "" + type.value :
+ type.flags & 2048 /* BigIntLiteral */ ? ts.pseudoBigIntToString(type.value) :
+ type.flags & 512 /* BooleanLiteral */ ? type.intrinsicName :
+ type.flags & 65536 /* Null */ ? "null" :
+ type.flags & 32768 /* Undefined */ ? "undefined" :
+ undefined;
+ }
+ function createTemplateLiteralType(texts, types) {
+ var type = createType(134217728 /* TemplateLiteral */);
+ type.texts = texts;
+ type.types = types;
+ return type;
+ }
+ function getStringMappingType(symbol, type) {
+ return type.flags & (1048576 /* Union */ | 131072 /* Never */) ? mapType(type, function (t) { return getStringMappingType(symbol, t); }) :
+ isGenericIndexType(type) ? getStringMappingTypeForGenericType(symbol, type) :
+ type.flags & 128 /* StringLiteral */ ? getLiteralType(applyStringMapping(symbol, type.value)) :
+ type;
+ }
+ function applyStringMapping(symbol, str) {
+ switch (intrinsicTypeKinds.get(symbol.escapedName)) {
+ case 0 /* Uppercase */: return str.toUpperCase();
+ case 1 /* Lowercase */: return str.toLowerCase();
+ case 2 /* Capitalize */: return str.charAt(0).toUpperCase() + str.slice(1);
+ case 3 /* Uncapitalize */: return str.charAt(0).toLowerCase() + str.slice(1);
+ }
+ return str;
+ }
+ function getStringMappingTypeForGenericType(symbol, type) {
+ var id = getSymbolId(symbol) + "," + getTypeId(type);
+ var result = stringMappingTypes.get(id);
+ if (!result) {
+ stringMappingTypes.set(id, result = createStringMappingType(symbol, type));
+ }
+ return result;
+ }
+ function createStringMappingType(symbol, type) {
+ var result = createType(268435456 /* StringMapping */);
+ result.symbol = symbol;
+ result.type = type;
+ return result;
+ }
+ function createIndexedAccessType(objectType, indexType, aliasSymbol, aliasTypeArguments, shouldIncludeUndefined) {
+ var type = createType(8388608 /* IndexedAccess */);
+ type.objectType = objectType;
+ type.indexType = indexType;
+ type.aliasSymbol = aliasSymbol;
+ type.aliasTypeArguments = aliasTypeArguments;
+ type.noUncheckedIndexedAccessCandidate = shouldIncludeUndefined;
+ return type;
+ }
+ /**
+ * Returns if a type is or consists of a JSLiteral object type
+ * In addition to objects which are directly literals,
+ * * unions where every element is a jsliteral
+ * * intersections where at least one element is a jsliteral
+ * * and instantiable types constrained to a jsliteral
+ * Should all count as literals and not print errors on access or assignment of possibly existing properties.
+ * This mirrors the behavior of the index signature propagation, to which this behaves similarly (but doesn't affect assignability or inference).
+ */
+ function isJSLiteralType(type) {
+ if (noImplicitAny) {
+ return false; // Flag is meaningless under `noImplicitAny` mode
+ }
+ if (ts.getObjectFlags(type) & 16384 /* JSLiteral */) {
+ return true;
+ }
+ if (type.flags & 1048576 /* Union */) {
+ return ts.every(type.types, isJSLiteralType);
+ }
+ if (type.flags & 2097152 /* Intersection */) {
+ return ts.some(type.types, isJSLiteralType);
+ }
+ if (type.flags & 465829888 /* Instantiable */) {
+ return isJSLiteralType(getResolvedBaseConstraint(type));
+ }
+ return false;
+ }
+ function getPropertyNameFromIndex(indexType, accessNode) {
+ var accessExpression = accessNode && accessNode.kind === 202 /* ElementAccessExpression */ ? accessNode : undefined;
+ return isTypeUsableAsPropertyName(indexType) ?
+ getPropertyNameFromType(indexType) :
+ accessExpression && checkThatExpressionIsProperSymbolReference(accessExpression.argumentExpression, indexType, /*reportError*/ false) ?
+ ts.getPropertyNameForKnownSymbolName(ts.idText(accessExpression.argumentExpression.name)) :
+ accessNode && ts.isPropertyName(accessNode) ?
+ // late bound names are handled in the first branch, so here we only need to handle normal names
+ ts.getPropertyNameForPropertyNameNode(accessNode) :
+ undefined;
+ }
+ function isUncalledFunctionReference(node, symbol) {
+ return !(symbol.flags & (16 /* Function */ | 8192 /* Method */))
+ || !ts.isCallLikeExpression(ts.findAncestor(node, function (n) { return !ts.isAccessExpression(n); }) || node.parent)
+ && ts.every(symbol.declarations, function (d) { return !ts.isFunctionLike(d) || !!(ts.getCombinedNodeFlags(d) & 134217728 /* Deprecated */); });
+ }
+ function getPropertyTypeForIndexType(originalObjectType, objectType, indexType, fullIndexType, suppressNoImplicitAnyError, accessNode, accessFlags, noUncheckedIndexedAccessCandidate, reportDeprecated) {
+ var _a;
+ var accessExpression = accessNode && accessNode.kind === 202 /* ElementAccessExpression */ ? accessNode : undefined;
+ var propName = accessNode && ts.isPrivateIdentifier(accessNode) ? undefined : getPropertyNameFromIndex(indexType, accessNode);
+ if (propName !== undefined) {
+ var prop = getPropertyOfType(objectType, propName);
+ if (prop) {
+ if (reportDeprecated && accessNode && getDeclarationNodeFlagsFromSymbol(prop) & 134217728 /* Deprecated */ && isUncalledFunctionReference(accessNode, prop)) {
+ var deprecatedNode = (_a = accessExpression === null || accessExpression === void 0 ? void 0 : accessExpression.argumentExpression) !== null && _a !== void 0 ? _a : (ts.isIndexedAccessTypeNode(accessNode) ? accessNode.indexType : accessNode);
+ errorOrSuggestion(/* isError */ false, deprecatedNode, ts.Diagnostics._0_is_deprecated, propName);
}
- }
- // Compute the set of types for each discriminant property.
- var sourceDiscriminantTypes = new Array(sourcePropertiesFiltered.length);
- var excludedProperties = ts.createUnderscoreEscapedMap();
- for (var i = 0; i < sourcePropertiesFiltered.length; i++) {
- var sourceProperty = sourcePropertiesFiltered[i];
- var sourcePropertyType = getTypeOfSymbol(sourceProperty);
- sourceDiscriminantTypes[i] = sourcePropertyType.flags & 1048576 /* Union */
- ? sourcePropertyType.types
- : [sourcePropertyType];
- excludedProperties.set(sourceProperty.escapedName, true);
- }
- // Match each combination of the cartesian product of discriminant properties to one or more
- // constituents of 'target'. If any combination does not have a match then 'source' is not relatable.
- var discriminantCombinations = ts.cartesianProduct(sourceDiscriminantTypes);
- var matchingTypes = [];
- var _loop_14 = function (combination) {
- var hasMatch = false;
- outer: for (var _i = 0, _a = target.types; _i < _a.length; _i++) {
- var type = _a[_i];
- var _loop_15 = function (i) {
- var sourceProperty = sourcePropertiesFiltered[i];
- var targetProperty = getPropertyOfType(type, sourceProperty.escapedName);
- if (!targetProperty)
- return "continue-outer";
- if (sourceProperty === targetProperty)
- return "continue";
- // We compare the source property to the target in the context of a single discriminant type.
- var related = propertyRelatedTo(source, target, sourceProperty, targetProperty, function (_) { return combination[i]; }, /*reportErrors*/ false, 0 /* None */, /*skipOptional*/ strictNullChecks || relation === comparableRelation);
- // If the target property could not be found, or if the properties were not related,
- // then this constituent is not a match.
- if (!related) {
- return "continue-outer";
- }
- };
- for (var i = 0; i < sourcePropertiesFiltered.length; i++) {
- var state_7 = _loop_15(i);
- switch (state_7) {
- case "continue-outer": continue outer;
- }
+ if (accessExpression) {
+ markPropertyAsReferenced(prop, accessExpression, /*isThisAccess*/ accessExpression.expression.kind === 107 /* ThisKeyword */);
+ if (isAssignmentToReadonlyEntity(accessExpression, prop, ts.getAssignmentTargetKind(accessExpression))) {
+ error(accessExpression.argumentExpression, ts.Diagnostics.Cannot_assign_to_0_because_it_is_a_read_only_property, symbolToString(prop));
+ return undefined;
+ }
+ if (accessFlags & 4 /* CacheSymbol */) {
+ getNodeLinks(accessNode).resolvedSymbol = prop;
+ }
+ if (isThisPropertyAccessInConstructor(accessExpression, prop)) {
+ return autoType;
}
- ts.pushIfUnique(matchingTypes, type, ts.equateValues);
- hasMatch = true;
- }
- if (!hasMatch) {
- return { value: 0 /* False */ };
}
- };
- for (var _a = 0, discriminantCombinations_1 = discriminantCombinations; _a < discriminantCombinations_1.length; _a++) {
- var combination = discriminantCombinations_1[_a];
- var state_6 = _loop_14(combination);
- if (typeof state_6 === "object")
- return state_6.value;
+ var propType = getTypeOfSymbol(prop);
+ return accessExpression && ts.getAssignmentTargetKind(accessExpression) !== 1 /* Definite */ ?
+ getFlowTypeOfReference(accessExpression, propType) :
+ propType;
}
- // Compare the remaining non-discriminant properties of each match.
- var result = -1 /* True */;
- for (var _b = 0, matchingTypes_1 = matchingTypes; _b < matchingTypes_1.length; _b++) {
- var type = matchingTypes_1[_b];
- result &= propertiesRelatedTo(source, type, /*reportErrors*/ false, excludedProperties, 0 /* None */);
- if (result) {
- result &= signaturesRelatedTo(source, type, 0 /* Call */, /*reportStructuralErrors*/ false);
- if (result) {
- result &= signaturesRelatedTo(source, type, 1 /* Construct */, /*reportStructuralErrors*/ false);
- if (result) {
- result &= indexTypesRelatedTo(source, type, 0 /* String */, /*sourceIsPrimitive*/ false, /*reportStructuralErrors*/ false, 0 /* None */);
- if (result) {
- result &= indexTypesRelatedTo(source, type, 1 /* Number */, /*sourceIsPrimitive*/ false, /*reportStructuralErrors*/ false, 0 /* None */);
- }
- }
+ if (everyType(objectType, isTupleType) && isNumericLiteralName(propName) && +propName >= 0) {
+ if (accessNode && everyType(objectType, function (t) { return !t.target.hasRestElement; }) && !(accessFlags & 8 /* NoTupleBoundsCheck */)) {
+ var indexNode = getIndexNodeForAccessExpression(accessNode);
+ if (isTupleType(objectType)) {
+ error(indexNode, ts.Diagnostics.Tuple_type_0_of_length_1_has_no_element_at_index_2, typeToString(objectType), getTypeReferenceArity(objectType), ts.unescapeLeadingUnderscores(propName));
+ }
+ else {
+ error(indexNode, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.unescapeLeadingUnderscores(propName), typeToString(objectType));
}
}
- if (!result) {
- return result;
- }
+ errorIfWritingToReadonlyIndex(getIndexInfoOfType(objectType, 1 /* Number */));
+ return mapType(objectType, function (t) {
+ var restType = getRestTypeOfTupleType(t) || undefinedType;
+ return noUncheckedIndexedAccessCandidate ? getUnionType([restType, undefinedType]) : restType;
+ });
}
- return result;
}
- function excludeProperties(properties, excludedProperties) {
- if (!excludedProperties || properties.length === 0)
- return properties;
- var result;
- for (var i = 0; i < properties.length; i++) {
- if (!excludedProperties.has(properties[i].escapedName)) {
- if (result) {
- result.push(properties[i]);
+ if (!(indexType.flags & 98304 /* Nullable */) && isTypeAssignableToKind(indexType, 402653316 /* StringLike */ | 296 /* NumberLike */ | 12288 /* ESSymbolLike */)) {
+ if (objectType.flags & (1 /* Any */ | 131072 /* Never */)) {
+ return objectType;
+ }
+ var stringIndexInfo = getIndexInfoOfType(objectType, 0 /* String */);
+ var indexInfo = isTypeAssignableToKind(indexType, 296 /* NumberLike */) && getIndexInfoOfType(objectType, 1 /* Number */) || stringIndexInfo;
+ if (indexInfo) {
+ if (accessFlags & 1 /* NoIndexSignatures */ && indexInfo === stringIndexInfo) {
+ if (accessExpression) {
+ error(accessExpression, ts.Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(indexType), typeToString(originalObjectType));
}
+ return undefined;
}
- else if (!result) {
- result = properties.slice(0, i);
+ if (accessNode && !isTypeAssignableToKind(indexType, 4 /* String */ | 8 /* Number */)) {
+ var indexNode = getIndexNodeForAccessExpression(accessNode);
+ error(indexNode, ts.Diagnostics.Type_0_cannot_be_used_as_an_index_type, typeToString(indexType));
+ return noUncheckedIndexedAccessCandidate ? getUnionType([indexInfo.type, undefinedType]) : indexInfo.type;
}
+ errorIfWritingToReadonlyIndex(indexInfo);
+ return noUncheckedIndexedAccessCandidate ? getUnionType([indexInfo.type, undefinedType]) : indexInfo.type;
}
- return result || properties;
- }
- function isPropertySymbolTypeRelated(sourceProp, targetProp, getTypeOfSourceProperty, reportErrors, intersectionState) {
- var targetIsOptional = strictNullChecks && !!(ts.getCheckFlags(targetProp) & 48 /* Partial */);
- var source = getTypeOfSourceProperty(sourceProp);
- if (ts.getCheckFlags(targetProp) & 65536 /* DeferredType */ && !getSymbolLinks(targetProp).type) {
- // Rather than resolving (and normalizing) the type, relate constituent-by-constituent without performing normalization or seconadary passes
- var links = getSymbolLinks(targetProp);
- ts.Debug.assertIsDefined(links.deferralParent);
- ts.Debug.assertIsDefined(links.deferralConstituents);
- var unionParent = !!(links.deferralParent.flags & 1048576 /* Union */);
- var result_9 = unionParent ? 0 /* False */ : -1 /* True */;
- var targetTypes = links.deferralConstituents;
- for (var _i = 0, targetTypes_3 = targetTypes; _i < targetTypes_3.length; _i++) {
- var targetType = targetTypes_3[_i];
- var related = isRelatedTo(source, targetType, /*reportErrors*/ false, /*headMessage*/ undefined, unionParent ? 0 : 2 /* Target */);
- if (!unionParent) {
- if (!related) {
- // Can't assign to a target individually - have to fallback to assigning to the _whole_ intersection (which forces normalization)
- return isRelatedTo(source, addOptionality(getTypeOfSymbol(targetProp), targetIsOptional), reportErrors);
- }
- result_9 &= related;
+ if (indexType.flags & 131072 /* Never */) {
+ return neverType;
+ }
+ if (isJSLiteralType(objectType)) {
+ return anyType;
+ }
+ if (accessExpression && !isConstEnumObjectType(objectType)) {
+ if (objectType.symbol === globalThisSymbol && propName !== undefined && globalThisSymbol.exports.has(propName) && (globalThisSymbol.exports.get(propName).flags & 418 /* BlockScoped */)) {
+ error(accessExpression, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.unescapeLeadingUnderscores(propName), typeToString(objectType));
+ }
+ else if (noImplicitAny && !compilerOptions.suppressImplicitAnyIndexErrors && !suppressNoImplicitAnyError) {
+ if (propName !== undefined && typeHasStaticProperty(propName, objectType)) {
+ error(accessExpression, ts.Diagnostics.Property_0_is_a_static_member_of_type_1, propName, typeToString(objectType));
+ }
+ else if (getIndexTypeOfType(objectType, 1 /* Number */)) {
+ error(accessExpression.argumentExpression, ts.Diagnostics.Element_implicitly_has_an_any_type_because_index_expression_is_not_of_type_number);
}
else {
- if (related) {
- return related;
+ var suggestion = void 0;
+ if (propName !== undefined && (suggestion = getSuggestionForNonexistentProperty(propName, objectType))) {
+ if (suggestion !== undefined) {
+ error(accessExpression.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2, propName, typeToString(objectType), suggestion);
+ }
+ }
+ else {
+ var suggestion_1 = getSuggestionForNonexistentIndexSignature(objectType, accessExpression, indexType);
+ if (suggestion_1 !== undefined) {
+ error(accessExpression, ts.Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature_Did_you_mean_to_call_1, typeToString(objectType), suggestion_1);
+ }
+ else {
+ var errorInfo = void 0;
+ if (indexType.flags & 1024 /* EnumLiteral */) {
+ errorInfo = ts.chainDiagnosticMessages(/* details */ undefined, ts.Diagnostics.Property_0_does_not_exist_on_type_1, "[" + typeToString(indexType) + "]", typeToString(objectType));
+ }
+ else if (indexType.flags & 8192 /* UniqueESSymbol */) {
+ var symbolName_2 = getFullyQualifiedName(indexType.symbol, accessExpression);
+ errorInfo = ts.chainDiagnosticMessages(/* details */ undefined, ts.Diagnostics.Property_0_does_not_exist_on_type_1, "[" + symbolName_2 + "]", typeToString(objectType));
+ }
+ else if (indexType.flags & 128 /* StringLiteral */) {
+ errorInfo = ts.chainDiagnosticMessages(/* details */ undefined, ts.Diagnostics.Property_0_does_not_exist_on_type_1, indexType.value, typeToString(objectType));
+ }
+ else if (indexType.flags & 256 /* NumberLiteral */) {
+ errorInfo = ts.chainDiagnosticMessages(/* details */ undefined, ts.Diagnostics.Property_0_does_not_exist_on_type_1, indexType.value, typeToString(objectType));
+ }
+ else if (indexType.flags & (8 /* Number */ | 4 /* String */)) {
+ errorInfo = ts.chainDiagnosticMessages(/* details */ undefined, ts.Diagnostics.No_index_signature_with_a_parameter_of_type_0_was_found_on_type_1, typeToString(indexType), typeToString(objectType));
+ }
+ errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Element_implicitly_has_an_any_type_because_expression_of_type_0_can_t_be_used_to_index_type_1, typeToString(fullIndexType), typeToString(objectType));
+ diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(accessExpression, errorInfo));
+ }
}
}
}
- if (unionParent && !result_9 && targetIsOptional) {
- result_9 = isRelatedTo(source, undefinedType);
- }
- if (unionParent && !result_9 && reportErrors) {
- // The easiest way to get the right errors here is to un-defer (which may be costly)
- // If it turns out this is too costly too often, we can replicate the error handling logic within
- // typeRelatedToSomeType without the discriminatable type branch (as that requires a manifest union
- // type on which to hand discriminable properties, which we are expressly trying to avoid here)
- return isRelatedTo(source, addOptionality(getTypeOfSymbol(targetProp), targetIsOptional), reportErrors);
- }
- return result_9;
+ return undefined;
+ }
+ }
+ if (isJSLiteralType(objectType)) {
+ return anyType;
+ }
+ if (accessNode) {
+ var indexNode = getIndexNodeForAccessExpression(accessNode);
+ if (indexType.flags & (128 /* StringLiteral */ | 256 /* NumberLiteral */)) {
+ error(indexNode, ts.Diagnostics.Property_0_does_not_exist_on_type_1, "" + indexType.value, typeToString(objectType));
+ }
+ else if (indexType.flags & (4 /* String */ | 8 /* Number */)) {
+ error(indexNode, ts.Diagnostics.Type_0_has_no_matching_index_signature_for_type_1, typeToString(objectType), typeToString(indexType));
}
else {
- return isRelatedTo(source, addOptionality(getTypeOfSymbol(targetProp), targetIsOptional), reportErrors, /*headMessage*/ undefined, intersectionState);
+ error(indexNode, ts.Diagnostics.Type_0_cannot_be_used_as_an_index_type, typeToString(indexType));
}
}
- function propertyRelatedTo(source, target, sourceProp, targetProp, getTypeOfSourceProperty, reportErrors, intersectionState, skipOptional) {
- var sourcePropFlags = ts.getDeclarationModifierFlagsFromSymbol(sourceProp);
- var targetPropFlags = ts.getDeclarationModifierFlagsFromSymbol(targetProp);
- if (sourcePropFlags & 8 /* Private */ || targetPropFlags & 8 /* Private */) {
- if (sourceProp.valueDeclaration !== targetProp.valueDeclaration) {
- if (reportErrors) {
- if (sourcePropFlags & 8 /* Private */ && targetPropFlags & 8 /* Private */) {
- reportError(ts.Diagnostics.Types_have_separate_declarations_of_a_private_property_0, symbolToString(targetProp));
- }
- else {
- reportError(ts.Diagnostics.Property_0_is_private_in_type_1_but_not_in_type_2, symbolToString(targetProp), typeToString(sourcePropFlags & 8 /* Private */ ? source : target), typeToString(sourcePropFlags & 8 /* Private */ ? target : source));
- }
- }
- return 0 /* False */;
- }
+ if (isTypeAny(indexType)) {
+ return indexType;
+ }
+ return undefined;
+ function errorIfWritingToReadonlyIndex(indexInfo) {
+ if (indexInfo && indexInfo.isReadonly && accessExpression && (ts.isAssignmentTarget(accessExpression) || ts.isDeleteTarget(accessExpression))) {
+ error(accessExpression, ts.Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(objectType));
}
- else if (targetPropFlags & 16 /* Protected */) {
- if (!isValidOverrideOf(sourceProp, targetProp)) {
- if (reportErrors) {
- reportError(ts.Diagnostics.Property_0_is_protected_but_type_1_is_not_a_class_derived_from_2, symbolToString(targetProp), typeToString(getDeclaringClass(sourceProp) || source), typeToString(getDeclaringClass(targetProp) || target));
- }
- return 0 /* False */;
- }
+ }
+ }
+ function getIndexNodeForAccessExpression(accessNode) {
+ return accessNode.kind === 202 /* ElementAccessExpression */ ? accessNode.argumentExpression :
+ accessNode.kind === 189 /* IndexedAccessType */ ? accessNode.indexType :
+ accessNode.kind === 158 /* ComputedPropertyName */ ? accessNode.expression :
+ accessNode;
+ }
+ function isPatternLiteralPlaceholderType(type) {
+ return templateConstraintType.types.indexOf(type) !== -1 || !!(type.flags & 1 /* Any */);
+ }
+ function isPatternLiteralType(type) {
+ return !!(type.flags & 134217728 /* TemplateLiteral */) && ts.every(type.types, isPatternLiteralPlaceholderType);
+ }
+ function isGenericObjectType(type) {
+ if (type.flags & 3145728 /* UnionOrIntersection */) {
+ if (!(type.objectFlags & 4194304 /* IsGenericObjectTypeComputed */)) {
+ type.objectFlags |= 4194304 /* IsGenericObjectTypeComputed */ |
+ (ts.some(type.types, isGenericObjectType) ? 8388608 /* IsGenericObjectType */ : 0);
}
- else if (sourcePropFlags & 16 /* Protected */) {
- if (reportErrors) {
- reportError(ts.Diagnostics.Property_0_is_protected_in_type_1_but_public_in_type_2, symbolToString(targetProp), typeToString(source), typeToString(target));
- }
- return 0 /* False */;
+ return !!(type.objectFlags & 8388608 /* IsGenericObjectType */);
+ }
+ return !!(type.flags & 58982400 /* InstantiableNonPrimitive */) || isGenericMappedType(type) || isGenericTupleType(type);
+ }
+ function isGenericIndexType(type) {
+ if (type.flags & 3145728 /* UnionOrIntersection */) {
+ if (!(type.objectFlags & 16777216 /* IsGenericIndexTypeComputed */)) {
+ type.objectFlags |= 16777216 /* IsGenericIndexTypeComputed */ |
+ (ts.some(type.types, isGenericIndexType) ? 33554432 /* IsGenericIndexType */ : 0);
}
- // If the target comes from a partial union prop, allow `undefined` in the target type
- var related = isPropertySymbolTypeRelated(sourceProp, targetProp, getTypeOfSourceProperty, reportErrors, intersectionState);
- if (!related) {
- if (reportErrors) {
- reportIncompatibleError(ts.Diagnostics.Types_of_property_0_are_incompatible, symbolToString(targetProp));
- }
- return 0 /* False */;
+ return !!(type.objectFlags & 33554432 /* IsGenericIndexType */);
+ }
+ return !!(type.flags & (58982400 /* InstantiableNonPrimitive */ | 4194304 /* Index */ | 134217728 /* TemplateLiteral */ | 268435456 /* StringMapping */)) && !isPatternLiteralType(type);
+ }
+ function isThisTypeParameter(type) {
+ return !!(type.flags & 262144 /* TypeParameter */ && type.isThisType);
+ }
+ function getSimplifiedType(type, writing) {
+ return type.flags & 8388608 /* IndexedAccess */ ? getSimplifiedIndexedAccessType(type, writing) :
+ type.flags & 16777216 /* Conditional */ ? getSimplifiedConditionalType(type, writing) :
+ type;
+ }
+ function distributeIndexOverObjectType(objectType, indexType, writing) {
+ // (T | U)[K] -> T[K] | U[K] (reading)
+ // (T | U)[K] -> T[K] & U[K] (writing)
+ // (T & U)[K] -> T[K] & U[K]
+ if (objectType.flags & 3145728 /* UnionOrIntersection */) {
+ var types = ts.map(objectType.types, function (t) { return getSimplifiedType(getIndexedAccessType(t, indexType), writing); });
+ return objectType.flags & 2097152 /* Intersection */ || writing ? getIntersectionType(types) : getUnionType(types);
+ }
+ }
+ function distributeObjectOverIndexType(objectType, indexType, writing) {
+ // T[A | B] -> T[A] | T[B] (reading)
+ // T[A | B] -> T[A] & T[B] (writing)
+ if (indexType.flags & 1048576 /* Union */) {
+ var types = ts.map(indexType.types, function (t) { return getSimplifiedType(getIndexedAccessType(objectType, t), writing); });
+ return writing ? getIntersectionType(types) : getUnionType(types);
+ }
+ }
+ function unwrapSubstitution(type) {
+ if (type.flags & 33554432 /* Substitution */) {
+ return type.substitute;
+ }
+ return type;
+ }
+ // Transform an indexed access to a simpler form, if possible. Return the simpler form, or return
+ // the type itself if no transformation is possible. The writing flag indicates that the type is
+ // the target of an assignment.
+ function getSimplifiedIndexedAccessType(type, writing) {
+ var cache = writing ? "simplifiedForWriting" : "simplifiedForReading";
+ if (type[cache]) {
+ return type[cache] === circularConstraintType ? type : type[cache];
+ }
+ type[cache] = circularConstraintType;
+ // We recursively simplify the object type as it may in turn be an indexed access type. For example, with
+ // '{ [P in T]: { [Q in U]: number } }[T][U]' we want to first simplify the inner indexed access type.
+ var objectType = unwrapSubstitution(getSimplifiedType(type.objectType, writing));
+ var indexType = getSimplifiedType(type.indexType, writing);
+ // T[A | B] -> T[A] | T[B] (reading)
+ // T[A | B] -> T[A] & T[B] (writing)
+ var distributedOverIndex = distributeObjectOverIndexType(objectType, indexType, writing);
+ if (distributedOverIndex) {
+ return type[cache] = distributedOverIndex;
+ }
+ // Only do the inner distributions if the index can no longer be instantiated to cause index distribution again
+ if (!(indexType.flags & 465829888 /* Instantiable */)) {
+ // (T | U)[K] -> T[K] | U[K] (reading)
+ // (T | U)[K] -> T[K] & U[K] (writing)
+ // (T & U)[K] -> T[K] & U[K]
+ var distributedOverObject = distributeIndexOverObjectType(objectType, indexType, writing);
+ if (distributedOverObject) {
+ return type[cache] = distributedOverObject;
}
- // When checking for comparability, be more lenient with optional properties.
- if (!skipOptional && sourceProp.flags & 16777216 /* Optional */ && !(targetProp.flags & 16777216 /* Optional */)) {
- // TypeScript 1.0 spec (April 2014): 3.8.3
- // S is a subtype of a type T, and T is a supertype of S if ...
- // S' and T are object types and, for each member M in T..
- // M is a property and S' contains a property N where
- // if M is a required property, N is also a required property
- // (M - property in T)
- // (N - property in S)
- if (reportErrors) {
- reportError(ts.Diagnostics.Property_0_is_optional_in_type_1_but_required_in_type_2, symbolToString(targetProp), typeToString(source), typeToString(target));
- }
- return 0 /* False */;
+ }
+ // So ultimately (reading):
+ // ((A & B) | C)[K1 | K2] -> ((A & B) | C)[K1] | ((A & B) | C)[K2] -> (A & B)[K1] | C[K1] | (A & B)[K2] | C[K2] -> (A[K1] & B[K1]) | C[K1] | (A[K2] & B[K2]) | C[K2]
+ // A generic tuple type indexed by a number exists only when the index type doesn't select a
+ // fixed element. We simplify to either the combined type of all elements (when the index type
+ // the actual number type) or to the combined type of all non-fixed elements.
+ if (isGenericTupleType(objectType) && indexType.flags & 296 /* NumberLike */) {
+ var elementType = getElementTypeOfSliceOfTupleType(objectType, indexType.flags & 8 /* Number */ ? 0 : objectType.target.fixedLength, /*endSkipCount*/ 0, writing);
+ if (elementType) {
+ return type[cache] = elementType;
}
- return related;
}
- function reportUnmatchedProperty(source, target, unmatchedProperty, requireOptionalProperties) {
- var shouldSkipElaboration = false;
- // give specific error in case where private names have the same description
- if (unmatchedProperty.valueDeclaration
- && ts.isNamedDeclaration(unmatchedProperty.valueDeclaration)
- && ts.isPrivateIdentifier(unmatchedProperty.valueDeclaration.name)
- && source.symbol
- && source.symbol.flags & 32 /* Class */) {
- var privateIdentifierDescription = unmatchedProperty.valueDeclaration.name.escapedText;
- var symbolTableKey = ts.getSymbolNameForPrivateIdentifier(source.symbol, privateIdentifierDescription);
- if (symbolTableKey && getPropertyOfType(source, symbolTableKey)) {
- var sourceName = ts.getDeclarationName(source.symbol.valueDeclaration);
- var targetName = ts.getDeclarationName(target.symbol.valueDeclaration);
- reportError(ts.Diagnostics.Property_0_in_type_1_refers_to_a_different_member_that_cannot_be_accessed_from_within_type_2, diagnosticName(privateIdentifierDescription), diagnosticName(sourceName.escapedText === "" ? anon : sourceName), diagnosticName(targetName.escapedText === "" ? anon : targetName));
- return;
- }
+ // If the object type is a mapped type { [P in K]: E }, where K is generic, instantiate E using a mapper
+ // that substitutes the index type for P. For example, for an index access { [P in K]: Box }[X], we
+ // construct the type Box.
+ if (isGenericMappedType(objectType)) {
+ return type[cache] = mapType(substituteIndexedMappedType(objectType, type.indexType), function (t) { return getSimplifiedType(t, writing); });
+ }
+ return type[cache] = type;
+ }
+ function getSimplifiedConditionalType(type, writing) {
+ var checkType = type.checkType;
+ var extendsType = type.extendsType;
+ var trueType = getTrueTypeFromConditionalType(type);
+ var falseType = getFalseTypeFromConditionalType(type);
+ // Simplifications for types of the form `T extends U ? T : never` and `T extends U ? never : T`.
+ if (falseType.flags & 131072 /* Never */ && getActualTypeVariable(trueType) === getActualTypeVariable(checkType)) {
+ if (checkType.flags & 1 /* Any */ || isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(extendsType))) { // Always true
+ return getSimplifiedType(trueType, writing);
}
- var props = ts.arrayFrom(getUnmatchedProperties(source, target, requireOptionalProperties, /*matchDiscriminantProperties*/ false));
- if (!headMessage || (headMessage.code !== ts.Diagnostics.Class_0_incorrectly_implements_interface_1.code &&
- headMessage.code !== ts.Diagnostics.Class_0_incorrectly_implements_class_1_Did_you_mean_to_extend_1_and_inherit_its_members_as_a_subclass.code)) {
- shouldSkipElaboration = true; // Retain top-level error for interface implementing issues, otherwise omit it
+ else if (isIntersectionEmpty(checkType, extendsType)) { // Always false
+ return neverType;
}
- if (props.length === 1) {
- var propName = symbolToString(unmatchedProperty);
- reportError.apply(void 0, __spreadArrays([ts.Diagnostics.Property_0_is_missing_in_type_1_but_required_in_type_2, propName], getTypeNamesForErrorDisplay(source, target)));
- if (ts.length(unmatchedProperty.declarations)) {
- associateRelatedInfo(ts.createDiagnosticForNode(unmatchedProperty.declarations[0], ts.Diagnostics._0_is_declared_here, propName));
- }
- if (shouldSkipElaboration && errorInfo) {
- overrideNextErrorInfo++;
+ }
+ else if (trueType.flags & 131072 /* Never */ && getActualTypeVariable(falseType) === getActualTypeVariable(checkType)) {
+ if (!(checkType.flags & 1 /* Any */) && isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(extendsType))) { // Always true
+ return neverType;
+ }
+ else if (checkType.flags & 1 /* Any */ || isIntersectionEmpty(checkType, extendsType)) { // Always false
+ return getSimplifiedType(falseType, writing);
+ }
+ }
+ return type;
+ }
+ /**
+ * Invokes union simplification logic to determine if an intersection is considered empty as a union constituent
+ */
+ function isIntersectionEmpty(type1, type2) {
+ return !!(getUnionType([intersectTypes(type1, type2), neverType]).flags & 131072 /* Never */);
+ }
+ function substituteIndexedMappedType(objectType, index) {
+ var mapper = createTypeMapper([getTypeParameterFromMappedType(objectType)], [index]);
+ var templateMapper = combineTypeMappers(objectType.mapper, mapper);
+ return instantiateType(getTemplateTypeFromMappedType(objectType), templateMapper);
+ }
+ function getIndexedAccessType(objectType, indexType, noUncheckedIndexedAccessCandidate, accessNode, aliasSymbol, aliasTypeArguments, accessFlags) {
+ if (accessFlags === void 0) { accessFlags = 0 /* None */; }
+ return getIndexedAccessTypeOrUndefined(objectType, indexType, noUncheckedIndexedAccessCandidate, accessNode, accessFlags, aliasSymbol, aliasTypeArguments) || (accessNode ? errorType : unknownType);
+ }
+ function indexTypeLessThan(indexType, limit) {
+ return everyType(indexType, function (t) {
+ if (t.flags & 384 /* StringOrNumberLiteral */) {
+ var propName = getPropertyNameFromType(t);
+ if (isNumericLiteralName(propName)) {
+ var index = +propName;
+ return index >= 0 && index < limit;
}
}
- else if (tryElaborateArrayLikeErrors(source, target, /*reportErrors*/ false)) {
- if (props.length > 5) { // arbitrary cutoff for too-long list form
- reportError(ts.Diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2_and_3_more, typeToString(source), typeToString(target), ts.map(props.slice(0, 4), function (p) { return symbolToString(p); }).join(", "), props.length - 4);
+ return false;
+ });
+ }
+ function getIndexedAccessTypeOrUndefined(objectType, indexType, noUncheckedIndexedAccessCandidate, accessNode, accessFlags, aliasSymbol, aliasTypeArguments) {
+ if (accessFlags === void 0) { accessFlags = 0 /* None */; }
+ if (objectType === wildcardType || indexType === wildcardType) {
+ return wildcardType;
+ }
+ var shouldIncludeUndefined = noUncheckedIndexedAccessCandidate ||
+ (!!compilerOptions.noUncheckedIndexedAccess &&
+ (accessFlags & (2 /* Writing */ | 16 /* ExpressionPosition */)) === 16 /* ExpressionPosition */);
+ // If the object type has a string index signature and no other members we know that the result will
+ // always be the type of that index signature and we can simplify accordingly.
+ if (isStringIndexSignatureOnlyType(objectType) && !(indexType.flags & 98304 /* Nullable */) && isTypeAssignableToKind(indexType, 4 /* String */ | 8 /* Number */)) {
+ indexType = stringType;
+ }
+ // If the index type is generic, or if the object type is generic and doesn't originate in an expression and
+ // the operation isn't exclusively indexing the fixed (non-variadic) portion of a tuple type, we are performing
+ // a higher-order index access where we cannot meaningfully access the properties of the object type. Note that
+ // for a generic T and a non-generic K, we eagerly resolve T[K] if it originates in an expression. This is to
+ // preserve backwards compatibility. For example, an element access 'this["foo"]' has always been resolved
+ // eagerly using the constraint type of 'this' at the given location.
+ if (isGenericIndexType(indexType) || (accessNode && accessNode.kind !== 189 /* IndexedAccessType */ ?
+ isGenericTupleType(objectType) && !indexTypeLessThan(indexType, objectType.target.fixedLength) :
+ isGenericObjectType(objectType) && !(isTupleType(objectType) && indexTypeLessThan(indexType, objectType.target.fixedLength)))) {
+ if (objectType.flags & 3 /* AnyOrUnknown */) {
+ return objectType;
+ }
+ // Defer the operation by creating an indexed access type.
+ var id = objectType.id + "," + indexType.id + (shouldIncludeUndefined ? "?" : "");
+ var type = indexedAccessTypes.get(id);
+ if (!type) {
+ indexedAccessTypes.set(id, type = createIndexedAccessType(objectType, indexType, aliasSymbol, aliasTypeArguments, shouldIncludeUndefined));
+ }
+ return type;
+ }
+ // In the following we resolve T[K] to the type of the property in T selected by K.
+ // We treat boolean as different from other unions to improve errors;
+ // skipping straight to getPropertyTypeForIndexType gives errors with 'boolean' instead of 'true'.
+ var apparentObjectType = getReducedApparentType(objectType);
+ if (indexType.flags & 1048576 /* Union */ && !(indexType.flags & 16 /* Boolean */)) {
+ var propTypes = [];
+ var wasMissingProp = false;
+ for (var _i = 0, _a = indexType.types; _i < _a.length; _i++) {
+ var t = _a[_i];
+ var propType = getPropertyTypeForIndexType(objectType, apparentObjectType, t, indexType, wasMissingProp, accessNode, accessFlags, shouldIncludeUndefined);
+ if (propType) {
+ propTypes.push(propType);
}
- else {
- reportError(ts.Diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2, typeToString(source), typeToString(target), ts.map(props, function (p) { return symbolToString(p); }).join(", "));
+ else if (!accessNode) {
+ // If there's no error node, we can immeditely stop, since error reporting is off
+ return undefined;
}
- if (shouldSkipElaboration && errorInfo) {
- overrideNextErrorInfo++;
+ else {
+ // Otherwise we set a flag and return at the end of the loop so we still mark all errors
+ wasMissingProp = true;
}
}
- // No array like or unmatched property error - just issue top level error (errorInfo = undefined)
- }
- function propertiesRelatedTo(source, target, reportErrors, excludedProperties, intersectionState) {
- if (relation === identityRelation) {
- return propertiesIdenticalTo(source, target, excludedProperties);
+ if (wasMissingProp) {
+ return undefined;
}
- var requireOptionalProperties = (relation === subtypeRelation || relation === strictSubtypeRelation) && !isObjectLiteralType(source) && !isEmptyArrayLiteralType(source) && !isTupleType(source);
- var unmatchedProperty = getUnmatchedProperty(source, target, requireOptionalProperties, /*matchDiscriminantProperties*/ false);
- if (unmatchedProperty) {
- if (reportErrors) {
- reportUnmatchedProperty(source, target, unmatchedProperty, requireOptionalProperties);
- }
- return 0 /* False */;
+ return accessFlags & 2 /* Writing */
+ ? getIntersectionType(propTypes, aliasSymbol, aliasTypeArguments)
+ : getUnionType(propTypes, 1 /* Literal */, aliasSymbol, aliasTypeArguments);
+ }
+ return getPropertyTypeForIndexType(objectType, apparentObjectType, indexType, indexType, /* supressNoImplicitAnyError */ false, accessNode, accessFlags | 4 /* CacheSymbol */, shouldIncludeUndefined, /* reportDeprecated */ true);
+ }
+ function getTypeFromIndexedAccessTypeNode(node) {
+ var links = getNodeLinks(node);
+ if (!links.resolvedType) {
+ var objectType = getTypeFromTypeNode(node.objectType);
+ var indexType = getTypeFromTypeNode(node.indexType);
+ var potentialAlias = getAliasSymbolForTypeNode(node);
+ var resolved = getIndexedAccessType(objectType, indexType, /*noUncheckedIndexedAccessCandidate*/ undefined, node, potentialAlias, getTypeArgumentsForAliasSymbol(potentialAlias));
+ links.resolvedType = resolved.flags & 8388608 /* IndexedAccess */ &&
+ resolved.objectType === objectType &&
+ resolved.indexType === indexType ?
+ getConditionalFlowTypeOfType(resolved, node) : resolved;
+ }
+ return links.resolvedType;
+ }
+ function getTypeFromMappedTypeNode(node) {
+ var links = getNodeLinks(node);
+ if (!links.resolvedType) {
+ var type = createObjectType(32 /* Mapped */, node.symbol);
+ type.declaration = node;
+ type.aliasSymbol = getAliasSymbolForTypeNode(node);
+ type.aliasTypeArguments = getTypeArgumentsForAliasSymbol(type.aliasSymbol);
+ links.resolvedType = type;
+ // Eagerly resolve the constraint type which forces an error if the constraint type circularly
+ // references itself through one or more type aliases.
+ getConstraintTypeFromMappedType(type);
+ }
+ return links.resolvedType;
+ }
+ function getActualTypeVariable(type) {
+ if (type.flags & 33554432 /* Substitution */) {
+ return type.baseType;
+ }
+ if (type.flags & 8388608 /* IndexedAccess */ && (type.objectType.flags & 33554432 /* Substitution */ ||
+ type.indexType.flags & 33554432 /* Substitution */)) {
+ return getIndexedAccessType(getActualTypeVariable(type.objectType), getActualTypeVariable(type.indexType));
+ }
+ return type;
+ }
+ function getConditionalType(root, mapper) {
+ var result;
+ var extraTypes;
+ var _loop_15 = function () {
+ var checkType = instantiateType(root.checkType, mapper);
+ var checkTypeInstantiable = isGenericObjectType(checkType) || isGenericIndexType(checkType);
+ var extendsType = instantiateType(root.extendsType, mapper);
+ if (checkType === wildcardType || extendsType === wildcardType) {
+ return { value: wildcardType };
}
- if (isObjectLiteralType(target)) {
- for (var _i = 0, _a = excludeProperties(getPropertiesOfType(source), excludedProperties); _i < _a.length; _i++) {
- var sourceProp = _a[_i];
- if (!getPropertyOfObjectType(target, sourceProp.escapedName)) {
- var sourceType = getTypeOfSymbol(sourceProp);
- if (!(sourceType === undefinedType || sourceType === undefinedWideningType || sourceType === optionalType)) {
- if (reportErrors) {
- reportError(ts.Diagnostics.Property_0_does_not_exist_on_type_1, symbolToString(sourceProp), typeToString(target));
- }
- return 0 /* False */;
- }
- }
+ var combinedMapper = void 0;
+ if (root.inferTypeParameters) {
+ var context = createInferenceContext(root.inferTypeParameters, /*signature*/ undefined, 0 /* None */);
+ // We skip inference of the possible `infer` types unles the `extendsType` _is_ an infer type
+ // if it was, it's trivial to say that extendsType = checkType, however such a pattern is used to
+ // "reset" the type being build up during constraint calculation and avoid making an apparently "infinite" constraint
+ // so in those cases we refain from performing inference and retain the uninfered type parameter
+ if (!checkTypeInstantiable || !ts.some(root.inferTypeParameters, function (t) { return t === extendsType; })) {
+ // We don't want inferences from constraints as they may cause us to eagerly resolve the
+ // conditional type instead of deferring resolution. Also, we always want strict function
+ // types rules (i.e. proper contravariance) for inferences.
+ inferTypes(context.inferences, checkType, extendsType, 256 /* NoConstraints */ | 512 /* AlwaysStrict */);
}
+ combinedMapper = mergeTypeMappers(mapper, context.mapper);
}
- var result = -1 /* True */;
- if (isTupleType(target)) {
- var targetRestType = getRestTypeOfTupleType(target);
- if (targetRestType) {
- if (!isTupleType(source)) {
- return 0 /* False */;
- }
- var sourceRestType = getRestTypeOfTupleType(source);
- if (sourceRestType && !isRelatedTo(sourceRestType, targetRestType, reportErrors)) {
- if (reportErrors) {
- reportError(ts.Diagnostics.Rest_signatures_are_incompatible);
- }
- return 0 /* False */;
+ // Instantiate the extends type including inferences for 'infer T' type parameters
+ var inferredExtendsType = combinedMapper ? instantiateType(root.extendsType, combinedMapper) : extendsType;
+ // We attempt to resolve the conditional type only when the check and extends types are non-generic
+ if (!checkTypeInstantiable && !isGenericObjectType(inferredExtendsType) && !isGenericIndexType(inferredExtendsType)) {
+ // Return falseType for a definitely false extends check. We check an instantiations of the two
+ // types with type parameters mapped to the wildcard type, the most permissive instantiations
+ // possible (the wildcard type is assignable to and from all types). If those are not related,
+ // then no instantiations will be and we can just return the false branch type.
+ if (!(inferredExtendsType.flags & 3 /* AnyOrUnknown */) && (checkType.flags & 1 /* Any */ || !isTypeAssignableTo(getPermissiveInstantiation(checkType), getPermissiveInstantiation(inferredExtendsType)))) {
+ // Return union of trueType and falseType for 'any' since it matches anything
+ if (checkType.flags & 1 /* Any */) {
+ (extraTypes || (extraTypes = [])).push(instantiateType(getTypeFromTypeNode(root.node.trueType), combinedMapper || mapper));
}
- var targetCount = getTypeReferenceArity(target) - 1;
- var sourceCount = getTypeReferenceArity(source) - (sourceRestType ? 1 : 0);
- var sourceTypeArguments = getTypeArguments(source);
- for (var i = targetCount; i < sourceCount; i++) {
- var related = isRelatedTo(sourceTypeArguments[i], targetRestType, reportErrors);
- if (!related) {
- if (reportErrors) {
- reportError(ts.Diagnostics.Property_0_is_incompatible_with_rest_element_type, "" + i);
- }
- return 0 /* False */;
+ // If falseType is an immediately nested conditional type that isn't distributive or has an
+ // identical checkType, switch to that type and loop.
+ var falseType_1 = getTypeFromTypeNode(root.node.falseType);
+ if (falseType_1.flags & 16777216 /* Conditional */) {
+ var newRoot = falseType_1.root;
+ if (newRoot.node.parent === root.node && (!newRoot.isDistributive || newRoot.checkType === root.checkType)) {
+ root = newRoot;
+ return "continue";
}
- result &= related;
}
+ result = instantiateType(falseType_1, mapper);
+ return "break";
}
- }
- // We only call this for union target types when we're attempting to do excess property checking - in those cases, we want to get _all possible props_
- // from the target union, across all members
- var properties = getPropertiesOfType(target);
- var numericNamesOnly = isTupleType(source) && isTupleType(target);
- for (var _b = 0, _c = excludeProperties(properties, excludedProperties); _b < _c.length; _b++) {
- var targetProp = _c[_b];
- var name = targetProp.escapedName;
- if (!(targetProp.flags & 4194304 /* Prototype */) && (!numericNamesOnly || isNumericLiteralName(name) || name === "length")) {
- var sourceProp = getPropertyOfType(source, name);
- if (sourceProp && sourceProp !== targetProp) {
- var related = propertyRelatedTo(source, target, sourceProp, targetProp, getTypeOfSymbol, reportErrors, intersectionState, relation === comparableRelation);
- if (!related) {
- return 0 /* False */;
- }
- result &= related;
- }
+ // Return trueType for a definitely true extends check. We check instantiations of the two
+ // types with type parameters mapped to their restrictive form, i.e. a form of the type parameter
+ // that has no constraint. This ensures that, for example, the type
+ // type Foo = T extends { x: string } ? string : number
+ // doesn't immediately resolve to 'string' instead of being deferred.
+ if (inferredExtendsType.flags & 3 /* AnyOrUnknown */ || isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(inferredExtendsType))) {
+ result = instantiateType(getTypeFromTypeNode(root.node.trueType), combinedMapper || mapper);
+ return "break";
}
}
- return result;
+ // Return a deferred type for a check that is neither definitely true nor definitely false
+ var erasedCheckType = getActualTypeVariable(checkType);
+ result = createType(16777216 /* Conditional */);
+ result.root = root;
+ result.checkType = erasedCheckType;
+ result.extendsType = extendsType;
+ result.mapper = mapper;
+ result.combinedMapper = combinedMapper;
+ result.aliasSymbol = root.aliasSymbol;
+ result.aliasTypeArguments = instantiateTypes(root.aliasTypeArguments, mapper); // TODO: GH#18217
+ return "break";
+ };
+ // We loop here for an immediately nested conditional type in the false position, effectively treating
+ // types of the form 'A extends B ? X : C extends D ? Y : E extends F ? Z : ...' as a single construct for
+ // purposes of resolution. This means such types aren't subject to the instatiation depth limiter.
+ while (true) {
+ var state_4 = _loop_15();
+ if (typeof state_4 === "object")
+ return state_4.value;
+ if (state_4 === "break")
+ break;
}
- function propertiesIdenticalTo(source, target, excludedProperties) {
- if (!(source.flags & 524288 /* Object */ && target.flags & 524288 /* Object */)) {
- return 0 /* False */;
- }
- var sourceProperties = excludeProperties(getPropertiesOfObjectType(source), excludedProperties);
- var targetProperties = excludeProperties(getPropertiesOfObjectType(target), excludedProperties);
- if (sourceProperties.length !== targetProperties.length) {
- return 0 /* False */;
- }
- var result = -1 /* True */;
- for (var _i = 0, sourceProperties_1 = sourceProperties; _i < sourceProperties_1.length; _i++) {
- var sourceProp = sourceProperties_1[_i];
- var targetProp = getPropertyOfObjectType(target, sourceProp.escapedName);
- if (!targetProp) {
- return 0 /* False */;
- }
- var related = compareProperties(sourceProp, targetProp, isRelatedTo);
- if (!related) {
- return 0 /* False */;
+ return extraTypes ? getUnionType(ts.append(extraTypes, result)) : result;
+ }
+ function getTrueTypeFromConditionalType(type) {
+ return type.resolvedTrueType || (type.resolvedTrueType = instantiateType(getTypeFromTypeNode(type.root.node.trueType), type.mapper));
+ }
+ function getFalseTypeFromConditionalType(type) {
+ return type.resolvedFalseType || (type.resolvedFalseType = instantiateType(getTypeFromTypeNode(type.root.node.falseType), type.mapper));
+ }
+ function getInferredTrueTypeFromConditionalType(type) {
+ return type.resolvedInferredTrueType || (type.resolvedInferredTrueType = type.combinedMapper ? instantiateType(getTypeFromTypeNode(type.root.node.trueType), type.combinedMapper) : getTrueTypeFromConditionalType(type));
+ }
+ function getInferTypeParameters(node) {
+ var result;
+ if (node.locals) {
+ node.locals.forEach(function (symbol) {
+ if (symbol.flags & 262144 /* TypeParameter */) {
+ result = ts.append(result, getDeclaredTypeOfSymbol(symbol));
}
- result &= related;
+ });
+ }
+ return result;
+ }
+ function getTypeFromConditionalTypeNode(node) {
+ var links = getNodeLinks(node);
+ if (!links.resolvedType) {
+ var checkType = getTypeFromTypeNode(node.checkType);
+ var aliasSymbol = getAliasSymbolForTypeNode(node);
+ var aliasTypeArguments = getTypeArgumentsForAliasSymbol(aliasSymbol);
+ var allOuterTypeParameters = getOuterTypeParameters(node, /*includeThisTypes*/ true);
+ var outerTypeParameters = aliasTypeArguments ? allOuterTypeParameters : ts.filter(allOuterTypeParameters, function (tp) { return isTypeParameterPossiblyReferenced(tp, node); });
+ var root = {
+ node: node,
+ checkType: checkType,
+ extendsType: getTypeFromTypeNode(node.extendsType),
+ isDistributive: !!(checkType.flags & 262144 /* TypeParameter */),
+ inferTypeParameters: getInferTypeParameters(node),
+ outerTypeParameters: outerTypeParameters,
+ instantiations: undefined,
+ aliasSymbol: aliasSymbol,
+ aliasTypeArguments: aliasTypeArguments
+ };
+ links.resolvedType = getConditionalType(root, /*mapper*/ undefined);
+ if (outerTypeParameters) {
+ root.instantiations = new ts.Map();
+ root.instantiations.set(getTypeListId(outerTypeParameters), links.resolvedType);
}
- return result;
}
- function signaturesRelatedTo(source, target, kind, reportErrors) {
- if (relation === identityRelation) {
- return signaturesIdenticalTo(source, target, kind);
+ return links.resolvedType;
+ }
+ function getTypeFromInferTypeNode(node) {
+ var links = getNodeLinks(node);
+ if (!links.resolvedType) {
+ links.resolvedType = getDeclaredTypeOfTypeParameter(getSymbolOfNode(node.typeParameter));
+ }
+ return links.resolvedType;
+ }
+ function getIdentifierChain(node) {
+ if (ts.isIdentifier(node)) {
+ return [node];
+ }
+ else {
+ return ts.append(getIdentifierChain(node.left), node.right);
+ }
+ }
+ function getTypeFromImportTypeNode(node) {
+ var links = getNodeLinks(node);
+ if (!links.resolvedType) {
+ if (node.isTypeOf && node.typeArguments) { // Only the non-typeof form can make use of type arguments
+ error(node, ts.Diagnostics.Type_arguments_cannot_be_used_here);
+ links.resolvedSymbol = unknownSymbol;
+ return links.resolvedType = errorType;
}
- if (target === anyFunctionType || source === anyFunctionType) {
- return -1 /* True */;
+ if (!ts.isLiteralImportTypeNode(node)) {
+ error(node.argument, ts.Diagnostics.String_literal_expected);
+ links.resolvedSymbol = unknownSymbol;
+ return links.resolvedType = errorType;
}
- var sourceIsJSConstructor = source.symbol && isJSConstructor(source.symbol.valueDeclaration);
- var targetIsJSConstructor = target.symbol && isJSConstructor(target.symbol.valueDeclaration);
- var sourceSignatures = getSignaturesOfType(source, (sourceIsJSConstructor && kind === 1 /* Construct */) ?
- 0 /* Call */ : kind);
- var targetSignatures = getSignaturesOfType(target, (targetIsJSConstructor && kind === 1 /* Construct */) ?
- 0 /* Call */ : kind);
- if (kind === 1 /* Construct */ && sourceSignatures.length && targetSignatures.length) {
- if (ts.isAbstractConstructorType(source) && !ts.isAbstractConstructorType(target)) {
- // An abstract constructor type is not assignable to a non-abstract constructor type
- // as it would otherwise be possible to new an abstract class. Note that the assignability
- // check we perform for an extends clause excludes construct signatures from the target,
- // so this check never proceeds.
- if (reportErrors) {
- reportError(ts.Diagnostics.Cannot_assign_an_abstract_constructor_type_to_a_non_abstract_constructor_type);
- }
- return 0 /* False */;
- }
- if (!constructorVisibilitiesAreCompatible(sourceSignatures[0], targetSignatures[0], reportErrors)) {
- return 0 /* False */;
- }
+ var targetMeaning = node.isTypeOf ? 111551 /* Value */ : node.flags & 4194304 /* JSDoc */ ? 111551 /* Value */ | 788968 /* Type */ : 788968 /* Type */;
+ // TODO: Future work: support unions/generics/whatever via a deferred import-type
+ var innerModuleSymbol = resolveExternalModuleName(node, node.argument.literal);
+ if (!innerModuleSymbol) {
+ links.resolvedSymbol = unknownSymbol;
+ return links.resolvedType = errorType;
}
- var result = -1 /* True */;
- var saveErrorInfo = captureErrorCalculationState();
- var incompatibleReporter = kind === 1 /* Construct */ ? reportIncompatibleConstructSignatureReturn : reportIncompatibleCallSignatureReturn;
- if (ts.getObjectFlags(source) & 64 /* Instantiated */ && ts.getObjectFlags(target) & 64 /* Instantiated */ && source.symbol === target.symbol) {
- // We have instantiations of the same anonymous type (which typically will be the type of a
- // method). Simply do a pairwise comparison of the signatures in the two signature lists instead
- // of the much more expensive N * M comparison matrix we explore below. We erase type parameters
- // as they are known to always be the same.
- for (var i = 0; i < targetSignatures.length; i++) {
- var related = signatureRelatedTo(sourceSignatures[i], targetSignatures[i], /*erase*/ true, reportErrors, incompatibleReporter(sourceSignatures[i], targetSignatures[i]));
- if (!related) {
- return 0 /* False */;
+ var moduleSymbol = resolveExternalModuleSymbol(innerModuleSymbol, /*dontResolveAlias*/ false);
+ if (!ts.nodeIsMissing(node.qualifier)) {
+ var nameStack = getIdentifierChain(node.qualifier);
+ var currentNamespace = moduleSymbol;
+ var current = void 0;
+ while (current = nameStack.shift()) {
+ var meaning = nameStack.length ? 1920 /* Namespace */ : targetMeaning;
+ // typeof a.b.c is normally resolved using `checkExpression` which in turn defers to `checkQualifiedName`
+ // That, in turn, ultimately uses `getPropertyOfType` on the type of the symbol, which differs slightly from
+ // the `exports` lookup process that only looks up namespace members which is used for most type references
+ var mergedResolvedSymbol = getMergedSymbol(resolveSymbol(currentNamespace));
+ var next = node.isTypeOf
+ ? getPropertyOfType(getTypeOfSymbol(mergedResolvedSymbol), current.escapedText)
+ : getSymbol(getExportsOfSymbol(mergedResolvedSymbol), current.escapedText, meaning);
+ if (!next) {
+ error(current, ts.Diagnostics.Namespace_0_has_no_exported_member_1, getFullyQualifiedName(currentNamespace), ts.declarationNameToString(current));
+ return links.resolvedType = errorType;
}
- result &= related;
+ getNodeLinks(current).resolvedSymbol = next;
+ getNodeLinks(current.parent).resolvedSymbol = next;
+ currentNamespace = next;
}
- }
- else if (sourceSignatures.length === 1 && targetSignatures.length === 1) {
- // For simple functions (functions with a single signature) we only erase type parameters for
- // the comparable relation. Otherwise, if the source signature is generic, we instantiate it
- // in the context of the target signature before checking the relationship. Ideally we'd do
- // this regardless of the number of signatures, but the potential costs are prohibitive due
- // to the quadratic nature of the logic below.
- var eraseGenerics = relation === comparableRelation || !!compilerOptions.noStrictGenericChecks;
- result = signatureRelatedTo(sourceSignatures[0], targetSignatures[0], eraseGenerics, reportErrors, incompatibleReporter(sourceSignatures[0], targetSignatures[0]));
+ links.resolvedType = resolveImportSymbolType(node, links, currentNamespace, targetMeaning);
}
else {
- outer: for (var _i = 0, targetSignatures_1 = targetSignatures; _i < targetSignatures_1.length; _i++) {
- var t = targetSignatures_1[_i];
- // Only elaborate errors from the first failure
- var shouldElaborateErrors = reportErrors;
- for (var _a = 0, sourceSignatures_1 = sourceSignatures; _a < sourceSignatures_1.length; _a++) {
- var s = sourceSignatures_1[_a];
- var related = signatureRelatedTo(s, t, /*erase*/ true, shouldElaborateErrors, incompatibleReporter(s, t));
- if (related) {
- result &= related;
- resetErrorInfo(saveErrorInfo);
- continue outer;
- }
- shouldElaborateErrors = false;
- }
- if (shouldElaborateErrors) {
- reportError(ts.Diagnostics.Type_0_provides_no_match_for_the_signature_1, typeToString(source), signatureToString(t, /*enclosingDeclaration*/ undefined, /*flags*/ undefined, kind));
- }
- return 0 /* False */;
+ if (moduleSymbol.flags & targetMeaning) {
+ links.resolvedType = resolveImportSymbolType(node, links, moduleSymbol, targetMeaning);
+ }
+ else {
+ var errorMessage = targetMeaning === 111551 /* Value */
+ ? ts.Diagnostics.Module_0_does_not_refer_to_a_value_but_is_used_as_a_value_here
+ : ts.Diagnostics.Module_0_does_not_refer_to_a_type_but_is_used_as_a_type_here_Did_you_mean_typeof_import_0;
+ error(node, errorMessage, node.argument.literal.text);
+ links.resolvedSymbol = unknownSymbol;
+ links.resolvedType = errorType;
}
}
- return result;
}
- function reportIncompatibleCallSignatureReturn(siga, sigb) {
- if (siga.parameters.length === 0 && sigb.parameters.length === 0) {
- return function (source, target) { return reportIncompatibleError(ts.Diagnostics.Call_signatures_with_no_arguments_have_incompatible_return_types_0_and_1, typeToString(source), typeToString(target)); };
- }
- return function (source, target) { return reportIncompatibleError(ts.Diagnostics.Call_signature_return_types_0_and_1_are_incompatible, typeToString(source), typeToString(target)); };
+ return links.resolvedType;
+ }
+ function resolveImportSymbolType(node, links, symbol, meaning) {
+ var resolvedSymbol = resolveSymbol(symbol);
+ links.resolvedSymbol = resolvedSymbol;
+ if (meaning === 111551 /* Value */) {
+ return getTypeOfSymbol(symbol); // intentionally doesn't use resolved symbol so type is cached as expected on the alias
}
- function reportIncompatibleConstructSignatureReturn(siga, sigb) {
- if (siga.parameters.length === 0 && sigb.parameters.length === 0) {
- return function (source, target) { return reportIncompatibleError(ts.Diagnostics.Construct_signatures_with_no_arguments_have_incompatible_return_types_0_and_1, typeToString(source), typeToString(target)); };
+ else {
+ return getTypeReferenceType(node, resolvedSymbol); // getTypeReferenceType doesn't handle aliases - it must get the resolved symbol
+ }
+ }
+ function getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node) {
+ var links = getNodeLinks(node);
+ if (!links.resolvedType) {
+ // Deferred resolution of members is handled by resolveObjectTypeMembers
+ var aliasSymbol = getAliasSymbolForTypeNode(node);
+ if (getMembersOfSymbol(node.symbol).size === 0 && !aliasSymbol) {
+ links.resolvedType = emptyTypeLiteralType;
+ }
+ else {
+ var type = createObjectType(16 /* Anonymous */, node.symbol);
+ type.aliasSymbol = aliasSymbol;
+ type.aliasTypeArguments = getTypeArgumentsForAliasSymbol(aliasSymbol);
+ if (ts.isJSDocTypeLiteral(node) && node.isArrayType) {
+ type = createArrayType(type);
+ }
+ links.resolvedType = type;
}
- return function (source, target) { return reportIncompatibleError(ts.Diagnostics.Construct_signature_return_types_0_and_1_are_incompatible, typeToString(source), typeToString(target)); };
}
- /**
- * See signatureAssignableTo, compareSignaturesIdentical
- */
- function signatureRelatedTo(source, target, erase, reportErrors, incompatibleReporter) {
- return compareSignaturesRelated(erase ? getErasedSignature(source) : source, erase ? getErasedSignature(target) : target, relation === strictSubtypeRelation ? 8 /* StrictArity */ : 0, reportErrors, reportError, incompatibleReporter, isRelatedTo, makeFunctionTypeMapper(reportUnreliableMarkers));
+ return links.resolvedType;
+ }
+ function getAliasSymbolForTypeNode(node) {
+ var host = node.parent;
+ while (ts.isParenthesizedTypeNode(host) || ts.isJSDocTypeExpression(host) || ts.isTypeOperatorNode(host) && host.operator === 142 /* ReadonlyKeyword */) {
+ host = host.parent;
}
- function signaturesIdenticalTo(source, target, kind) {
- var sourceSignatures = getSignaturesOfType(source, kind);
- var targetSignatures = getSignaturesOfType(target, kind);
- if (sourceSignatures.length !== targetSignatures.length) {
- return 0 /* False */;
+ return ts.isTypeAlias(host) ? getSymbolOfNode(host) : undefined;
+ }
+ function getTypeArgumentsForAliasSymbol(symbol) {
+ return symbol ? getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol) : undefined;
+ }
+ function isNonGenericObjectType(type) {
+ return !!(type.flags & 524288 /* Object */) && !isGenericMappedType(type);
+ }
+ function isEmptyObjectTypeOrSpreadsIntoEmptyObject(type) {
+ return isEmptyObjectType(type) || !!(type.flags & (65536 /* Null */ | 32768 /* Undefined */ | 528 /* BooleanLike */ | 296 /* NumberLike */ | 2112 /* BigIntLike */ | 402653316 /* StringLike */ | 1056 /* EnumLike */ | 67108864 /* NonPrimitive */ | 4194304 /* Index */));
+ }
+ function tryMergeUnionOfObjectTypeAndEmptyObject(type, readonly) {
+ if (type.types.length === 2) {
+ var firstType = type.types[0];
+ var secondType = type.types[1];
+ if (ts.every(type.types, isEmptyObjectTypeOrSpreadsIntoEmptyObject)) {
+ return isEmptyObjectType(firstType) ? firstType : isEmptyObjectType(secondType) ? secondType : emptyObjectType;
}
- var result = -1 /* True */;
- for (var i = 0; i < sourceSignatures.length; i++) {
- var related = compareSignaturesIdentical(sourceSignatures[i], targetSignatures[i], /*partialMatch*/ false, /*ignoreThisTypes*/ false, /*ignoreReturnTypes*/ false, isRelatedTo);
- if (!related) {
- return 0 /* False */;
- }
- result &= related;
+ if (isEmptyObjectTypeOrSpreadsIntoEmptyObject(firstType)) {
+ return getAnonymousPartialType(secondType);
+ }
+ if (isEmptyObjectTypeOrSpreadsIntoEmptyObject(secondType)) {
+ return getAnonymousPartialType(firstType);
}
- return result;
}
- function eachPropertyRelatedTo(source, target, kind, reportErrors) {
- var result = -1 /* True */;
- var props = source.flags & 2097152 /* Intersection */ ? getPropertiesOfUnionOrIntersectionType(source) : getPropertiesOfObjectType(source);
- for (var _i = 0, props_2 = props; _i < props_2.length; _i++) {
- var prop = props_2[_i];
- // Skip over ignored JSX and symbol-named members
- if (isIgnoredJsxProperty(source, prop)) {
- continue;
- }
- var nameType = getSymbolLinks(prop).nameType;
- if (nameType && nameType.flags & 8192 /* UniqueESSymbol */) {
- continue;
+ function getAnonymousPartialType(type) {
+ // gets the type as if it had been spread, but where everything in the spread is made optional
+ var members = ts.createSymbolTable();
+ for (var _i = 0, _a = getPropertiesOfType(type); _i < _a.length; _i++) {
+ var prop = _a[_i];
+ if (ts.getDeclarationModifierFlagsFromSymbol(prop) & (8 /* Private */ | 16 /* Protected */)) {
+ // do nothing, skip privates
}
- if (kind === 0 /* String */ || isNumericLiteralName(prop.escapedName)) {
- var related = isRelatedTo(getTypeOfSymbol(prop), target, reportErrors);
- if (!related) {
- if (reportErrors) {
- reportError(ts.Diagnostics.Property_0_is_incompatible_with_index_signature, symbolToString(prop));
- }
- return 0 /* False */;
- }
- result &= related;
+ else if (isSpreadableProperty(prop)) {
+ var isSetonlyAccessor = prop.flags & 65536 /* SetAccessor */ && !(prop.flags & 32768 /* GetAccessor */);
+ var flags = 4 /* Property */ | 16777216 /* Optional */;
+ var result = createSymbol(flags, prop.escapedName, readonly ? 8 /* Readonly */ : 0);
+ result.type = isSetonlyAccessor ? undefinedType : getTypeOfSymbol(prop);
+ result.declarations = prop.declarations;
+ result.nameType = getSymbolLinks(prop).nameType;
+ result.syntheticOrigin = prop;
+ members.set(prop.escapedName, result);
}
}
- return result;
+ var spread = createAnonymousType(type.symbol, members, ts.emptyArray, ts.emptyArray, getIndexInfoOfType(type, 0 /* String */), getIndexInfoOfType(type, 1 /* Number */));
+ spread.objectFlags |= 128 /* ObjectLiteral */ | 1048576 /* ContainsObjectOrArrayLiteral */;
+ return spread;
}
- function indexTypeRelatedTo(sourceType, targetType, reportErrors) {
- var related = isRelatedTo(sourceType, targetType, reportErrors);
- if (!related && reportErrors) {
- reportError(ts.Diagnostics.Index_signatures_are_incompatible);
- }
- return related;
+ }
+ /**
+ * Since the source of spread types are object literals, which are not binary,
+ * this function should be called in a left folding style, with left = previous result of getSpreadType
+ * and right = the new element to be spread.
+ */
+ function getSpreadType(left, right, symbol, objectFlags, readonly) {
+ if (left.flags & 1 /* Any */ || right.flags & 1 /* Any */) {
+ return anyType;
}
- function indexTypesRelatedTo(source, target, kind, sourceIsPrimitive, reportErrors, intersectionState) {
- if (relation === identityRelation) {
- return indexTypesIdenticalTo(source, target, kind);
- }
- var targetType = getIndexTypeOfType(target, kind);
- if (!targetType || targetType.flags & 1 /* Any */ && !sourceIsPrimitive) {
- // Index signature of type any permits assignment from everything but primitives
- return -1 /* True */;
+ if (left.flags & 2 /* Unknown */ || right.flags & 2 /* Unknown */) {
+ return unknownType;
+ }
+ if (left.flags & 131072 /* Never */) {
+ return right;
+ }
+ if (right.flags & 131072 /* Never */) {
+ return left;
+ }
+ if (left.flags & 1048576 /* Union */) {
+ var merged = tryMergeUnionOfObjectTypeAndEmptyObject(left, readonly);
+ if (merged) {
+ return getSpreadType(merged, right, symbol, objectFlags, readonly);
}
- if (isGenericMappedType(source)) {
- // A generic mapped type { [P in K]: T } is related to an index signature { [x: string]: U }
- // if T is related to U.
- return kind === 0 /* String */ ? isRelatedTo(getTemplateTypeFromMappedType(source), targetType, reportErrors) : 0 /* False */;
+ return checkCrossProductUnion([left, right])
+ ? mapType(left, function (t) { return getSpreadType(t, right, symbol, objectFlags, readonly); })
+ : errorType;
+ }
+ if (right.flags & 1048576 /* Union */) {
+ var merged = tryMergeUnionOfObjectTypeAndEmptyObject(right, readonly);
+ if (merged) {
+ return getSpreadType(left, merged, symbol, objectFlags, readonly);
}
- var indexType = getIndexTypeOfType(source, kind) || kind === 1 /* Number */ && getIndexTypeOfType(source, 0 /* String */);
- if (indexType) {
- return indexTypeRelatedTo(indexType, targetType, reportErrors);
+ return checkCrossProductUnion([left, right])
+ ? mapType(right, function (t) { return getSpreadType(left, t, symbol, objectFlags, readonly); })
+ : errorType;
+ }
+ if (right.flags & (528 /* BooleanLike */ | 296 /* NumberLike */ | 2112 /* BigIntLike */ | 402653316 /* StringLike */ | 1056 /* EnumLike */ | 67108864 /* NonPrimitive */ | 4194304 /* Index */)) {
+ return left;
+ }
+ if (isGenericObjectType(left) || isGenericObjectType(right)) {
+ if (isEmptyObjectType(left)) {
+ return right;
}
- if (!(intersectionState & 1 /* Source */) && isObjectTypeWithInferableIndex(source)) {
- // Intersection constituents are never considered to have an inferred index signature
- var related = eachPropertyRelatedTo(source, targetType, kind, reportErrors);
- if (related && kind === 0 /* String */) {
- var numberIndexType = getIndexTypeOfType(source, 1 /* Number */);
- if (numberIndexType) {
- related &= indexTypeRelatedTo(numberIndexType, targetType, reportErrors);
- }
+ // When the left type is an intersection, we may need to merge the last constituent of the
+ // intersection with the right type. For example when the left type is 'T & { a: string }'
+ // and the right type is '{ b: string }' we produce 'T & { a: string, b: string }'.
+ if (left.flags & 2097152 /* Intersection */) {
+ var types = left.types;
+ var lastLeft = types[types.length - 1];
+ if (isNonGenericObjectType(lastLeft) && isNonGenericObjectType(right)) {
+ return getIntersectionType(ts.concatenate(types.slice(0, types.length - 1), [getSpreadType(lastLeft, right, symbol, objectFlags, readonly)]));
}
- return related;
}
- if (reportErrors) {
- reportError(ts.Diagnostics.Index_signature_is_missing_in_type_0, typeToString(source));
- }
- return 0 /* False */;
+ return getIntersectionType([left, right]);
}
- function indexTypesIdenticalTo(source, target, indexKind) {
- var targetInfo = getIndexInfoOfType(target, indexKind);
- var sourceInfo = getIndexInfoOfType(source, indexKind);
- if (!sourceInfo && !targetInfo) {
- return -1 /* True */;
- }
- if (sourceInfo && targetInfo && sourceInfo.isReadonly === targetInfo.isReadonly) {
- return isRelatedTo(sourceInfo.type, targetInfo.type);
- }
- return 0 /* False */;
+ var members = ts.createSymbolTable();
+ var skippedPrivateMembers = new ts.Set();
+ var stringIndexInfo;
+ var numberIndexInfo;
+ if (left === emptyObjectType) {
+ // for the first spread element, left === emptyObjectType, so take the right's string indexer
+ stringIndexInfo = getIndexInfoOfType(right, 0 /* String */);
+ numberIndexInfo = getIndexInfoOfType(right, 1 /* Number */);
}
- function constructorVisibilitiesAreCompatible(sourceSignature, targetSignature, reportErrors) {
- if (!sourceSignature.declaration || !targetSignature.declaration) {
- return true;
- }
- var sourceAccessibility = ts.getSelectedModifierFlags(sourceSignature.declaration, 24 /* NonPublicAccessibilityModifier */);
- var targetAccessibility = ts.getSelectedModifierFlags(targetSignature.declaration, 24 /* NonPublicAccessibilityModifier */);
- // A public, protected and private signature is assignable to a private signature.
- if (targetAccessibility === 8 /* Private */) {
- return true;
- }
- // A public and protected signature is assignable to a protected signature.
- if (targetAccessibility === 16 /* Protected */ && sourceAccessibility !== 8 /* Private */) {
- return true;
- }
- // Only a public signature is assignable to public signature.
- if (targetAccessibility !== 16 /* Protected */ && !sourceAccessibility) {
- return true;
+ else {
+ stringIndexInfo = unionSpreadIndexInfos(getIndexInfoOfType(left, 0 /* String */), getIndexInfoOfType(right, 0 /* String */));
+ numberIndexInfo = unionSpreadIndexInfos(getIndexInfoOfType(left, 1 /* Number */), getIndexInfoOfType(right, 1 /* Number */));
+ }
+ for (var _i = 0, _a = getPropertiesOfType(right); _i < _a.length; _i++) {
+ var rightProp = _a[_i];
+ if (ts.getDeclarationModifierFlagsFromSymbol(rightProp) & (8 /* Private */ | 16 /* Protected */)) {
+ skippedPrivateMembers.add(rightProp.escapedName);
}
- if (reportErrors) {
- reportError(ts.Diagnostics.Cannot_assign_a_0_constructor_type_to_a_1_constructor_type, visibilityToString(sourceAccessibility), visibilityToString(targetAccessibility));
+ else if (isSpreadableProperty(rightProp)) {
+ members.set(rightProp.escapedName, getSpreadSymbol(rightProp, readonly));
}
- return false;
}
- }
- function getBestMatchingType(source, target, isRelatedTo) {
- if (isRelatedTo === void 0) { isRelatedTo = compareTypesAssignable; }
- return findMatchingDiscriminantType(source, target, isRelatedTo, /*skipPartial*/ true) ||
- findMatchingTypeReferenceOrTypeAliasReference(source, target) ||
- findBestTypeForObjectLiteral(source, target) ||
- findBestTypeForInvokable(source, target) ||
- findMostOverlappyType(source, target);
- }
- function discriminateTypeByDiscriminableItems(target, discriminators, related, defaultValue, skipPartial) {
- // undefined=unknown, true=discriminated, false=not discriminated
- // The state of each type progresses from left to right. Discriminated types stop at 'true'.
- var discriminable = target.types.map(function (_) { return undefined; });
- for (var _i = 0, discriminators_1 = discriminators; _i < discriminators_1.length; _i++) {
- var _a = discriminators_1[_i], getDiscriminatingType = _a[0], propertyName = _a[1];
- var targetProp = getUnionOrIntersectionProperty(target, propertyName);
- if (skipPartial && targetProp && ts.getCheckFlags(targetProp) & 16 /* ReadPartial */) {
+ for (var _b = 0, _c = getPropertiesOfType(left); _b < _c.length; _b++) {
+ var leftProp = _c[_b];
+ if (skippedPrivateMembers.has(leftProp.escapedName) || !isSpreadableProperty(leftProp)) {
continue;
}
- var i = 0;
- for (var _b = 0, _c = target.types; _b < _c.length; _b++) {
- var type = _c[_b];
- var targetType = getTypeOfPropertyOfType(type, propertyName);
- if (targetType && related(getDiscriminatingType(), targetType)) {
- discriminable[i] = discriminable[i] === undefined ? true : discriminable[i];
- }
- else {
- discriminable[i] = false;
+ if (members.has(leftProp.escapedName)) {
+ var rightProp = members.get(leftProp.escapedName);
+ var rightType = getTypeOfSymbol(rightProp);
+ if (rightProp.flags & 16777216 /* Optional */) {
+ var declarations = ts.concatenate(leftProp.declarations, rightProp.declarations);
+ var flags = 4 /* Property */ | (leftProp.flags & 16777216 /* Optional */);
+ var result = createSymbol(flags, leftProp.escapedName);
+ result.type = getUnionType([getTypeOfSymbol(leftProp), getTypeWithFacts(rightType, 524288 /* NEUndefined */)]);
+ result.leftSpread = leftProp;
+ result.rightSpread = rightProp;
+ result.declarations = declarations;
+ result.nameType = getSymbolLinks(leftProp).nameType;
+ members.set(leftProp.escapedName, result);
}
- i++;
+ }
+ else {
+ members.set(leftProp.escapedName, getSpreadSymbol(leftProp, readonly));
}
}
- var match = discriminable.indexOf(/*searchElement*/ true);
- // make sure exactly 1 matches before returning it
- return match === -1 || discriminable.indexOf(/*searchElement*/ true, match + 1) !== -1 ? defaultValue : target.types[match];
+ var spread = createAnonymousType(symbol, members, ts.emptyArray, ts.emptyArray, getIndexInfoWithReadonly(stringIndexInfo, readonly), getIndexInfoWithReadonly(numberIndexInfo, readonly));
+ spread.objectFlags |= 128 /* ObjectLiteral */ | 1048576 /* ContainsObjectOrArrayLiteral */ | 1024 /* ContainsSpread */ | objectFlags;
+ return spread;
}
- /**
- * A type is 'weak' if it is an object type with at least one optional property
- * and no required properties, call/construct signatures or index signatures
- */
- function isWeakType(type) {
- if (type.flags & 524288 /* Object */) {
- var resolved = resolveStructuredTypeMembers(type);
- return resolved.callSignatures.length === 0 && resolved.constructSignatures.length === 0 &&
- !resolved.stringIndexInfo && !resolved.numberIndexInfo &&
- resolved.properties.length > 0 &&
- ts.every(resolved.properties, function (p) { return !!(p.flags & 16777216 /* Optional */); });
- }
- if (type.flags & 2097152 /* Intersection */) {
- return ts.every(type.types, isWeakType);
+ /** We approximate own properties as non-methods plus methods that are inside the object literal */
+ function isSpreadableProperty(prop) {
+ return !ts.some(prop.declarations, ts.isPrivateIdentifierPropertyDeclaration) &&
+ (!(prop.flags & (8192 /* Method */ | 32768 /* GetAccessor */ | 65536 /* SetAccessor */)) ||
+ !prop.declarations.some(function (decl) { return ts.isClassLike(decl.parent); }));
+ }
+ function getSpreadSymbol(prop, readonly) {
+ var isSetonlyAccessor = prop.flags & 65536 /* SetAccessor */ && !(prop.flags & 32768 /* GetAccessor */);
+ if (!isSetonlyAccessor && readonly === isReadonlySymbol(prop)) {
+ return prop;
}
- return false;
+ var flags = 4 /* Property */ | (prop.flags & 16777216 /* Optional */);
+ var result = createSymbol(flags, prop.escapedName, readonly ? 8 /* Readonly */ : 0);
+ result.type = isSetonlyAccessor ? undefinedType : getTypeOfSymbol(prop);
+ result.declarations = prop.declarations;
+ result.nameType = getSymbolLinks(prop).nameType;
+ result.syntheticOrigin = prop;
+ return result;
}
- function hasCommonProperties(source, target, isComparingJsxAttributes) {
- for (var _i = 0, _a = getPropertiesOfType(source); _i < _a.length; _i++) {
- var prop = _a[_i];
- if (isKnownProperty(target, prop.escapedName, isComparingJsxAttributes)) {
- return true;
+ function getIndexInfoWithReadonly(info, readonly) {
+ return info && info.isReadonly !== readonly ? createIndexInfo(info.type, readonly, info.declaration) : info;
+ }
+ function createLiteralType(flags, value, symbol) {
+ var type = createType(flags);
+ type.symbol = symbol;
+ type.value = value;
+ return type;
+ }
+ function getFreshTypeOfLiteralType(type) {
+ if (type.flags & 2944 /* Literal */) {
+ if (!type.freshType) {
+ var freshType = createLiteralType(type.flags, type.value, type.symbol);
+ freshType.regularType = type;
+ freshType.freshType = freshType;
+ type.freshType = freshType;
}
+ return type.freshType;
}
- return false;
+ return type;
}
- // Return a type reference where the source type parameter is replaced with the target marker
- // type, and flag the result as a marker type reference.
- function getMarkerTypeReference(type, source, target) {
- var result = createTypeReference(type, ts.map(type.typeParameters, function (t) { return t === source ? target : t; }));
- result.objectFlags |= 8192 /* MarkerType */;
- return result;
+ function getRegularTypeOfLiteralType(type) {
+ return type.flags & 2944 /* Literal */ ? type.regularType :
+ type.flags & 1048576 /* Union */ ? (type.regularType || (type.regularType = getUnionType(ts.sameMap(type.types, getRegularTypeOfLiteralType)))) :
+ type;
}
- function getAliasVariances(symbol) {
- var links = getSymbolLinks(symbol);
- return getVariancesWorker(links.typeParameters, links, function (_links, param, marker) {
- var type = getTypeAliasInstantiation(symbol, instantiateTypes(links.typeParameters, makeUnaryTypeMapper(param, marker)));
- type.aliasTypeArgumentsContainsMarker = true;
- return type;
- });
+ function isFreshLiteralType(type) {
+ return !!(type.flags & 2944 /* Literal */) && type.freshType === type;
}
- // Return an array containing the variance of each type parameter. The variance is effectively
- // a digest of the type comparisons that occur for each type argument when instantiations of the
- // generic type are structurally compared. We infer the variance information by comparing
- // instantiations of the generic type for type arguments with known relations. The function
- // returns the emptyArray singleton when invoked recursively for the given generic type.
- function getVariancesWorker(typeParameters, cache, createMarkerType) {
- if (typeParameters === void 0) { typeParameters = ts.emptyArray; }
- var variances = cache.variances;
- if (!variances) {
- // The emptyArray singleton is used to signal a recursive invocation.
- cache.variances = ts.emptyArray;
- variances = [];
- var _loop_16 = function (tp) {
- var unmeasurable = false;
- var unreliable = false;
- var oldHandler = outofbandVarianceMarkerHandler;
- outofbandVarianceMarkerHandler = function (onlyUnreliable) { return onlyUnreliable ? unreliable = true : unmeasurable = true; };
- // We first compare instantiations where the type parameter is replaced with
- // marker types that have a known subtype relationship. From this we can infer
- // invariance, covariance, contravariance or bivariance.
- var typeWithSuper = createMarkerType(cache, tp, markerSuperType);
- var typeWithSub = createMarkerType(cache, tp, markerSubType);
- var variance = (isTypeAssignableTo(typeWithSub, typeWithSuper) ? 1 /* Covariant */ : 0) |
- (isTypeAssignableTo(typeWithSuper, typeWithSub) ? 2 /* Contravariant */ : 0);
- // If the instantiations appear to be related bivariantly it may be because the
- // type parameter is independent (i.e. it isn't witnessed anywhere in the generic
- // type). To determine this we compare instantiations where the type parameter is
- // replaced with marker types that are known to be unrelated.
- if (variance === 3 /* Bivariant */ && isTypeAssignableTo(createMarkerType(cache, tp, markerOtherType), typeWithSuper)) {
- variance = 4 /* Independent */;
- }
- outofbandVarianceMarkerHandler = oldHandler;
- if (unmeasurable || unreliable) {
- if (unmeasurable) {
- variance |= 8 /* Unmeasurable */;
- }
- if (unreliable) {
- variance |= 16 /* Unreliable */;
- }
- }
- variances.push(variance);
- };
- for (var _i = 0, typeParameters_1 = typeParameters; _i < typeParameters_1.length; _i++) {
- var tp = typeParameters_1[_i];
- _loop_16(tp);
- }
- cache.variances = variances;
+ function getLiteralType(value, enumId, symbol) {
+ // We store all literal types in a single map with keys of the form '#NNN' and '@SSS',
+ // where NNN is the text representation of a numeric literal and SSS are the characters
+ // of a string literal. For literal enum members we use 'EEE#NNN' and 'EEE@SSS', where
+ // EEE is a unique id for the containing enum type.
+ var qualifier = typeof value === "number" ? "#" : typeof value === "string" ? "@" : "n";
+ var key = (enumId ? enumId : "") + qualifier + (typeof value === "object" ? ts.pseudoBigIntToString(value) : value);
+ var type = literalTypes.get(key);
+ if (!type) {
+ var flags = (typeof value === "number" ? 256 /* NumberLiteral */ :
+ typeof value === "string" ? 128 /* StringLiteral */ : 2048 /* BigIntLiteral */) |
+ (enumId ? 1024 /* EnumLiteral */ : 0);
+ literalTypes.set(key, type = createLiteralType(flags, value, symbol));
+ type.regularType = type;
}
- return variances;
+ return type;
}
- function getVariances(type) {
- // Arrays and tuples are known to be covariant, no need to spend time computing this.
- if (type === globalArrayType || type === globalReadonlyArrayType || type.objectFlags & 8 /* Tuple */) {
- return arrayVariances;
+ function getTypeFromLiteralTypeNode(node) {
+ if (node.literal.kind === 103 /* NullKeyword */) {
+ return nullType;
}
- return getVariancesWorker(type.typeParameters, type, getMarkerTypeReference);
+ var links = getNodeLinks(node);
+ if (!links.resolvedType) {
+ links.resolvedType = getRegularTypeOfLiteralType(checkExpression(node.literal));
+ }
+ return links.resolvedType;
}
- // Return true if the given type reference has a 'void' type argument for a covariant type parameter.
- // See comment at call in recursiveTypeRelatedTo for when this case matters.
- function hasCovariantVoidArgument(typeArguments, variances) {
- for (var i = 0; i < variances.length; i++) {
- if ((variances[i] & 7 /* VarianceMask */) === 1 /* Covariant */ && typeArguments[i].flags & 16384 /* Void */) {
- return true;
- }
+ function createUniqueESSymbolType(symbol) {
+ var type = createType(8192 /* UniqueESSymbol */);
+ type.symbol = symbol;
+ type.escapedName = "__@" + type.symbol.escapedName + "@" + getSymbolId(type.symbol);
+ return type;
+ }
+ function getESSymbolLikeTypeForNode(node) {
+ if (ts.isValidESSymbolDeclaration(node)) {
+ var symbol = getSymbolOfNode(node);
+ var links = getSymbolLinks(symbol);
+ return links.uniqueESSymbolType || (links.uniqueESSymbolType = createUniqueESSymbolType(symbol));
}
- return false;
+ return esSymbolType;
}
- function isUnconstrainedTypeParameter(type) {
- return type.flags & 262144 /* TypeParameter */ && !getConstraintOfTypeParameter(type);
+ function getThisType(node) {
+ var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false);
+ var parent = container && container.parent;
+ if (parent && (ts.isClassLike(parent) || parent.kind === 253 /* InterfaceDeclaration */)) {
+ if (!ts.hasSyntacticModifier(container, 32 /* Static */) &&
+ (!ts.isConstructorDeclaration(container) || ts.isNodeDescendantOf(node, container.body))) {
+ return getDeclaredTypeOfClassOrInterface(getSymbolOfNode(parent)).thisType;
+ }
+ }
+ // inside x.prototype = { ... }
+ if (parent && ts.isObjectLiteralExpression(parent) && ts.isBinaryExpression(parent.parent) && ts.getAssignmentDeclarationKind(parent.parent) === 6 /* Prototype */) {
+ return getDeclaredTypeOfClassOrInterface(getSymbolOfNode(parent.parent.left).parent).thisType;
+ }
+ // /** @return {this} */
+ // x.prototype.m = function() { ... }
+ var host = node.flags & 4194304 /* JSDoc */ ? ts.getHostSignatureFromJSDoc(node) : undefined;
+ if (host && ts.isFunctionExpression(host) && ts.isBinaryExpression(host.parent) && ts.getAssignmentDeclarationKind(host.parent) === 3 /* PrototypeProperty */) {
+ return getDeclaredTypeOfClassOrInterface(getSymbolOfNode(host.parent.left).parent).thisType;
+ }
+ // inside constructor function C() { ... }
+ if (isJSConstructor(container) && ts.isNodeDescendantOf(node, container.body)) {
+ return getDeclaredTypeOfClassOrInterface(getSymbolOfNode(container)).thisType;
+ }
+ error(node, ts.Diagnostics.A_this_type_is_available_only_in_a_non_static_member_of_a_class_or_interface);
+ return errorType;
}
- function isNonDeferredTypeReference(type) {
- return !!(ts.getObjectFlags(type) & 4 /* Reference */) && !type.node;
+ function getTypeFromThisTypeNode(node) {
+ var links = getNodeLinks(node);
+ if (!links.resolvedType) {
+ links.resolvedType = getThisType(node);
+ }
+ return links.resolvedType;
}
- function isTypeReferenceWithGenericArguments(type) {
- return isNonDeferredTypeReference(type) && ts.some(getTypeArguments(type), function (t) { return isUnconstrainedTypeParameter(t) || isTypeReferenceWithGenericArguments(t); });
+ function getTypeFromRestTypeNode(node) {
+ return getTypeFromTypeNode(getArrayElementTypeNode(node.type) || node.type);
}
- /**
- * getTypeReferenceId(A) returns "111=0-12=1"
- * where A.id=111 and number.id=12
- */
- function getTypeReferenceId(type, typeParameters, depth) {
- if (depth === void 0) { depth = 0; }
- var result = "" + type.target.id;
- for (var _i = 0, _a = getTypeArguments(type); _i < _a.length; _i++) {
- var t = _a[_i];
- if (isUnconstrainedTypeParameter(t)) {
- var index = typeParameters.indexOf(t);
- if (index < 0) {
- index = typeParameters.length;
- typeParameters.push(t);
+ function getArrayElementTypeNode(node) {
+ switch (node.kind) {
+ case 186 /* ParenthesizedType */:
+ return getArrayElementTypeNode(node.type);
+ case 179 /* TupleType */:
+ if (node.elements.length === 1) {
+ node = node.elements[0];
+ if (node.kind === 181 /* RestType */ || node.kind === 192 /* NamedTupleMember */ && node.dotDotDotToken) {
+ return getArrayElementTypeNode(node.type);
+ }
}
- result += "=" + index;
- }
- else if (depth < 4 && isTypeReferenceWithGenericArguments(t)) {
- result += "<" + getTypeReferenceId(t, typeParameters, depth + 1) + ">";
- }
- else {
- result += "-" + t.id;
- }
+ break;
+ case 178 /* ArrayType */:
+ return node.elementType;
}
- return result;
+ return undefined;
}
- /**
- * To improve caching, the relation key for two generic types uses the target's id plus ids of the type parameters.
- * For other cases, the types ids are used.
- */
- function getRelationKey(source, target, intersectionState, relation) {
- if (relation === identityRelation && source.id > target.id) {
- var temp = source;
- source = target;
- target = temp;
- }
- var postFix = intersectionState ? ":" + intersectionState : "";
- if (isTypeReferenceWithGenericArguments(source) && isTypeReferenceWithGenericArguments(target)) {
- var typeParameters = [];
- return getTypeReferenceId(source, typeParameters) + "," + getTypeReferenceId(target, typeParameters) + postFix;
+ function getTypeFromNamedTupleTypeNode(node) {
+ var links = getNodeLinks(node);
+ return links.resolvedType || (links.resolvedType =
+ node.dotDotDotToken ? getTypeFromRestTypeNode(node) :
+ node.questionToken && strictNullChecks ? getOptionalType(getTypeFromTypeNode(node.type)) :
+ getTypeFromTypeNode(node.type));
+ }
+ function getTypeFromTypeNode(node) {
+ return getConditionalFlowTypeOfType(getTypeFromTypeNodeWorker(node), node);
+ }
+ function getTypeFromTypeNodeWorker(node) {
+ switch (node.kind) {
+ case 128 /* AnyKeyword */:
+ case 303 /* JSDocAllType */:
+ case 304 /* JSDocUnknownType */:
+ return anyType;
+ case 152 /* UnknownKeyword */:
+ return unknownType;
+ case 147 /* StringKeyword */:
+ return stringType;
+ case 144 /* NumberKeyword */:
+ return numberType;
+ case 155 /* BigIntKeyword */:
+ return bigintType;
+ case 131 /* BooleanKeyword */:
+ return booleanType;
+ case 148 /* SymbolKeyword */:
+ return esSymbolType;
+ case 113 /* VoidKeyword */:
+ return voidType;
+ case 150 /* UndefinedKeyword */:
+ return undefinedType;
+ case 103 /* NullKeyword */:
+ // TODO(rbuckton): `NullKeyword` is no longer a `TypeNode`, but we defensively allow it here because of incorrect casts in the Language Service.
+ return nullType;
+ case 141 /* NeverKeyword */:
+ return neverType;
+ case 145 /* ObjectKeyword */:
+ return node.flags & 131072 /* JavaScriptFile */ && !noImplicitAny ? anyType : nonPrimitiveType;
+ case 136 /* IntrinsicKeyword */:
+ return intrinsicMarkerType;
+ case 187 /* ThisType */:
+ case 107 /* ThisKeyword */:
+ // TODO(rbuckton): `ThisKeyword` is no longer a `TypeNode`, but we defensively allow it here because of incorrect casts in the Language Service and because of `isPartOfTypeNode`.
+ return getTypeFromThisTypeNode(node);
+ case 191 /* LiteralType */:
+ return getTypeFromLiteralTypeNode(node);
+ case 173 /* TypeReference */:
+ return getTypeFromTypeReference(node);
+ case 172 /* TypePredicate */:
+ return node.assertsModifier ? voidType : booleanType;
+ case 223 /* ExpressionWithTypeArguments */:
+ return getTypeFromTypeReference(node);
+ case 176 /* TypeQuery */:
+ return getTypeFromTypeQueryNode(node);
+ case 178 /* ArrayType */:
+ case 179 /* TupleType */:
+ return getTypeFromArrayOrTupleTypeNode(node);
+ case 180 /* OptionalType */:
+ return getTypeFromOptionalTypeNode(node);
+ case 182 /* UnionType */:
+ return getTypeFromUnionTypeNode(node);
+ case 183 /* IntersectionType */:
+ return getTypeFromIntersectionTypeNode(node);
+ case 305 /* JSDocNullableType */:
+ return getTypeFromJSDocNullableTypeNode(node);
+ case 307 /* JSDocOptionalType */:
+ return addOptionality(getTypeFromTypeNode(node.type));
+ case 192 /* NamedTupleMember */:
+ return getTypeFromNamedTupleTypeNode(node);
+ case 186 /* ParenthesizedType */:
+ case 306 /* JSDocNonNullableType */:
+ case 301 /* JSDocTypeExpression */:
+ return getTypeFromTypeNode(node.type);
+ case 181 /* RestType */:
+ return getTypeFromRestTypeNode(node);
+ case 309 /* JSDocVariadicType */:
+ return getTypeFromJSDocVariadicType(node);
+ case 174 /* FunctionType */:
+ case 175 /* ConstructorType */:
+ case 177 /* TypeLiteral */:
+ case 312 /* JSDocTypeLiteral */:
+ case 308 /* JSDocFunctionType */:
+ case 313 /* JSDocSignature */:
+ return getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node);
+ case 188 /* TypeOperator */:
+ return getTypeFromTypeOperatorNode(node);
+ case 189 /* IndexedAccessType */:
+ return getTypeFromIndexedAccessTypeNode(node);
+ case 190 /* MappedType */:
+ return getTypeFromMappedTypeNode(node);
+ case 184 /* ConditionalType */:
+ return getTypeFromConditionalTypeNode(node);
+ case 185 /* InferType */:
+ return getTypeFromInferTypeNode(node);
+ case 193 /* TemplateLiteralType */:
+ return getTypeFromTemplateTypeNode(node);
+ case 195 /* ImportType */:
+ return getTypeFromImportTypeNode(node);
+ // This function assumes that an identifier, qualified name, or property access expression is a type expression
+ // Callers should first ensure this by calling `isPartOfTypeNode`
+ // TODO(rbuckton): These aren't valid TypeNodes, but we treat them as such because of `isPartOfTypeNode`, which returns `true` for things that aren't `TypeNode`s.
+ case 78 /* Identifier */:
+ case 157 /* QualifiedName */:
+ case 201 /* PropertyAccessExpression */:
+ var symbol = getSymbolAtLocation(node);
+ return symbol ? getDeclaredTypeOfSymbol(symbol) : errorType;
+ default:
+ return errorType;
}
- return source.id + "," + target.id + postFix;
}
- // Invoke the callback for each underlying property symbol of the given symbol and return the first
- // value that isn't undefined.
- function forEachProperty(prop, callback) {
- if (ts.getCheckFlags(prop) & 6 /* Synthetic */) {
- for (var _i = 0, _a = prop.containingType.types; _i < _a.length; _i++) {
- var t = _a[_i];
- var p = getPropertyOfType(t, prop.escapedName);
- var result = p && forEachProperty(p, callback);
- if (result) {
+ function instantiateList(items, mapper, instantiator) {
+ if (items && items.length) {
+ for (var i = 0; i < items.length; i++) {
+ var item = items[i];
+ var mapped = instantiator(item, mapper);
+ if (item !== mapped) {
+ var result = i === 0 ? [] : items.slice(0, i);
+ result.push(mapped);
+ for (i++; i < items.length; i++) {
+ result.push(instantiator(items[i], mapper));
+ }
return result;
}
}
- return undefined;
}
- return callback(prop);
- }
- // Return the declaring class type of a property or undefined if property not declared in class
- function getDeclaringClass(prop) {
- return prop.parent && prop.parent.flags & 32 /* Class */ ? getDeclaredTypeOfSymbol(getParentOfSymbol(prop)) : undefined;
+ return items;
}
- // Return true if some underlying source property is declared in a class that derives
- // from the given base class.
- function isPropertyInClassDerivedFrom(prop, baseClass) {
- return forEachProperty(prop, function (sp) {
- var sourceClass = getDeclaringClass(sp);
- return sourceClass ? hasBaseType(sourceClass, baseClass) : false;
- });
+ function instantiateTypes(types, mapper) {
+ return instantiateList(types, mapper, instantiateType);
}
- // Return true if source property is a valid override of protected parts of target property.
- function isValidOverrideOf(sourceProp, targetProp) {
- return !forEachProperty(targetProp, function (tp) { return ts.getDeclarationModifierFlagsFromSymbol(tp) & 16 /* Protected */ ?
- !isPropertyInClassDerivedFrom(sourceProp, getDeclaringClass(tp)) : false; });
+ function instantiateSignatures(signatures, mapper) {
+ return instantiateList(signatures, mapper, instantiateSignature);
}
- // Return true if the given class derives from each of the declaring classes of the protected
- // constituents of the given property.
- function isClassDerivedFromDeclaringClasses(checkClass, prop) {
- return forEachProperty(prop, function (p) { return ts.getDeclarationModifierFlagsFromSymbol(p) & 16 /* Protected */ ?
- !hasBaseType(checkClass, getDeclaringClass(p)) : false; }) ? undefined : checkClass;
+ function createTypeMapper(sources, targets) {
+ return sources.length === 1 ? makeUnaryTypeMapper(sources[0], targets ? targets[0] : anyType) : makeArrayTypeMapper(sources, targets);
}
- // Return true if the given type is deeply nested. We consider this to be the case when structural type comparisons
- // for 5 or more occurrences or instantiations of the type have been recorded on the given stack. It is possible,
- // though highly unlikely, for this test to be true in a situation where a chain of instantiations is not infinitely
- // expanding. Effectively, we will generate a false positive when two types are structurally equal to at least 5
- // levels, but unequal at some level beyond that.
- // In addition, this will also detect when an indexed access has been chained off of 5 or more times (which is essentially
- // the dual of the structural comparison), and likewise mark the type as deeply nested, potentially adding false positives
- // for finite but deeply expanding indexed accesses (eg, for `Q[P1][P2][P3][P4][P5]`).
- function isDeeplyNestedType(type, stack, depth) {
- // We track all object types that have an associated symbol (representing the origin of the type)
- if (depth >= 5 && type.flags & 524288 /* Object */ && !isObjectOrArrayLiteralType(type)) {
- var symbol = type.symbol;
- if (symbol) {
- var count = 0;
- for (var i = 0; i < depth; i++) {
- var t = stack[i];
- if (t.flags & 524288 /* Object */ && t.symbol === symbol) {
- count++;
- if (count >= 5)
- return true;
+ function getMappedType(type, mapper) {
+ switch (mapper.kind) {
+ case 0 /* Simple */:
+ return type === mapper.source ? mapper.target : type;
+ case 1 /* Array */:
+ var sources = mapper.sources;
+ var targets = mapper.targets;
+ for (var i = 0; i < sources.length; i++) {
+ if (type === sources[i]) {
+ return targets ? targets[i] : anyType;
}
}
- }
- }
- if (depth >= 5 && type.flags & 8388608 /* IndexedAccess */) {
- var root = getRootObjectTypeFromIndexedAccessChain(type);
- var count = 0;
- for (var i = 0; i < depth; i++) {
- var t = stack[i];
- if (getRootObjectTypeFromIndexedAccessChain(t) === root) {
- count++;
- if (count >= 5)
- return true;
- }
- }
+ return type;
+ case 2 /* Function */:
+ return mapper.func(type);
+ case 3 /* Composite */:
+ case 4 /* Merged */:
+ var t1 = getMappedType(type, mapper.mapper1);
+ return t1 !== type && mapper.kind === 3 /* Composite */ ? instantiateType(t1, mapper.mapper2) : getMappedType(t1, mapper.mapper2);
}
- return false;
+ }
+ function makeUnaryTypeMapper(source, target) {
+ return { kind: 0 /* Simple */, source: source, target: target };
+ }
+ function makeArrayTypeMapper(sources, targets) {
+ return { kind: 1 /* Array */, sources: sources, targets: targets };
+ }
+ function makeFunctionTypeMapper(func) {
+ return { kind: 2 /* Function */, func: func };
+ }
+ function makeCompositeTypeMapper(kind, mapper1, mapper2) {
+ return { kind: kind, mapper1: mapper1, mapper2: mapper2 };
+ }
+ function createTypeEraser(sources) {
+ return createTypeMapper(sources, /*targets*/ undefined);
}
/**
- * Gets the leftmost object type in a chain of indexed accesses, eg, in A[P][Q], returns A
+ * Maps forward-references to later types parameters to the empty object type.
+ * This is used during inference when instantiating type parameter defaults.
*/
- function getRootObjectTypeFromIndexedAccessChain(type) {
- var t = type;
- while (t.flags & 8388608 /* IndexedAccess */) {
- t = t.objectType;
- }
- return t;
+ function createBackreferenceMapper(context, index) {
+ return makeFunctionTypeMapper(function (t) { return ts.findIndex(context.inferences, function (info) { return info.typeParameter === t; }) >= index ? unknownType : t; });
}
- function isPropertyIdenticalTo(sourceProp, targetProp) {
- return compareProperties(sourceProp, targetProp, compareTypesIdentical) !== 0 /* False */;
+ function combineTypeMappers(mapper1, mapper2) {
+ return mapper1 ? makeCompositeTypeMapper(3 /* Composite */, mapper1, mapper2) : mapper2;
}
- function compareProperties(sourceProp, targetProp, compareTypes) {
- // Two members are considered identical when
- // - they are public properties with identical names, optionality, and types,
- // - they are private or protected properties originating in the same declaration and having identical types
- if (sourceProp === targetProp) {
- return -1 /* True */;
- }
- var sourcePropAccessibility = ts.getDeclarationModifierFlagsFromSymbol(sourceProp) & 24 /* NonPublicAccessibilityModifier */;
- var targetPropAccessibility = ts.getDeclarationModifierFlagsFromSymbol(targetProp) & 24 /* NonPublicAccessibilityModifier */;
- if (sourcePropAccessibility !== targetPropAccessibility) {
- return 0 /* False */;
- }
- if (sourcePropAccessibility) {
- if (getTargetSymbol(sourceProp) !== getTargetSymbol(targetProp)) {
- return 0 /* False */;
- }
- }
- else {
- if ((sourceProp.flags & 16777216 /* Optional */) !== (targetProp.flags & 16777216 /* Optional */)) {
- return 0 /* False */;
- }
- }
- if (isReadonlySymbol(sourceProp) !== isReadonlySymbol(targetProp)) {
- return 0 /* False */;
- }
- return compareTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp));
+ function mergeTypeMappers(mapper1, mapper2) {
+ return mapper1 ? makeCompositeTypeMapper(4 /* Merged */, mapper1, mapper2) : mapper2;
}
- function isMatchingSignature(source, target, partialMatch) {
- var sourceParameterCount = getParameterCount(source);
- var targetParameterCount = getParameterCount(target);
- var sourceMinArgumentCount = getMinArgumentCount(source);
- var targetMinArgumentCount = getMinArgumentCount(target);
- var sourceHasRestParameter = hasEffectiveRestParameter(source);
- var targetHasRestParameter = hasEffectiveRestParameter(target);
- // A source signature matches a target signature if the two signatures have the same number of required,
- // optional, and rest parameters.
- if (sourceParameterCount === targetParameterCount &&
- sourceMinArgumentCount === targetMinArgumentCount &&
- sourceHasRestParameter === targetHasRestParameter) {
- return true;
- }
- // A source signature partially matches a target signature if the target signature has no fewer required
- // parameters
- if (partialMatch && sourceMinArgumentCount <= targetMinArgumentCount) {
- return true;
+ function prependTypeMapping(source, target, mapper) {
+ return !mapper ? makeUnaryTypeMapper(source, target) : makeCompositeTypeMapper(4 /* Merged */, makeUnaryTypeMapper(source, target), mapper);
+ }
+ function appendTypeMapping(mapper, source, target) {
+ return !mapper ? makeUnaryTypeMapper(source, target) : makeCompositeTypeMapper(4 /* Merged */, mapper, makeUnaryTypeMapper(source, target));
+ }
+ function getRestrictiveTypeParameter(tp) {
+ return tp.constraint === unknownType ? tp : tp.restrictiveInstantiation || (tp.restrictiveInstantiation = createTypeParameter(tp.symbol),
+ tp.restrictiveInstantiation.constraint = unknownType,
+ tp.restrictiveInstantiation);
+ }
+ function cloneTypeParameter(typeParameter) {
+ var result = createTypeParameter(typeParameter.symbol);
+ result.target = typeParameter;
+ return result;
+ }
+ function instantiateTypePredicate(predicate, mapper) {
+ return createTypePredicate(predicate.kind, predicate.parameterName, predicate.parameterIndex, instantiateType(predicate.type, mapper));
+ }
+ function instantiateSignature(signature, mapper, eraseTypeParameters) {
+ var freshTypeParameters;
+ if (signature.typeParameters && !eraseTypeParameters) {
+ // First create a fresh set of type parameters, then include a mapping from the old to the
+ // new type parameters in the mapper function. Finally store this mapper in the new type
+ // parameters such that we can use it when instantiating constraints.
+ freshTypeParameters = ts.map(signature.typeParameters, cloneTypeParameter);
+ mapper = combineTypeMappers(createTypeMapper(signature.typeParameters, freshTypeParameters), mapper);
+ for (var _i = 0, freshTypeParameters_1 = freshTypeParameters; _i < freshTypeParameters_1.length; _i++) {
+ var tp = freshTypeParameters_1[_i];
+ tp.mapper = mapper;
+ }
}
- return false;
+ // Don't compute resolvedReturnType and resolvedTypePredicate now,
+ // because using `mapper` now could trigger inferences to become fixed. (See `createInferenceContext`.)
+ // See GH#17600.
+ var result = createSignature(signature.declaration, freshTypeParameters, signature.thisParameter && instantiateSymbol(signature.thisParameter, mapper), instantiateList(signature.parameters, mapper, instantiateSymbol),
+ /*resolvedReturnType*/ undefined,
+ /*resolvedTypePredicate*/ undefined, signature.minArgumentCount, signature.flags & 19 /* PropagatingFlags */);
+ result.target = signature;
+ result.mapper = mapper;
+ return result;
}
- /**
- * See signatureRelatedTo, compareSignaturesIdentical
- */
- function compareSignaturesIdentical(source, target, partialMatch, ignoreThisTypes, ignoreReturnTypes, compareTypes) {
- // TODO (drosen): De-duplicate code between related functions.
- if (source === target) {
- return -1 /* True */;
+ function instantiateSymbol(symbol, mapper) {
+ var links = getSymbolLinks(symbol);
+ if (links.type && !couldContainTypeVariables(links.type)) {
+ // If the type of the symbol is already resolved, and if that type could not possibly
+ // be affected by instantiation, simply return the symbol itself.
+ return symbol;
}
- if (!(isMatchingSignature(source, target, partialMatch))) {
- return 0 /* False */;
+ if (ts.getCheckFlags(symbol) & 1 /* Instantiated */) {
+ // If symbol being instantiated is itself a instantiation, fetch the original target and combine the
+ // type mappers. This ensures that original type identities are properly preserved and that aliases
+ // always reference a non-aliases.
+ symbol = links.target;
+ mapper = combineTypeMappers(links.mapper, mapper);
}
- // Check that the two signatures have the same number of type parameters.
- if (ts.length(source.typeParameters) !== ts.length(target.typeParameters)) {
- return 0 /* False */;
+ // Keep the flags from the symbol we're instantiating. Mark that is instantiated, and
+ // also transient so that we can just store data on it directly.
+ var result = createSymbol(symbol.flags, symbol.escapedName, 1 /* Instantiated */ | ts.getCheckFlags(symbol) & (8 /* Readonly */ | 4096 /* Late */ | 16384 /* OptionalParameter */ | 32768 /* RestParameter */));
+ result.declarations = symbol.declarations;
+ result.parent = symbol.parent;
+ result.target = symbol;
+ result.mapper = mapper;
+ if (symbol.valueDeclaration) {
+ result.valueDeclaration = symbol.valueDeclaration;
}
- // Check that type parameter constraints and defaults match. If they do, instantiate the source
- // signature with the type parameters of the target signature and continue the comparison.
- if (target.typeParameters) {
- var mapper = createTypeMapper(source.typeParameters, target.typeParameters);
- for (var i = 0; i < target.typeParameters.length; i++) {
- var s = source.typeParameters[i];
- var t = target.typeParameters[i];
- if (!(s === t || compareTypes(instantiateType(getConstraintFromTypeParameter(s), mapper) || unknownType, getConstraintFromTypeParameter(t) || unknownType) &&
- compareTypes(instantiateType(getDefaultFromTypeParameter(s), mapper) || unknownType, getDefaultFromTypeParameter(t) || unknownType))) {
- return 0 /* False */;
- }
- }
- source = instantiateSignature(source, mapper, /*eraseTypeParameters*/ true);
+ if (links.nameType) {
+ result.nameType = links.nameType;
}
- var result = -1 /* True */;
- if (!ignoreThisTypes) {
- var sourceThisType = getThisTypeOfSignature(source);
- if (sourceThisType) {
- var targetThisType = getThisTypeOfSignature(target);
- if (targetThisType) {
- var related = compareTypes(sourceThisType, targetThisType);
- if (!related) {
- return 0 /* False */;
- }
- result &= related;
- }
+ return result;
+ }
+ function getObjectTypeInstantiation(type, mapper) {
+ var declaration = type.objectFlags & 4 /* Reference */ ? type.node : type.symbol.declarations[0];
+ var links = getNodeLinks(declaration);
+ var target = type.objectFlags & 4 /* Reference */ ? links.resolvedType :
+ type.objectFlags & 64 /* Instantiated */ ? type.target : type;
+ var typeParameters = links.outerTypeParameters;
+ if (!typeParameters) {
+ // The first time an anonymous type is instantiated we compute and store a list of the type
+ // parameters that are in scope (and therefore potentially referenced). For type literals that
+ // aren't the right hand side of a generic type alias declaration we optimize by reducing the
+ // set of type parameters to those that are possibly referenced in the literal.
+ var outerTypeParameters = getOuterTypeParameters(declaration, /*includeThisTypes*/ true);
+ if (isJSConstructor(declaration)) {
+ var templateTagParameters = getTypeParametersFromDeclaration(declaration);
+ outerTypeParameters = ts.addRange(outerTypeParameters, templateTagParameters);
}
+ typeParameters = outerTypeParameters || ts.emptyArray;
+ typeParameters = (target.objectFlags & 4 /* Reference */ || target.symbol.flags & 2048 /* TypeLiteral */) && !target.aliasTypeArguments ?
+ ts.filter(typeParameters, function (tp) { return isTypeParameterPossiblyReferenced(tp, declaration); }) :
+ typeParameters;
+ links.outerTypeParameters = typeParameters;
}
- var targetLen = getParameterCount(target);
- for (var i = 0; i < targetLen; i++) {
- var s = getTypeAtPosition(source, i);
- var t = getTypeAtPosition(target, i);
- var related = compareTypes(t, s);
- if (!related) {
- return 0 /* False */;
+ if (typeParameters.length) {
+ // We are instantiating an anonymous type that has one or more type parameters in scope. Apply the
+ // mapper to the type parameters to produce the effective list of type arguments, and compute the
+ // instantiation cache key from the type IDs of the type arguments.
+ var combinedMapper_1 = combineTypeMappers(type.mapper, mapper);
+ var typeArguments = ts.map(typeParameters, function (t) { return getMappedType(t, combinedMapper_1); });
+ var id = getTypeListId(typeArguments);
+ if (!target.instantiations) {
+ target.instantiations = new ts.Map();
+ target.instantiations.set(getTypeListId(typeParameters), target);
}
- result &= related;
- }
- if (!ignoreReturnTypes) {
- var sourceTypePredicate = getTypePredicateOfSignature(source);
- var targetTypePredicate = getTypePredicateOfSignature(target);
- result &= sourceTypePredicate || targetTypePredicate ?
- compareTypePredicatesIdentical(sourceTypePredicate, targetTypePredicate, compareTypes) :
- compareTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target));
+ var result = target.instantiations.get(id);
+ if (!result) {
+ var newMapper = createTypeMapper(typeParameters, typeArguments);
+ result = target.objectFlags & 4 /* Reference */ ? createDeferredTypeReference(type.target, type.node, newMapper) :
+ target.objectFlags & 32 /* Mapped */ ? instantiateMappedType(target, newMapper) :
+ instantiateAnonymousType(target, newMapper);
+ target.instantiations.set(id, result);
+ }
+ return result;
}
- return result;
+ return type;
}
- function compareTypePredicatesIdentical(source, target, compareTypes) {
- return !(source && target && typePredicateKindsMatch(source, target)) ? 0 /* False */ :
- source.type === target.type ? -1 /* True */ :
- source.type && target.type ? compareTypes(source.type, target.type) :
- 0 /* False */;
+ function maybeTypeParameterReference(node) {
+ return !(node.kind === 157 /* QualifiedName */ ||
+ node.parent.kind === 173 /* TypeReference */ && node.parent.typeArguments && node === node.parent.typeName ||
+ node.parent.kind === 195 /* ImportType */ && node.parent.typeArguments && node === node.parent.qualifier);
}
- function literalTypesWithSameBaseType(types) {
- var commonBaseType;
- for (var _i = 0, types_12 = types; _i < types_12.length; _i++) {
- var t = types_12[_i];
- var baseType = getBaseTypeOfLiteralType(t);
- if (!commonBaseType) {
- commonBaseType = baseType;
- }
- if (baseType === t || baseType !== commonBaseType) {
- return false;
+ function isTypeParameterPossiblyReferenced(tp, node) {
+ // If the type parameter doesn't have exactly one declaration, if there are invening statement blocks
+ // between the node and the type parameter declaration, if the node contains actual references to the
+ // type parameter, or if the node contains type queries, we consider the type parameter possibly referenced.
+ if (tp.symbol && tp.symbol.declarations && tp.symbol.declarations.length === 1) {
+ var container = tp.symbol.declarations[0].parent;
+ for (var n = node; n !== container; n = n.parent) {
+ if (!n || n.kind === 230 /* Block */ || n.kind === 184 /* ConditionalType */ && ts.forEachChild(n.extendsType, containsReference)) {
+ return true;
+ }
}
+ return !!ts.forEachChild(node, containsReference);
}
return true;
- }
- // When the candidate types are all literal types with the same base type, return a union
- // of those literal types. Otherwise, return the leftmost type for which no type to the
- // right is a supertype.
- function getSupertypeOrUnion(types) {
- return literalTypesWithSameBaseType(types) ?
- getUnionType(types) :
- ts.reduceLeft(types, function (s, t) { return isTypeSubtypeOf(s, t) ? t : s; });
- }
- function getCommonSupertype(types) {
- if (!strictNullChecks) {
- return getSupertypeOrUnion(types);
+ function containsReference(node) {
+ switch (node.kind) {
+ case 187 /* ThisType */:
+ return !!tp.isThisType;
+ case 78 /* Identifier */:
+ return !tp.isThisType && ts.isPartOfTypeNode(node) && maybeTypeParameterReference(node) &&
+ getTypeFromTypeNodeWorker(node) === tp; // use worker because we're looking for === equality
+ case 176 /* TypeQuery */:
+ return true;
+ }
+ return !!ts.forEachChild(node, containsReference);
}
- var primaryTypes = ts.filter(types, function (t) { return !(t.flags & 98304 /* Nullable */); });
- return primaryTypes.length ?
- getNullableType(getSupertypeOrUnion(primaryTypes), getFalsyFlagsOfTypes(types) & 98304 /* Nullable */) :
- getUnionType(types, 2 /* Subtype */);
- }
- // Return the leftmost type for which no type to the right is a subtype.
- function getCommonSubtype(types) {
- return ts.reduceLeft(types, function (s, t) { return isTypeSubtypeOf(t, s) ? t : s; });
- }
- function isArrayType(type) {
- return !!(ts.getObjectFlags(type) & 4 /* Reference */) && (type.target === globalArrayType || type.target === globalReadonlyArrayType);
- }
- function isReadonlyArrayType(type) {
- return !!(ts.getObjectFlags(type) & 4 /* Reference */) && type.target === globalReadonlyArrayType;
- }
- function isMutableArrayOrTuple(type) {
- return isArrayType(type) && !isReadonlyArrayType(type) || isTupleType(type) && !type.target.readonly;
- }
- function getElementTypeOfArrayType(type) {
- return isArrayType(type) ? getTypeArguments(type)[0] : undefined;
- }
- function isArrayLikeType(type) {
- // A type is array-like if it is a reference to the global Array or global ReadonlyArray type,
- // or if it is not the undefined or null type and if it is assignable to ReadonlyArray
- return isArrayType(type) || !(type.flags & 98304 /* Nullable */) && isTypeAssignableTo(type, anyReadonlyArrayType);
- }
- function isEmptyArrayLiteralType(type) {
- var elementType = isArrayType(type) ? getTypeArguments(type)[0] : undefined;
- return elementType === undefinedWideningType || elementType === implicitNeverType;
- }
- function isTupleLikeType(type) {
- return isTupleType(type) || !!getPropertyOfType(type, "0");
- }
- function isArrayOrTupleLikeType(type) {
- return isArrayLikeType(type) || isTupleLikeType(type);
}
- function getTupleElementType(type, index) {
- var propType = getTypeOfPropertyOfType(type, "" + index);
- if (propType) {
- return propType;
- }
- if (everyType(type, isTupleType)) {
- return mapType(type, function (t) { return getRestTypeOfTupleType(t) || undefinedType; });
+ function getHomomorphicTypeVariable(type) {
+ var constraintType = getConstraintTypeFromMappedType(type);
+ if (constraintType.flags & 4194304 /* Index */) {
+ var typeVariable = getActualTypeVariable(constraintType.type);
+ if (typeVariable.flags & 262144 /* TypeParameter */) {
+ return typeVariable;
+ }
}
return undefined;
}
- function isNeitherUnitTypeNorNever(type) {
- return !(type.flags & (109440 /* Unit */ | 131072 /* Never */));
+ function instantiateMappedType(type, mapper) {
+ // For a homomorphic mapped type { [P in keyof T]: X }, where T is some type variable, the mapping
+ // operation depends on T as follows:
+ // * If T is a primitive type no mapping is performed and the result is simply T.
+ // * If T is a union type we distribute the mapped type over the union.
+ // * If T is an array we map to an array where the element type has been transformed.
+ // * If T is a tuple we map to a tuple where the element types have been transformed.
+ // * Otherwise we map to an object type where the type of each property has been transformed.
+ // For example, when T is instantiated to a union type A | B, we produce { [P in keyof A]: X } |
+ // { [P in keyof B]: X }, and when when T is instantiated to a union type A | undefined, we produce
+ // { [P in keyof A]: X } | undefined.
+ var typeVariable = getHomomorphicTypeVariable(type);
+ if (typeVariable) {
+ var mappedTypeVariable = instantiateType(typeVariable, mapper);
+ if (typeVariable !== mappedTypeVariable) {
+ return mapType(getReducedType(mappedTypeVariable), function (t) {
+ if (t.flags & (3 /* AnyOrUnknown */ | 58982400 /* InstantiableNonPrimitive */ | 524288 /* Object */ | 2097152 /* Intersection */) && t !== wildcardType && t !== errorType) {
+ if (!type.declaration.nameType) {
+ if (isArrayType(t)) {
+ return instantiateMappedArrayType(t, type, prependTypeMapping(typeVariable, t, mapper));
+ }
+ if (isGenericTupleType(t)) {
+ return instantiateMappedGenericTupleType(t, type, typeVariable, mapper);
+ }
+ if (isTupleType(t)) {
+ return instantiateMappedTupleType(t, type, prependTypeMapping(typeVariable, t, mapper));
+ }
+ }
+ return instantiateAnonymousType(type, prependTypeMapping(typeVariable, t, mapper));
+ }
+ return t;
+ });
+ }
+ }
+ return instantiateAnonymousType(type, mapper);
}
- function isUnitType(type) {
- return !!(type.flags & 109440 /* Unit */);
+ function getModifiedReadonlyState(state, modifiers) {
+ return modifiers & 1 /* IncludeReadonly */ ? true : modifiers & 2 /* ExcludeReadonly */ ? false : state;
}
- function isLiteralType(type) {
- return type.flags & 16 /* Boolean */ ? true :
- type.flags & 1048576 /* Union */ ? type.flags & 1024 /* EnumLiteral */ ? true : ts.every(type.types, isUnitType) :
- isUnitType(type);
+ function instantiateMappedGenericTupleType(tupleType, mappedType, typeVariable, mapper) {
+ // When a tuple type is generic (i.e. when it contains variadic elements), we want to eagerly map the
+ // non-generic elements and defer mapping the generic elements. In order to facilitate this, we transform
+ // M<[A, B?, ...T, ...C[]] into [...M<[A]>, ...M<[B?]>, ...M, ...M] and then rely on tuple type
+ // normalization to resolve the non-generic parts of the resulting tuple.
+ var elementFlags = tupleType.target.elementFlags;
+ var elementTypes = ts.map(getTypeArguments(tupleType), function (t, i) {
+ var singleton = elementFlags[i] & 8 /* Variadic */ ? t :
+ elementFlags[i] & 4 /* Rest */ ? createArrayType(t) :
+ createTupleType([t], [elementFlags[i]]);
+ // The singleton is never a generic tuple type, so it is safe to recurse here.
+ return instantiateMappedType(mappedType, prependTypeMapping(typeVariable, singleton, mapper));
+ });
+ var newReadonly = getModifiedReadonlyState(tupleType.target.readonly, getMappedTypeModifiers(mappedType));
+ return createTupleType(elementTypes, ts.map(elementTypes, function (_) { return 8 /* Variadic */; }), newReadonly);
}
- function getBaseTypeOfLiteralType(type) {
- return type.flags & 1024 /* EnumLiteral */ ? getBaseTypeOfEnumLiteralType(type) :
- type.flags & 128 /* StringLiteral */ ? stringType :
- type.flags & 256 /* NumberLiteral */ ? numberType :
- type.flags & 2048 /* BigIntLiteral */ ? bigintType :
- type.flags & 512 /* BooleanLiteral */ ? booleanType :
- type.flags & 1048576 /* Union */ ? getUnionType(ts.sameMap(type.types, getBaseTypeOfLiteralType)) :
- type;
+ function instantiateMappedArrayType(arrayType, mappedType, mapper) {
+ var elementType = instantiateMappedTypeTemplate(mappedType, numberType, /*isOptional*/ true, mapper);
+ return elementType === errorType ? errorType :
+ createArrayType(elementType, getModifiedReadonlyState(isReadonlyArrayType(arrayType), getMappedTypeModifiers(mappedType)));
}
- function getWidenedLiteralType(type) {
- return type.flags & 1024 /* EnumLiteral */ && isFreshLiteralType(type) ? getBaseTypeOfEnumLiteralType(type) :
- type.flags & 128 /* StringLiteral */ && isFreshLiteralType(type) ? stringType :
- type.flags & 256 /* NumberLiteral */ && isFreshLiteralType(type) ? numberType :
- type.flags & 2048 /* BigIntLiteral */ && isFreshLiteralType(type) ? bigintType :
- type.flags & 512 /* BooleanLiteral */ && isFreshLiteralType(type) ? booleanType :
- type.flags & 1048576 /* Union */ ? getUnionType(ts.sameMap(type.types, getWidenedLiteralType)) :
- type;
+ function instantiateMappedTupleType(tupleType, mappedType, mapper) {
+ var elementFlags = tupleType.target.elementFlags;
+ var elementTypes = ts.map(getTypeArguments(tupleType), function (_, i) {
+ return instantiateMappedTypeTemplate(mappedType, getLiteralType("" + i), !!(elementFlags[i] & 2 /* Optional */), mapper);
+ });
+ var modifiers = getMappedTypeModifiers(mappedType);
+ var newTupleModifiers = modifiers & 4 /* IncludeOptional */ ? ts.map(elementFlags, function (f) { return f & 1 /* Required */ ? 2 /* Optional */ : f; }) :
+ modifiers & 8 /* ExcludeOptional */ ? ts.map(elementFlags, function (f) { return f & 2 /* Optional */ ? 1 /* Required */ : f; }) :
+ elementFlags;
+ var newReadonly = getModifiedReadonlyState(tupleType.target.readonly, modifiers);
+ return ts.contains(elementTypes, errorType) ? errorType :
+ createTupleType(elementTypes, newTupleModifiers, newReadonly, tupleType.target.labeledElementDeclarations);
}
- function getWidenedUniqueESSymbolType(type) {
- return type.flags & 8192 /* UniqueESSymbol */ ? esSymbolType :
- type.flags & 1048576 /* Union */ ? getUnionType(ts.sameMap(type.types, getWidenedUniqueESSymbolType)) :
- type;
+ function instantiateMappedTypeTemplate(type, key, isOptional, mapper) {
+ var templateMapper = appendTypeMapping(mapper, getTypeParameterFromMappedType(type), key);
+ var propType = instantiateType(getTemplateTypeFromMappedType(type.target || type), templateMapper);
+ var modifiers = getMappedTypeModifiers(type);
+ return strictNullChecks && modifiers & 4 /* IncludeOptional */ && !maybeTypeOfKind(propType, 32768 /* Undefined */ | 16384 /* Void */) ? getOptionalType(propType) :
+ strictNullChecks && modifiers & 8 /* ExcludeOptional */ && isOptional ? getTypeWithFacts(propType, 524288 /* NEUndefined */) :
+ propType;
}
- function getWidenedLiteralLikeTypeForContextualType(type, contextualType) {
- if (!isLiteralOfContextualType(type, contextualType)) {
- type = getWidenedUniqueESSymbolType(getWidenedLiteralType(type));
+ function instantiateAnonymousType(type, mapper) {
+ var result = createObjectType(type.objectFlags | 64 /* Instantiated */, type.symbol);
+ if (type.objectFlags & 32 /* Mapped */) {
+ result.declaration = type.declaration;
+ // C.f. instantiateSignature
+ var origTypeParameter = getTypeParameterFromMappedType(type);
+ var freshTypeParameter = cloneTypeParameter(origTypeParameter);
+ result.typeParameter = freshTypeParameter;
+ mapper = combineTypeMappers(makeUnaryTypeMapper(origTypeParameter, freshTypeParameter), mapper);
+ freshTypeParameter.mapper = mapper;
}
- return type;
+ result.target = type;
+ result.mapper = mapper;
+ result.aliasSymbol = type.aliasSymbol;
+ result.aliasTypeArguments = instantiateTypes(type.aliasTypeArguments, mapper);
+ return result;
}
- function getWidenedLiteralLikeTypeForContextualReturnTypeIfNeeded(type, contextualSignatureReturnType, isAsync) {
- if (type && isUnitType(type)) {
- var contextualType = !contextualSignatureReturnType ? undefined :
- isAsync ? getPromisedTypeOfPromise(contextualSignatureReturnType) :
- contextualSignatureReturnType;
- type = getWidenedLiteralLikeTypeForContextualType(type, contextualType);
+ function getConditionalTypeInstantiation(type, mapper) {
+ var root = type.root;
+ if (root.outerTypeParameters) {
+ // We are instantiating a conditional type that has one or more type parameters in scope. Apply the
+ // mapper to the type parameters to produce the effective list of type arguments, and compute the
+ // instantiation cache key from the type IDs of the type arguments.
+ var typeArguments = ts.map(root.outerTypeParameters, function (t) { return getMappedType(t, mapper); });
+ var id = getTypeListId(typeArguments);
+ var result = root.instantiations.get(id);
+ if (!result) {
+ var newMapper = createTypeMapper(root.outerTypeParameters, typeArguments);
+ result = instantiateConditionalType(root, newMapper);
+ root.instantiations.set(id, result);
+ }
+ return result;
}
return type;
}
- function getWidenedLiteralLikeTypeForContextualIterationTypeIfNeeded(type, contextualSignatureReturnType, kind, isAsyncGenerator) {
- if (type && isUnitType(type)) {
- var contextualType = !contextualSignatureReturnType ? undefined :
- getIterationTypeOfGeneratorFunctionReturnType(kind, contextualSignatureReturnType, isAsyncGenerator);
- type = getWidenedLiteralLikeTypeForContextualType(type, contextualType);
+ function instantiateConditionalType(root, mapper) {
+ // Check if we have a conditional type where the check type is a naked type parameter. If so,
+ // the conditional type is distributive over union types and when T is instantiated to a union
+ // type A | B, we produce (A extends U ? X : Y) | (B extends U ? X : Y).
+ if (root.isDistributive) {
+ var checkType_1 = root.checkType;
+ var instantiatedType = getMappedType(checkType_1, mapper);
+ if (checkType_1 !== instantiatedType && instantiatedType.flags & (1048576 /* Union */ | 131072 /* Never */)) {
+ return mapType(instantiatedType, function (t) { return getConditionalType(root, prependTypeMapping(checkType_1, t, mapper)); });
+ }
}
- return type;
- }
- /**
- * Check if a Type was written as a tuple type literal.
- * Prefer using isTupleLikeType() unless the use of `elementTypes`/`getTypeArguments` is required.
- */
- function isTupleType(type) {
- return !!(ts.getObjectFlags(type) & 4 /* Reference */ && type.target.objectFlags & 8 /* Tuple */);
- }
- function getRestTypeOfTupleType(type) {
- return type.target.hasRestElement ? getTypeArguments(type)[type.target.typeParameters.length - 1] : undefined;
+ return getConditionalType(root, mapper);
}
- function getRestArrayTypeOfTupleType(type) {
- var restType = getRestTypeOfTupleType(type);
- return restType && createArrayType(restType);
+ function instantiateType(type, mapper) {
+ if (!(type && mapper && couldContainTypeVariables(type))) {
+ return type;
+ }
+ if (instantiationDepth === 50 || instantiationCount >= 5000000) {
+ // We have reached 50 recursive type instantiations and there is a very high likelyhood we're dealing
+ // with a combination of infinite generic types that perpetually generate new type identities. We stop
+ // the recursion here by yielding the error type.
+ ts.tracing.instant("check" /* Check */, "instantiateType_DepthLimit", { typeId: type.id, instantiationDepth: instantiationDepth, instantiationCount: instantiationCount });
+ error(currentNode, ts.Diagnostics.Type_instantiation_is_excessively_deep_and_possibly_infinite);
+ return errorType;
+ }
+ totalInstantiationCount++;
+ instantiationCount++;
+ instantiationDepth++;
+ var result = instantiateTypeWorker(type, mapper);
+ instantiationDepth--;
+ return result;
}
- function getLengthOfTupleType(type) {
- return getTypeReferenceArity(type) - (type.target.hasRestElement ? 1 : 0);
+ function instantiateTypeWorker(type, mapper) {
+ var flags = type.flags;
+ if (flags & 262144 /* TypeParameter */) {
+ return getMappedType(type, mapper);
+ }
+ if (flags & 524288 /* Object */) {
+ var objectFlags = type.objectFlags;
+ if (objectFlags & (4 /* Reference */ | 16 /* Anonymous */ | 32 /* Mapped */)) {
+ if (objectFlags & 4 /* Reference */ && !(type.node)) {
+ var resolvedTypeArguments = type.resolvedTypeArguments;
+ var newTypeArguments = instantiateTypes(resolvedTypeArguments, mapper);
+ return newTypeArguments !== resolvedTypeArguments ? createNormalizedTypeReference(type.target, newTypeArguments) : type;
+ }
+ return getObjectTypeInstantiation(type, mapper);
+ }
+ return type;
+ }
+ if (flags & 3145728 /* UnionOrIntersection */) {
+ var types = type.types;
+ var newTypes = instantiateTypes(types, mapper);
+ return newTypes === types ? type :
+ flags & 2097152 /* Intersection */ ?
+ getIntersectionType(newTypes, type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper)) :
+ getUnionType(newTypes, 1 /* Literal */, type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper));
+ }
+ if (flags & 4194304 /* Index */) {
+ return getIndexType(instantiateType(type.type, mapper));
+ }
+ if (flags & 134217728 /* TemplateLiteral */) {
+ return getTemplateLiteralType(type.texts, instantiateTypes(type.types, mapper));
+ }
+ if (flags & 268435456 /* StringMapping */) {
+ return getStringMappingType(type.symbol, instantiateType(type.type, mapper));
+ }
+ if (flags & 8388608 /* IndexedAccess */) {
+ return getIndexedAccessType(instantiateType(type.objectType, mapper), instantiateType(type.indexType, mapper), type.noUncheckedIndexedAccessCandidate, /*accessNode*/ undefined, type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper));
+ }
+ if (flags & 16777216 /* Conditional */) {
+ return getConditionalTypeInstantiation(type, combineTypeMappers(type.mapper, mapper));
+ }
+ if (flags & 33554432 /* Substitution */) {
+ var maybeVariable = instantiateType(type.baseType, mapper);
+ if (maybeVariable.flags & 8650752 /* TypeVariable */) {
+ return getSubstitutionType(maybeVariable, instantiateType(type.substitute, mapper));
+ }
+ else {
+ var sub = instantiateType(type.substitute, mapper);
+ if (sub.flags & 3 /* AnyOrUnknown */ || isTypeAssignableTo(getRestrictiveInstantiation(maybeVariable), getRestrictiveInstantiation(sub))) {
+ return maybeVariable;
+ }
+ return sub;
+ }
+ }
+ return type;
}
- function isZeroBigInt(_a) {
- var value = _a.value;
- return value.base10Value === "0";
+ function getPermissiveInstantiation(type) {
+ return type.flags & (131068 /* Primitive */ | 3 /* AnyOrUnknown */ | 131072 /* Never */) ? type :
+ type.permissiveInstantiation || (type.permissiveInstantiation = instantiateType(type, permissiveMapper));
}
- function getFalsyFlagsOfTypes(types) {
- var result = 0;
- for (var _i = 0, types_13 = types; _i < types_13.length; _i++) {
- var t = types_13[_i];
- result |= getFalsyFlags(t);
+ function getRestrictiveInstantiation(type) {
+ if (type.flags & (131068 /* Primitive */ | 3 /* AnyOrUnknown */ | 131072 /* Never */)) {
+ return type;
}
- return result;
+ if (type.restrictiveInstantiation) {
+ return type.restrictiveInstantiation;
+ }
+ type.restrictiveInstantiation = instantiateType(type, restrictiveMapper);
+ // We set the following so we don't attempt to set the restrictive instance of a restrictive instance
+ // which is redundant - we'll produce new type identities, but all type params have already been mapped.
+ // This also gives us a way to detect restrictive instances upon comparisons and _disable_ the "distributeive constraint"
+ // assignability check for them, which is distinctly unsafe, as once you have a restrctive instance, all the type parameters
+ // are constrained to `unknown` and produce tons of false positives/negatives!
+ type.restrictiveInstantiation.restrictiveInstantiation = type.restrictiveInstantiation;
+ return type.restrictiveInstantiation;
}
- // Returns the String, Number, Boolean, StringLiteral, NumberLiteral, BooleanLiteral, Void, Undefined, or Null
- // flags for the string, number, boolean, "", 0, false, void, undefined, or null types respectively. Returns
- // no flags for all other types (including non-falsy literal types).
- function getFalsyFlags(type) {
- return type.flags & 1048576 /* Union */ ? getFalsyFlagsOfTypes(type.types) :
- type.flags & 128 /* StringLiteral */ ? type.value === "" ? 128 /* StringLiteral */ : 0 :
- type.flags & 256 /* NumberLiteral */ ? type.value === 0 ? 256 /* NumberLiteral */ : 0 :
- type.flags & 2048 /* BigIntLiteral */ ? isZeroBigInt(type) ? 2048 /* BigIntLiteral */ : 0 :
- type.flags & 512 /* BooleanLiteral */ ? (type === falseType || type === regularFalseType) ? 512 /* BooleanLiteral */ : 0 :
- type.flags & 117724 /* PossiblyFalsy */;
+ function instantiateIndexInfo(info, mapper) {
+ return info && createIndexInfo(instantiateType(info.type, mapper), info.isReadonly, info.declaration);
}
- function removeDefinitelyFalsyTypes(type) {
- return getFalsyFlags(type) & 117632 /* DefinitelyFalsy */ ?
- filterType(type, function (t) { return !(getFalsyFlags(t) & 117632 /* DefinitelyFalsy */); }) :
- type;
+ // Returns true if the given expression contains (at any level of nesting) a function or arrow expression
+ // that is subject to contextual typing.
+ function isContextSensitive(node) {
+ ts.Debug.assert(node.kind !== 165 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node));
+ switch (node.kind) {
+ case 208 /* FunctionExpression */:
+ case 209 /* ArrowFunction */:
+ case 165 /* MethodDeclaration */:
+ case 251 /* FunctionDeclaration */: // Function declarations can have context when annotated with a jsdoc @type
+ return isContextSensitiveFunctionLikeDeclaration(node);
+ case 200 /* ObjectLiteralExpression */:
+ return ts.some(node.properties, isContextSensitive);
+ case 199 /* ArrayLiteralExpression */:
+ return ts.some(node.elements, isContextSensitive);
+ case 217 /* ConditionalExpression */:
+ return isContextSensitive(node.whenTrue) ||
+ isContextSensitive(node.whenFalse);
+ case 216 /* BinaryExpression */:
+ return (node.operatorToken.kind === 56 /* BarBarToken */ || node.operatorToken.kind === 60 /* QuestionQuestionToken */) &&
+ (isContextSensitive(node.left) || isContextSensitive(node.right));
+ case 288 /* PropertyAssignment */:
+ return isContextSensitive(node.initializer);
+ case 207 /* ParenthesizedExpression */:
+ return isContextSensitive(node.expression);
+ case 281 /* JsxAttributes */:
+ return ts.some(node.properties, isContextSensitive) || ts.isJsxOpeningElement(node.parent) && ts.some(node.parent.parent.children, isContextSensitive);
+ case 280 /* JsxAttribute */: {
+ // If there is no initializer, JSX attribute has a boolean value of true which is not context sensitive.
+ var initializer = node.initializer;
+ return !!initializer && isContextSensitive(initializer);
+ }
+ case 283 /* JsxExpression */: {
+ // It is possible to that node.expression is undefined (e.g )
+ var expression = node.expression;
+ return !!expression && isContextSensitive(expression);
+ }
+ }
+ return false;
}
- function extractDefinitelyFalsyTypes(type) {
- return mapType(type, getDefinitelyFalsyPartOfType);
+ function isContextSensitiveFunctionLikeDeclaration(node) {
+ return (!ts.isFunctionDeclaration(node) || ts.isInJSFile(node) && !!getTypeForDeclarationFromJSDocComment(node)) &&
+ (hasContextSensitiveParameters(node) || hasContextSensitiveReturnExpression(node));
}
- function getDefinitelyFalsyPartOfType(type) {
- return type.flags & 4 /* String */ ? emptyStringType :
- type.flags & 8 /* Number */ ? zeroType :
- type.flags & 64 /* BigInt */ ? zeroBigIntType :
- type === regularFalseType ||
- type === falseType ||
- type.flags & (16384 /* Void */ | 32768 /* Undefined */ | 65536 /* Null */) ||
- type.flags & 128 /* StringLiteral */ && type.value === "" ||
- type.flags & 256 /* NumberLiteral */ && type.value === 0 ||
- type.flags & 2048 /* BigIntLiteral */ && isZeroBigInt(type) ? type :
- neverType;
+ function hasContextSensitiveParameters(node) {
+ // Functions with type parameters are not context sensitive.
+ if (!node.typeParameters) {
+ // Functions with any parameters that lack type annotations are context sensitive.
+ if (ts.some(node.parameters, function (p) { return !ts.getEffectiveTypeAnnotationNode(p); })) {
+ return true;
+ }
+ if (node.kind !== 209 /* ArrowFunction */) {
+ // If the first parameter is not an explicit 'this' parameter, then the function has
+ // an implicit 'this' parameter which is subject to contextual typing.
+ var parameter = ts.firstOrUndefined(node.parameters);
+ if (!(parameter && ts.parameterIsThisKeyword(parameter))) {
+ return true;
+ }
+ }
+ }
+ return false;
}
- /**
- * Add undefined or null or both to a type if they are missing.
- * @param type - type to add undefined and/or null to if not present
- * @param flags - Either TypeFlags.Undefined or TypeFlags.Null, or both
- */
- function getNullableType(type, flags) {
- var missing = (flags & ~type.flags) & (32768 /* Undefined */ | 65536 /* Null */);
- return missing === 0 ? type :
- missing === 32768 /* Undefined */ ? getUnionType([type, undefinedType]) :
- missing === 65536 /* Null */ ? getUnionType([type, nullType]) :
- getUnionType([type, undefinedType, nullType]);
+ function hasContextSensitiveReturnExpression(node) {
+ // TODO(anhans): A block should be context-sensitive if it has a context-sensitive return value.
+ return !node.typeParameters && !ts.getEffectiveReturnTypeNode(node) && !!node.body && node.body.kind !== 230 /* Block */ && isContextSensitive(node.body);
}
- function getOptionalType(type) {
- ts.Debug.assert(strictNullChecks);
- return type.flags & 32768 /* Undefined */ ? type : getUnionType([type, undefinedType]);
+ function isContextSensitiveFunctionOrObjectLiteralMethod(func) {
+ return (ts.isInJSFile(func) && ts.isFunctionDeclaration(func) || isFunctionExpressionOrArrowFunction(func) || ts.isObjectLiteralMethod(func)) &&
+ isContextSensitiveFunctionLikeDeclaration(func);
}
- function getGlobalNonNullableTypeInstantiation(type) {
- if (!deferredGlobalNonNullableTypeAlias) {
- deferredGlobalNonNullableTypeAlias = getGlobalSymbol("NonNullable", 524288 /* TypeAlias */, /*diagnostic*/ undefined) || unknownSymbol;
+ function getTypeWithoutSignatures(type) {
+ if (type.flags & 524288 /* Object */) {
+ var resolved = resolveStructuredTypeMembers(type);
+ if (resolved.constructSignatures.length || resolved.callSignatures.length) {
+ var result = createObjectType(16 /* Anonymous */, type.symbol);
+ result.members = resolved.members;
+ result.properties = resolved.properties;
+ result.callSignatures = ts.emptyArray;
+ result.constructSignatures = ts.emptyArray;
+ return result;
+ }
}
- // Use NonNullable global type alias if available to improve quick info/declaration emit
- if (deferredGlobalNonNullableTypeAlias !== unknownSymbol) {
- return getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [type]);
+ else if (type.flags & 2097152 /* Intersection */) {
+ return getIntersectionType(ts.map(type.types, getTypeWithoutSignatures));
}
- return getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */); // Type alias unavailable, fall back to non-higher-order behavior
+ return type;
}
- function getNonNullableType(type) {
- return strictNullChecks ? getGlobalNonNullableTypeInstantiation(type) : type;
+ // TYPE CHECKING
+ function isTypeIdenticalTo(source, target) {
+ return isTypeRelatedTo(source, target, identityRelation);
}
- function addOptionalTypeMarker(type) {
- return strictNullChecks ? getUnionType([type, optionalType]) : type;
+ function compareTypesIdentical(source, target) {
+ return isTypeRelatedTo(source, target, identityRelation) ? -1 /* True */ : 0 /* False */;
}
- function isNotOptionalTypeMarker(type) {
- return type !== optionalType;
+ function compareTypesAssignable(source, target) {
+ return isTypeRelatedTo(source, target, assignableRelation) ? -1 /* True */ : 0 /* False */;
}
- function removeOptionalTypeMarker(type) {
- return strictNullChecks ? filterType(type, isNotOptionalTypeMarker) : type;
+ function compareTypesSubtypeOf(source, target) {
+ return isTypeRelatedTo(source, target, subtypeRelation) ? -1 /* True */ : 0 /* False */;
}
- function propagateOptionalTypeMarker(type, node, wasOptional) {
- return wasOptional ? ts.isOutermostOptionalChain(node) ? getOptionalType(type) : addOptionalTypeMarker(type) : type;
+ function isTypeSubtypeOf(source, target) {
+ return isTypeRelatedTo(source, target, subtypeRelation);
}
- function getOptionalExpressionType(exprType, expression) {
- return ts.isExpressionOfOptionalChainRoot(expression) ? getNonNullableType(exprType) :
- ts.isOptionalChain(expression) ? removeOptionalTypeMarker(exprType) :
- exprType;
+ function isTypeAssignableTo(source, target) {
+ return isTypeRelatedTo(source, target, assignableRelation);
}
- /**
- * Is source potentially coercible to target type under `==`.
- * Assumes that `source` is a constituent of a union, hence
- * the boolean literal flag on the LHS, but not on the RHS.
- *
- * This does not fully replicate the semantics of `==`. The
- * intention is to catch cases that are clearly not right.
- *
- * Comparing (string | number) to number should not remove the
- * string element.
- *
- * Comparing (string | number) to 1 will remove the string
- * element, though this is not sound. This is a pragmatic
- * choice.
- *
- * @see narrowTypeByEquality
- *
- * @param source
- * @param target
- */
- function isCoercibleUnderDoubleEquals(source, target) {
- return ((source.flags & (8 /* Number */ | 4 /* String */ | 512 /* BooleanLiteral */)) !== 0)
- && ((target.flags & (8 /* Number */ | 4 /* String */ | 16 /* Boolean */)) !== 0);
+ // An object type S is considered to be derived from an object type T if
+ // S is a union type and every constituent of S is derived from T,
+ // T is a union type and S is derived from at least one constituent of T, or
+ // S is a type variable with a base constraint that is derived from T,
+ // T is one of the global types Object and Function and S is a subtype of T, or
+ // T occurs directly or indirectly in an 'extends' clause of S.
+ // Note that this check ignores type parameters and only considers the
+ // inheritance hierarchy.
+ function isTypeDerivedFrom(source, target) {
+ return source.flags & 1048576 /* Union */ ? ts.every(source.types, function (t) { return isTypeDerivedFrom(t, target); }) :
+ target.flags & 1048576 /* Union */ ? ts.some(target.types, function (t) { return isTypeDerivedFrom(source, t); }) :
+ source.flags & 58982400 /* InstantiableNonPrimitive */ ? isTypeDerivedFrom(getBaseConstraintOfType(source) || unknownType, target) :
+ target === globalObjectType ? !!(source.flags & (524288 /* Object */ | 67108864 /* NonPrimitive */)) :
+ target === globalFunctionType ? !!(source.flags & 524288 /* Object */) && isFunctionObjectType(source) :
+ hasBaseType(source, getTargetType(target)) || (isArrayType(target) && !isReadonlyArrayType(target) && isTypeDerivedFrom(source, globalReadonlyArrayType));
}
/**
- * Return true if type was inferred from an object literal, written as an object type literal, or is the shape of a module
- * with no call or construct signatures.
+ * This is *not* a bi-directional relationship.
+ * If one needs to check both directions for comparability, use a second call to this function or 'checkTypeComparableTo'.
+ *
+ * A type S is comparable to a type T if some (but not necessarily all) of the possible values of S are also possible values of T.
+ * It is used to check following cases:
+ * - the types of the left and right sides of equality/inequality operators (`===`, `!==`, `==`, `!=`).
+ * - the types of `case` clause expressions and their respective `switch` expressions.
+ * - the type of an expression in a type assertion with the type being asserted.
*/
- function isObjectTypeWithInferableIndex(type) {
- return type.flags & 2097152 /* Intersection */ ? ts.every(type.types, isObjectTypeWithInferableIndex) :
- !!(type.symbol && (type.symbol.flags & (4096 /* ObjectLiteral */ | 2048 /* TypeLiteral */ | 384 /* Enum */ | 512 /* ValueModule */)) !== 0 &&
- !typeHasCallOrConstructSignatures(type)) || !!(ts.getObjectFlags(type) & 2048 /* ReverseMapped */ && isObjectTypeWithInferableIndex(type.source));
+ function isTypeComparableTo(source, target) {
+ return isTypeRelatedTo(source, target, comparableRelation);
}
- function createSymbolWithType(source, type) {
- var symbol = createSymbol(source.flags, source.escapedName, ts.getCheckFlags(source) & 8 /* Readonly */);
- symbol.declarations = source.declarations;
- symbol.parent = source.parent;
- symbol.type = type;
- symbol.target = source;
- if (source.valueDeclaration) {
- symbol.valueDeclaration = source.valueDeclaration;
- }
- var nameType = getSymbolLinks(source).nameType;
- if (nameType) {
- symbol.nameType = nameType;
- }
- return symbol;
+ function areTypesComparable(type1, type2) {
+ return isTypeComparableTo(type1, type2) || isTypeComparableTo(type2, type1);
}
- function transformTypeOfMembers(type, f) {
- var members = ts.createSymbolTable();
- for (var _i = 0, _a = getPropertiesOfObjectType(type); _i < _a.length; _i++) {
- var property = _a[_i];
- var original = getTypeOfSymbol(property);
- var updated = f(original);
- members.set(property.escapedName, updated === original ? property : createSymbolWithType(property, updated));
- }
- return members;
+ function checkTypeAssignableTo(source, target, errorNode, headMessage, containingMessageChain, errorOutputObject) {
+ return checkTypeRelatedTo(source, target, assignableRelation, errorNode, headMessage, containingMessageChain, errorOutputObject);
}
/**
- * If the the provided object literal is subject to the excess properties check,
- * create a new that is exempt. Recursively mark object literal members as exempt.
- * Leave signatures alone since they are not subject to the check.
+ * Like `checkTypeAssignableTo`, but if it would issue an error, instead performs structural comparisons of the types using the given expression node to
+ * attempt to issue more specific errors on, for example, specific object literal properties or tuple members.
*/
- function getRegularTypeOfObjectLiteral(type) {
- if (!(isObjectLiteralType(type) && ts.getObjectFlags(type) & 32768 /* FreshLiteral */)) {
- return type;
- }
- var regularType = type.regularType;
- if (regularType) {
- return regularType;
+ function checkTypeAssignableToAndOptionallyElaborate(source, target, errorNode, expr, headMessage, containingMessageChain) {
+ return checkTypeRelatedToAndOptionallyElaborate(source, target, assignableRelation, errorNode, expr, headMessage, containingMessageChain, /*errorOutputContainer*/ undefined);
+ }
+ function checkTypeRelatedToAndOptionallyElaborate(source, target, relation, errorNode, expr, headMessage, containingMessageChain, errorOutputContainer) {
+ if (isTypeRelatedTo(source, target, relation))
+ return true;
+ if (!errorNode || !elaborateError(expr, source, target, relation, headMessage, containingMessageChain, errorOutputContainer)) {
+ return checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain, errorOutputContainer);
}
- var resolved = type;
- var members = transformTypeOfMembers(type, getRegularTypeOfObjectLiteral);
- var regularNew = createAnonymousType(resolved.symbol, members, resolved.callSignatures, resolved.constructSignatures, resolved.stringIndexInfo, resolved.numberIndexInfo);
- regularNew.flags = resolved.flags;
- regularNew.objectFlags |= resolved.objectFlags & ~32768 /* FreshLiteral */;
- type.regularType = regularNew;
- return regularNew;
+ return false;
}
- function createWideningContext(parent, propertyName, siblings) {
- return { parent: parent, propertyName: propertyName, siblings: siblings, resolvedProperties: undefined };
+ function isOrHasGenericConditional(type) {
+ return !!(type.flags & 16777216 /* Conditional */ || (type.flags & 2097152 /* Intersection */ && ts.some(type.types, isOrHasGenericConditional)));
}
- function getSiblingsOfContext(context) {
- if (!context.siblings) {
- var siblings_1 = [];
- for (var _i = 0, _a = getSiblingsOfContext(context.parent); _i < _a.length; _i++) {
- var type = _a[_i];
- if (isObjectLiteralType(type)) {
- var prop = getPropertyOfObjectType(type, context.propertyName);
- if (prop) {
- forEachType(getTypeOfSymbol(prop), function (t) {
- siblings_1.push(t);
- });
- }
+ function elaborateError(node, source, target, relation, headMessage, containingMessageChain, errorOutputContainer) {
+ if (!node || isOrHasGenericConditional(target))
+ return false;
+ if (!checkTypeRelatedTo(source, target, relation, /*errorNode*/ undefined)
+ && elaborateDidYouMeanToCallOrConstruct(node, source, target, relation, headMessage, containingMessageChain, errorOutputContainer)) {
+ return true;
+ }
+ switch (node.kind) {
+ case 283 /* JsxExpression */:
+ case 207 /* ParenthesizedExpression */:
+ return elaborateError(node.expression, source, target, relation, headMessage, containingMessageChain, errorOutputContainer);
+ case 216 /* BinaryExpression */:
+ switch (node.operatorToken.kind) {
+ case 62 /* EqualsToken */:
+ case 27 /* CommaToken */:
+ return elaborateError(node.right, source, target, relation, headMessage, containingMessageChain, errorOutputContainer);
}
- }
- context.siblings = siblings_1;
+ break;
+ case 200 /* ObjectLiteralExpression */:
+ return elaborateObjectLiteral(node, source, target, relation, containingMessageChain, errorOutputContainer);
+ case 199 /* ArrayLiteralExpression */:
+ return elaborateArrayLiteral(node, source, target, relation, containingMessageChain, errorOutputContainer);
+ case 281 /* JsxAttributes */:
+ return elaborateJsxComponents(node, source, target, relation, containingMessageChain, errorOutputContainer);
+ case 209 /* ArrowFunction */:
+ return elaborateArrowFunction(node, source, target, relation, containingMessageChain, errorOutputContainer);
}
- return context.siblings;
+ return false;
}
- function getPropertiesOfContext(context) {
- if (!context.resolvedProperties) {
- var names = ts.createMap();
- for (var _i = 0, _a = getSiblingsOfContext(context); _i < _a.length; _i++) {
- var t = _a[_i];
- if (isObjectLiteralType(t) && !(ts.getObjectFlags(t) & 1024 /* ContainsSpread */)) {
- for (var _b = 0, _c = getPropertiesOfType(t); _b < _c.length; _b++) {
- var prop = _c[_b];
- names.set(prop.escapedName, prop);
- }
- }
+ function elaborateDidYouMeanToCallOrConstruct(node, source, target, relation, headMessage, containingMessageChain, errorOutputContainer) {
+ var callSignatures = getSignaturesOfType(source, 0 /* Call */);
+ var constructSignatures = getSignaturesOfType(source, 1 /* Construct */);
+ for (var _i = 0, _a = [constructSignatures, callSignatures]; _i < _a.length; _i++) {
+ var signatures = _a[_i];
+ if (ts.some(signatures, function (s) {
+ var returnType = getReturnTypeOfSignature(s);
+ return !(returnType.flags & (1 /* Any */ | 131072 /* Never */)) && checkTypeRelatedTo(returnType, target, relation, /*errorNode*/ undefined);
+ })) {
+ var resultObj = errorOutputContainer || {};
+ checkTypeAssignableTo(source, target, node, headMessage, containingMessageChain, resultObj);
+ var diagnostic = resultObj.errors[resultObj.errors.length - 1];
+ ts.addRelatedInfo(diagnostic, ts.createDiagnosticForNode(node, signatures === constructSignatures ? ts.Diagnostics.Did_you_mean_to_use_new_with_this_expression : ts.Diagnostics.Did_you_mean_to_call_this_expression));
+ return true;
}
- context.resolvedProperties = ts.arrayFrom(names.values());
}
- return context.resolvedProperties;
+ return false;
}
- function getWidenedProperty(prop, context) {
- if (!(prop.flags & 4 /* Property */)) {
- // Since get accessors already widen their return value there is no need to
- // widen accessor based properties here.
- return prop;
+ function elaborateArrowFunction(node, source, target, relation, containingMessageChain, errorOutputContainer) {
+ // Don't elaborate blocks
+ if (ts.isBlock(node.body)) {
+ return false;
}
- var original = getTypeOfSymbol(prop);
- var propContext = context && createWideningContext(context, prop.escapedName, /*siblings*/ undefined);
- var widened = getWidenedTypeWithContext(original, propContext);
- return widened === original ? prop : createSymbolWithType(prop, widened);
- }
- function getUndefinedProperty(prop) {
- var cached = undefinedProperties.get(prop.escapedName);
- if (cached) {
- return cached;
+ // Or functions with annotated parameter types
+ if (ts.some(node.parameters, ts.hasType)) {
+ return false;
}
- var result = createSymbolWithType(prop, undefinedType);
- result.flags |= 16777216 /* Optional */;
- undefinedProperties.set(prop.escapedName, result);
- return result;
- }
- function getWidenedTypeOfObjectLiteral(type, context) {
- var members = ts.createSymbolTable();
- for (var _i = 0, _a = getPropertiesOfObjectType(type); _i < _a.length; _i++) {
- var prop = _a[_i];
- members.set(prop.escapedName, getWidenedProperty(prop, context));
+ var sourceSig = getSingleCallSignature(source);
+ if (!sourceSig) {
+ return false;
}
- if (context) {
- for (var _b = 0, _c = getPropertiesOfContext(context); _b < _c.length; _b++) {
- var prop = _c[_b];
- if (!members.has(prop.escapedName)) {
- members.set(prop.escapedName, getUndefinedProperty(prop));
+ var targetSignatures = getSignaturesOfType(target, 0 /* Call */);
+ if (!ts.length(targetSignatures)) {
+ return false;
+ }
+ var returnExpression = node.body;
+ var sourceReturn = getReturnTypeOfSignature(sourceSig);
+ var targetReturn = getUnionType(ts.map(targetSignatures, getReturnTypeOfSignature));
+ if (!checkTypeRelatedTo(sourceReturn, targetReturn, relation, /*errorNode*/ undefined)) {
+ var elaborated = returnExpression && elaborateError(returnExpression, sourceReturn, targetReturn, relation, /*headMessage*/ undefined, containingMessageChain, errorOutputContainer);
+ if (elaborated) {
+ return elaborated;
+ }
+ var resultObj = errorOutputContainer || {};
+ checkTypeRelatedTo(sourceReturn, targetReturn, relation, returnExpression, /*message*/ undefined, containingMessageChain, resultObj);
+ if (resultObj.errors) {
+ if (target.symbol && ts.length(target.symbol.declarations)) {
+ ts.addRelatedInfo(resultObj.errors[resultObj.errors.length - 1], ts.createDiagnosticForNode(target.symbol.declarations[0], ts.Diagnostics.The_expected_type_comes_from_the_return_type_of_this_signature));
}
+ if ((ts.getFunctionFlags(node) & 2 /* Async */) === 0
+ // exclude cases where source itself is promisy - this way we don't make a suggestion when relating
+ // an IPromise and a Promise that are slightly different
+ && !getTypeOfPropertyOfType(sourceReturn, "then")
+ && checkTypeRelatedTo(createPromiseType(sourceReturn), targetReturn, relation, /*errorNode*/ undefined)) {
+ ts.addRelatedInfo(resultObj.errors[resultObj.errors.length - 1], ts.createDiagnosticForNode(node, ts.Diagnostics.Did_you_mean_to_mark_this_function_as_async));
+ }
+ return true;
}
}
- var stringIndexInfo = getIndexInfoOfType(type, 0 /* String */);
- var numberIndexInfo = getIndexInfoOfType(type, 1 /* Number */);
- var result = createAnonymousType(type.symbol, members, ts.emptyArray, ts.emptyArray, stringIndexInfo && createIndexInfo(getWidenedType(stringIndexInfo.type), stringIndexInfo.isReadonly), numberIndexInfo && createIndexInfo(getWidenedType(numberIndexInfo.type), numberIndexInfo.isReadonly));
- result.objectFlags |= (ts.getObjectFlags(type) & (16384 /* JSLiteral */ | 2097152 /* NonInferrableType */)); // Retain js literal flag through widening
- return result;
- }
- function getWidenedType(type) {
- return getWidenedTypeWithContext(type, /*context*/ undefined);
+ return false;
}
- function getWidenedTypeWithContext(type, context) {
- if (ts.getObjectFlags(type) & 1572864 /* RequiresWidening */) {
- if (context === undefined && type.widened) {
- return type.widened;
- }
- var result = void 0;
- if (type.flags & (1 /* Any */ | 98304 /* Nullable */)) {
- result = anyType;
- }
- else if (isObjectLiteralType(type)) {
- result = getWidenedTypeOfObjectLiteral(type, context);
- }
- else if (type.flags & 1048576 /* Union */) {
- var unionContext_1 = context || createWideningContext(/*parent*/ undefined, /*propertyName*/ undefined, type.types);
- var widenedTypes = ts.sameMap(type.types, function (t) { return t.flags & 98304 /* Nullable */ ? t : getWidenedTypeWithContext(t, unionContext_1); });
- // Widening an empty object literal transitions from a highly restrictive type to
- // a highly inclusive one. For that reason we perform subtype reduction here if the
- // union includes empty object types (e.g. reducing {} | string to just {}).
- result = getUnionType(widenedTypes, ts.some(widenedTypes, isEmptyObjectType) ? 2 /* Subtype */ : 1 /* Literal */);
- }
- else if (type.flags & 2097152 /* Intersection */) {
- result = getIntersectionType(ts.sameMap(type.types, getWidenedType));
- }
- else if (isArrayType(type) || isTupleType(type)) {
- result = createTypeReference(type.target, ts.sameMap(getTypeArguments(type), getWidenedType));
- }
- if (result && context === undefined) {
- type.widened = result;
+ function getBestMatchIndexedAccessTypeOrUndefined(source, target, nameType) {
+ var idx = getIndexedAccessTypeOrUndefined(target, nameType);
+ if (idx) {
+ return idx;
+ }
+ if (target.flags & 1048576 /* Union */) {
+ var best = getBestMatchingType(source, target);
+ if (best) {
+ return getIndexedAccessTypeOrUndefined(best, nameType);
}
- return result || type;
}
- return type;
+ }
+ function checkExpressionForMutableLocationWithContextualType(next, sourcePropType) {
+ next.contextualType = sourcePropType;
+ try {
+ return checkExpressionForMutableLocation(next, 1 /* Contextual */, sourcePropType);
+ }
+ finally {
+ next.contextualType = undefined;
+ }
}
/**
- * Reports implicit any errors that occur as a result of widening 'null' and 'undefined'
- * to 'any'. A call to reportWideningErrorsInType is normally accompanied by a call to
- * getWidenedType. But in some cases getWidenedType is called without reporting errors
- * (type argument inference is an example).
- *
- * The return value indicates whether an error was in fact reported. The particular circumstances
- * are on a best effort basis. Currently, if the null or undefined that causes widening is inside
- * an object literal property (arbitrarily deeply), this function reports an error. If no error is
- * reported, reportImplicitAnyError is a suitable fallback to report a general error.
+ * For every element returned from the iterator, checks that element to issue an error on a property of that element's type
+ * If that element would issue an error, we first attempt to dive into that element's inner expression and issue a more specific error by recuring into `elaborateError`
+ * Otherwise, we issue an error on _every_ element which fail the assignability check
*/
- function reportWideningErrorsInType(type) {
- var errorReported = false;
- if (ts.getObjectFlags(type) & 524288 /* ContainsWideningType */) {
- if (type.flags & 1048576 /* Union */) {
- if (ts.some(type.types, isEmptyObjectType)) {
- errorReported = true;
+ function elaborateElementwise(iterator, source, target, relation, containingMessageChain, errorOutputContainer) {
+ // Assignability failure - check each prop individually, and if that fails, fall back on the bad error span
+ var reportedError = false;
+ for (var status = iterator.next(); !status.done; status = iterator.next()) {
+ var _a = status.value, prop = _a.errorNode, next = _a.innerExpression, nameType = _a.nameType, errorMessage = _a.errorMessage;
+ var targetPropType = getBestMatchIndexedAccessTypeOrUndefined(source, target, nameType);
+ if (!targetPropType || targetPropType.flags & 8388608 /* IndexedAccess */)
+ continue; // Don't elaborate on indexes on generic variables
+ var sourcePropType = getIndexedAccessTypeOrUndefined(source, nameType);
+ if (sourcePropType && !checkTypeRelatedTo(sourcePropType, targetPropType, relation, /*errorNode*/ undefined)) {
+ var elaborated = next && elaborateError(next, sourcePropType, targetPropType, relation, /*headMessage*/ undefined, containingMessageChain, errorOutputContainer);
+ if (elaborated) {
+ reportedError = true;
}
else {
- for (var _i = 0, _a = type.types; _i < _a.length; _i++) {
- var t = _a[_i];
- if (reportWideningErrorsInType(t)) {
- errorReported = true;
- }
- }
- }
- }
- if (isArrayType(type) || isTupleType(type)) {
- for (var _b = 0, _c = getTypeArguments(type); _b < _c.length; _b++) {
- var t = _c[_b];
- if (reportWideningErrorsInType(t)) {
- errorReported = true;
+ // Issue error on the prop itself, since the prop couldn't elaborate the error
+ var resultObj = errorOutputContainer || {};
+ // Use the expression type, if available
+ var specificSource = next ? checkExpressionForMutableLocationWithContextualType(next, sourcePropType) : sourcePropType;
+ var result = checkTypeRelatedTo(specificSource, targetPropType, relation, prop, errorMessage, containingMessageChain, resultObj);
+ if (result && specificSource !== sourcePropType) {
+ // If for whatever reason the expression type doesn't yield an error, make sure we still issue an error on the sourcePropType
+ checkTypeRelatedTo(sourcePropType, targetPropType, relation, prop, errorMessage, containingMessageChain, resultObj);
}
- }
- }
- if (isObjectLiteralType(type)) {
- for (var _d = 0, _e = getPropertiesOfObjectType(type); _d < _e.length; _d++) {
- var p = _e[_d];
- var t = getTypeOfSymbol(p);
- if (ts.getObjectFlags(t) & 524288 /* ContainsWideningType */) {
- if (!reportWideningErrorsInType(t)) {
- error(p.valueDeclaration, ts.Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type, symbolToString(p), typeToString(getWidenedType(t)));
+ if (resultObj.errors) {
+ var reportedDiag = resultObj.errors[resultObj.errors.length - 1];
+ var propertyName = isTypeUsableAsPropertyName(nameType) ? getPropertyNameFromType(nameType) : undefined;
+ var targetProp = propertyName !== undefined ? getPropertyOfType(target, propertyName) : undefined;
+ var issuedElaboration = false;
+ if (!targetProp) {
+ var indexInfo = isTypeAssignableToKind(nameType, 296 /* NumberLike */) && getIndexInfoOfType(target, 1 /* Number */) ||
+ getIndexInfoOfType(target, 0 /* String */) ||
+ undefined;
+ if (indexInfo && indexInfo.declaration && !ts.getSourceFileOfNode(indexInfo.declaration).hasNoDefaultLib) {
+ issuedElaboration = true;
+ ts.addRelatedInfo(reportedDiag, ts.createDiagnosticForNode(indexInfo.declaration, ts.Diagnostics.The_expected_type_comes_from_this_index_signature));
+ }
+ }
+ if (!issuedElaboration && (targetProp && ts.length(targetProp.declarations) || target.symbol && ts.length(target.symbol.declarations))) {
+ var targetNode = targetProp && ts.length(targetProp.declarations) ? targetProp.declarations[0] : target.symbol.declarations[0];
+ if (!ts.getSourceFileOfNode(targetNode).hasNoDefaultLib) {
+ ts.addRelatedInfo(reportedDiag, ts.createDiagnosticForNode(targetNode, ts.Diagnostics.The_expected_type_comes_from_property_0_which_is_declared_here_on_type_1, propertyName && !(nameType.flags & 8192 /* UniqueESSymbol */) ? ts.unescapeLeadingUnderscores(propertyName) : typeToString(nameType), typeToString(target)));
+ }
}
- errorReported = true;
}
+ reportedError = true;
}
}
}
- return errorReported;
+ return reportedError;
}
- function reportImplicitAny(declaration, type, wideningKind) {
- var typeAsString = typeToString(getWidenedType(type));
- if (ts.isInJSFile(declaration) && !ts.isCheckJsEnabledForFile(ts.getSourceFileOfNode(declaration), compilerOptions)) {
- // Only report implicit any errors/suggestions in TS and ts-check JS files
- return;
- }
- var diagnostic;
- switch (declaration.kind) {
- case 209 /* BinaryExpression */:
- case 159 /* PropertyDeclaration */:
- case 158 /* PropertySignature */:
- diagnostic = noImplicitAny ? ts.Diagnostics.Member_0_implicitly_has_an_1_type : ts.Diagnostics.Member_0_implicitly_has_an_1_type_but_a_better_type_may_be_inferred_from_usage;
- break;
- case 156 /* Parameter */:
- var param = declaration;
- if (ts.isIdentifier(param.name) &&
- (ts.isCallSignatureDeclaration(param.parent) || ts.isMethodSignature(param.parent) || ts.isFunctionTypeNode(param.parent)) &&
- param.parent.parameters.indexOf(param) > -1 &&
- (resolveName(param, param.name.escapedText, 788968 /* Type */, undefined, param.name.escapedText, /*isUse*/ true) ||
- param.name.originalKeywordKind && ts.isTypeNodeKind(param.name.originalKeywordKind))) {
- var newName = "arg" + param.parent.parameters.indexOf(param);
- errorOrSuggestion(noImplicitAny, declaration, ts.Diagnostics.Parameter_has_a_name_but_no_type_Did_you_mean_0_Colon_1, newName, ts.declarationNameToString(param.name));
- return;
- }
- diagnostic = declaration.dotDotDotToken ?
- noImplicitAny ? ts.Diagnostics.Rest_parameter_0_implicitly_has_an_any_type : ts.Diagnostics.Rest_parameter_0_implicitly_has_an_any_type_but_a_better_type_may_be_inferred_from_usage :
- noImplicitAny ? ts.Diagnostics.Parameter_0_implicitly_has_an_1_type : ts.Diagnostics.Parameter_0_implicitly_has_an_1_type_but_a_better_type_may_be_inferred_from_usage;
- break;
- case 191 /* BindingElement */:
- diagnostic = ts.Diagnostics.Binding_element_0_implicitly_has_an_1_type;
- if (!noImplicitAny) {
- // Don't issue a suggestion for binding elements since the codefix doesn't yet support them.
- return;
- }
- break;
- case 300 /* JSDocFunctionType */:
- error(declaration, ts.Diagnostics.Function_type_which_lacks_return_type_annotation_implicitly_has_an_0_return_type, typeAsString);
- return;
- case 244 /* FunctionDeclaration */:
- case 161 /* MethodDeclaration */:
- case 160 /* MethodSignature */:
- case 163 /* GetAccessor */:
- case 164 /* SetAccessor */:
- case 201 /* FunctionExpression */:
- case 202 /* ArrowFunction */:
- if (noImplicitAny && !declaration.name) {
- if (wideningKind === 3 /* GeneratorYield */) {
- error(declaration, ts.Diagnostics.Generator_implicitly_has_yield_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type_annotation, typeAsString);
- }
- else {
- error(declaration, ts.Diagnostics.Function_expression_which_lacks_return_type_annotation_implicitly_has_an_0_return_type, typeAsString);
- }
- return;
- }
- diagnostic = !noImplicitAny ? ts.Diagnostics._0_implicitly_has_an_1_return_type_but_a_better_type_may_be_inferred_from_usage :
- wideningKind === 3 /* GeneratorYield */ ? ts.Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_yield_type :
- ts.Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type;
- break;
- case 186 /* MappedType */:
- if (noImplicitAny) {
- error(declaration, ts.Diagnostics.Mapped_object_type_implicitly_has_an_any_template_type);
+ function generateJsxAttributes(node) {
+ var _i, _a, prop;
+ return __generator(this, function (_b) {
+ switch (_b.label) {
+ case 0:
+ if (!ts.length(node.properties))
+ return [2 /*return*/];
+ _i = 0, _a = node.properties;
+ _b.label = 1;
+ case 1:
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
+ prop = _a[_i];
+ if (ts.isJsxSpreadAttribute(prop))
+ return [3 /*break*/, 3];
+ return [4 /*yield*/, { errorNode: prop.name, innerExpression: prop.initializer, nameType: getLiteralType(ts.idText(prop.name)) }];
+ case 2:
+ _b.sent();
+ _b.label = 3;
+ case 3:
+ _i++;
+ return [3 /*break*/, 1];
+ case 4: return [2 /*return*/];
+ }
+ });
+ }
+ function generateJsxChildren(node, getInvalidTextDiagnostic) {
+ var memberOffset, i, child, nameType, elem;
+ return __generator(this, function (_a) {
+ switch (_a.label) {
+ case 0:
+ if (!ts.length(node.children))
+ return [2 /*return*/];
+ memberOffset = 0;
+ i = 0;
+ _a.label = 1;
+ case 1:
+ if (!(i < node.children.length)) return [3 /*break*/, 5];
+ child = node.children[i];
+ nameType = getLiteralType(i - memberOffset);
+ elem = getElaborationElementForJsxChild(child, nameType, getInvalidTextDiagnostic);
+ if (!elem) return [3 /*break*/, 3];
+ return [4 /*yield*/, elem];
+ case 2:
+ _a.sent();
+ return [3 /*break*/, 4];
+ case 3:
+ memberOffset++;
+ _a.label = 4;
+ case 4:
+ i++;
+ return [3 /*break*/, 1];
+ case 5: return [2 /*return*/];
+ }
+ });
+ }
+ function getElaborationElementForJsxChild(child, nameType, getInvalidTextDiagnostic) {
+ switch (child.kind) {
+ case 283 /* JsxExpression */:
+ // child is of the type of the expression
+ return { errorNode: child, innerExpression: child.expression, nameType: nameType };
+ case 11 /* JsxText */:
+ if (child.containsOnlyTriviaWhiteSpaces) {
+ break; // Whitespace only jsx text isn't real jsx text
}
- return;
+ // child is a string
+ return { errorNode: child, innerExpression: undefined, nameType: nameType, errorMessage: getInvalidTextDiagnostic() };
+ case 273 /* JsxElement */:
+ case 274 /* JsxSelfClosingElement */:
+ case 277 /* JsxFragment */:
+ // child is of type JSX.Element
+ return { errorNode: child, innerExpression: child, nameType: nameType };
default:
- diagnostic = noImplicitAny ? ts.Diagnostics.Variable_0_implicitly_has_an_1_type : ts.Diagnostics.Variable_0_implicitly_has_an_1_type_but_a_better_type_may_be_inferred_from_usage;
+ return ts.Debug.assertNever(child, "Found invalid jsx child");
}
- errorOrSuggestion(noImplicitAny, declaration, diagnostic, ts.declarationNameToString(ts.getNameOfDeclaration(declaration)), typeAsString);
}
- function reportErrorsFromWidening(declaration, type, wideningKind) {
- if (produceDiagnostics && noImplicitAny && ts.getObjectFlags(type) & 524288 /* ContainsWideningType */ && (!wideningKind || !getContextualSignatureForFunctionLikeDeclaration(declaration))) {
- // Report implicit any error within type if possible, otherwise report error on declaration
- if (!reportWideningErrorsInType(type)) {
- reportImplicitAny(declaration, type, wideningKind);
+ function elaborateJsxComponents(node, source, target, relation, containingMessageChain, errorOutputContainer) {
+ var result = elaborateElementwise(generateJsxAttributes(node), source, target, relation, containingMessageChain, errorOutputContainer);
+ var invalidTextDiagnostic;
+ if (ts.isJsxOpeningElement(node.parent) && ts.isJsxElement(node.parent.parent)) {
+ var containingElement = node.parent.parent;
+ var childPropName = getJsxElementChildrenPropertyName(getJsxNamespaceAt(node));
+ var childrenPropName = childPropName === undefined ? "children" : ts.unescapeLeadingUnderscores(childPropName);
+ var childrenNameType = getLiteralType(childrenPropName);
+ var childrenTargetType = getIndexedAccessType(target, childrenNameType);
+ var validChildren = ts.getSemanticJsxChildren(containingElement.children);
+ if (!ts.length(validChildren)) {
+ return result;
}
- }
- }
- function applyToParameterTypes(source, target, callback) {
- var sourceCount = getParameterCount(source);
- var targetCount = getParameterCount(target);
- var sourceRestType = getEffectiveRestType(source);
- var targetRestType = getEffectiveRestType(target);
- var targetNonRestCount = targetRestType ? targetCount - 1 : targetCount;
- var paramCount = sourceRestType ? targetNonRestCount : Math.min(sourceCount, targetNonRestCount);
- var sourceThisType = getThisTypeOfSignature(source);
- if (sourceThisType) {
- var targetThisType = getThisTypeOfSignature(target);
- if (targetThisType) {
- callback(sourceThisType, targetThisType);
+ var moreThanOneRealChildren = ts.length(validChildren) > 1;
+ var arrayLikeTargetParts = filterType(childrenTargetType, isArrayOrTupleLikeType);
+ var nonArrayLikeTargetParts = filterType(childrenTargetType, function (t) { return !isArrayOrTupleLikeType(t); });
+ if (moreThanOneRealChildren) {
+ if (arrayLikeTargetParts !== neverType) {
+ var realSource = createTupleType(checkJsxChildren(containingElement, 0 /* Normal */));
+ var children = generateJsxChildren(containingElement, getInvalidTextualChildDiagnostic);
+ result = elaborateElementwise(children, realSource, arrayLikeTargetParts, relation, containingMessageChain, errorOutputContainer) || result;
+ }
+ else if (!isTypeRelatedTo(getIndexedAccessType(source, childrenNameType), childrenTargetType, relation)) {
+ // arity mismatch
+ result = true;
+ var diag = error(containingElement.openingElement.tagName, ts.Diagnostics.This_JSX_tag_s_0_prop_expects_a_single_child_of_type_1_but_multiple_children_were_provided, childrenPropName, typeToString(childrenTargetType));
+ if (errorOutputContainer && errorOutputContainer.skipLogging) {
+ (errorOutputContainer.errors || (errorOutputContainer.errors = [])).push(diag);
+ }
+ }
}
- }
- for (var i = 0; i < paramCount; i++) {
- callback(getTypeAtPosition(source, i), getTypeAtPosition(target, i));
- }
- if (targetRestType) {
- callback(getRestTypeAtPosition(source, paramCount), targetRestType);
- }
- }
- function applyToReturnTypes(source, target, callback) {
- var sourceTypePredicate = getTypePredicateOfSignature(source);
- var targetTypePredicate = getTypePredicateOfSignature(target);
- if (sourceTypePredicate && targetTypePredicate && typePredicateKindsMatch(sourceTypePredicate, targetTypePredicate) && sourceTypePredicate.type && targetTypePredicate.type) {
- callback(sourceTypePredicate.type, targetTypePredicate.type);
- }
- else {
- callback(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target));
- }
- }
- function createInferenceContext(typeParameters, signature, flags, compareTypes) {
- return createInferenceContextWorker(typeParameters.map(createInferenceInfo), signature, flags, compareTypes || compareTypesAssignable);
- }
- function cloneInferenceContext(context, extraFlags) {
- if (extraFlags === void 0) { extraFlags = 0; }
- return context && createInferenceContextWorker(ts.map(context.inferences, cloneInferenceInfo), context.signature, context.flags | extraFlags, context.compareTypes);
- }
- function createInferenceContextWorker(inferences, signature, flags, compareTypes) {
- var context = {
- inferences: inferences,
- signature: signature,
- flags: flags,
- compareTypes: compareTypes,
- mapper: makeFunctionTypeMapper(function (t) { return mapToInferredType(context, t, /*fix*/ true); }),
- nonFixingMapper: makeFunctionTypeMapper(function (t) { return mapToInferredType(context, t, /*fix*/ false); }),
- };
- return context;
- }
- function mapToInferredType(context, t, fix) {
- var inferences = context.inferences;
- for (var i = 0; i < inferences.length; i++) {
- var inference = inferences[i];
- if (t === inference.typeParameter) {
- if (fix && !inference.isFixed) {
- clearCachedInferences(inferences);
- inference.isFixed = true;
+ else {
+ if (nonArrayLikeTargetParts !== neverType) {
+ var child = validChildren[0];
+ var elem_1 = getElaborationElementForJsxChild(child, childrenNameType, getInvalidTextualChildDiagnostic);
+ if (elem_1) {
+ result = elaborateElementwise((function () { return __generator(this, function (_a) {
+ switch (_a.label) {
+ case 0: return [4 /*yield*/, elem_1];
+ case 1:
+ _a.sent();
+ return [2 /*return*/];
+ }
+ }); })(), source, target, relation, containingMessageChain, errorOutputContainer) || result;
+ }
+ }
+ else if (!isTypeRelatedTo(getIndexedAccessType(source, childrenNameType), childrenTargetType, relation)) {
+ // arity mismatch
+ result = true;
+ var diag = error(containingElement.openingElement.tagName, ts.Diagnostics.This_JSX_tag_s_0_prop_expects_type_1_which_requires_multiple_children_but_only_a_single_child_was_provided, childrenPropName, typeToString(childrenTargetType));
+ if (errorOutputContainer && errorOutputContainer.skipLogging) {
+ (errorOutputContainer.errors || (errorOutputContainer.errors = [])).push(diag);
+ }
}
- return getInferredType(context, i);
}
}
- return t;
- }
- function clearCachedInferences(inferences) {
- for (var _i = 0, inferences_1 = inferences; _i < inferences_1.length; _i++) {
- var inference = inferences_1[_i];
- if (!inference.isFixed) {
- inference.inferredType = undefined;
+ return result;
+ function getInvalidTextualChildDiagnostic() {
+ if (!invalidTextDiagnostic) {
+ var tagNameText = ts.getTextOfNode(node.parent.tagName);
+ var childPropName = getJsxElementChildrenPropertyName(getJsxNamespaceAt(node));
+ var childrenPropName = childPropName === undefined ? "children" : ts.unescapeLeadingUnderscores(childPropName);
+ var childrenTargetType = getIndexedAccessType(target, getLiteralType(childrenPropName));
+ var diagnostic = ts.Diagnostics._0_components_don_t_accept_text_as_child_elements_Text_in_JSX_has_the_type_string_but_the_expected_type_of_1_is_2;
+ invalidTextDiagnostic = __assign(__assign({}, diagnostic), { key: "!!ALREADY FORMATTED!!", message: ts.formatMessage(/*_dummy*/ undefined, diagnostic, tagNameText, childrenPropName, typeToString(childrenTargetType)) });
}
+ return invalidTextDiagnostic;
}
}
- function createInferenceInfo(typeParameter) {
- return {
- typeParameter: typeParameter,
- candidates: undefined,
- contraCandidates: undefined,
- inferredType: undefined,
- priority: undefined,
- topLevel: true,
- isFixed: false
- };
- }
- function cloneInferenceInfo(inference) {
- return {
- typeParameter: inference.typeParameter,
- candidates: inference.candidates && inference.candidates.slice(),
- contraCandidates: inference.contraCandidates && inference.contraCandidates.slice(),
- inferredType: inference.inferredType,
- priority: inference.priority,
- topLevel: inference.topLevel,
- isFixed: inference.isFixed
- };
- }
- function cloneInferredPartOfContext(context) {
- var inferences = ts.filter(context.inferences, hasInferenceCandidates);
- return inferences.length ?
- createInferenceContextWorker(ts.map(inferences, cloneInferenceInfo), context.signature, context.flags, context.compareTypes) :
- undefined;
- }
- function getMapperFromContext(context) {
- return context && context.mapper;
- }
- // Return true if the given type could possibly reference a type parameter for which
- // we perform type inference (i.e. a type parameter of a generic function). We cache
- // results for union and intersection types for performance reasons.
- function couldContainTypeVariables(type) {
- var objectFlags = ts.getObjectFlags(type);
- if (objectFlags & 67108864 /* CouldContainTypeVariablesComputed */) {
- return !!(objectFlags & 134217728 /* CouldContainTypeVariables */);
- }
- var result = !!(type.flags & 63176704 /* Instantiable */ ||
- objectFlags & 4 /* Reference */ && (type.node || ts.forEach(getTypeArguments(type), couldContainTypeVariables)) ||
- objectFlags & 16 /* Anonymous */ && type.symbol && type.symbol.flags & (16 /* Function */ | 8192 /* Method */ | 32 /* Class */ | 2048 /* TypeLiteral */ | 4096 /* ObjectLiteral */) && type.symbol.declarations ||
- objectFlags & (32 /* Mapped */ | 131072 /* ObjectRestType */) ||
- type.flags & 3145728 /* UnionOrIntersection */ && !(type.flags & 1024 /* EnumLiteral */) && ts.some(type.types, couldContainTypeVariables));
- if (type.flags & 3899393 /* ObjectFlagsType */) {
- type.objectFlags |= 67108864 /* CouldContainTypeVariablesComputed */ | (result ? 134217728 /* CouldContainTypeVariables */ : 0);
- }
- return result;
- }
- function isTypeParameterAtTopLevel(type, typeParameter) {
- return !!(type === typeParameter ||
- type.flags & 3145728 /* UnionOrIntersection */ && ts.some(type.types, function (t) { return isTypeParameterAtTopLevel(t, typeParameter); }) ||
- type.flags & 16777216 /* Conditional */ && (isTypeParameterAtTopLevel(getTrueTypeFromConditionalType(type), typeParameter) ||
- isTypeParameterAtTopLevel(getFalseTypeFromConditionalType(type), typeParameter)));
- }
- /** Create an object with properties named in the string literal type. Every property has type `any` */
- function createEmptyObjectTypeFromStringLiteral(type) {
- var members = ts.createSymbolTable();
- forEachType(type, function (t) {
- if (!(t.flags & 128 /* StringLiteral */)) {
- return;
- }
- var name = ts.escapeLeadingUnderscores(t.value);
- var literalProp = createSymbol(4 /* Property */, name);
- literalProp.type = anyType;
- if (t.symbol) {
- literalProp.declarations = t.symbol.declarations;
- literalProp.valueDeclaration = t.symbol.valueDeclaration;
+ function generateLimitedTupleElements(node, target) {
+ var len, i, elem, nameType;
+ return __generator(this, function (_a) {
+ switch (_a.label) {
+ case 0:
+ len = ts.length(node.elements);
+ if (!len)
+ return [2 /*return*/];
+ i = 0;
+ _a.label = 1;
+ case 1:
+ if (!(i < len)) return [3 /*break*/, 4];
+ // Skip elements which do not exist in the target - a length error on the tuple overall is likely better than an error on a mismatched index signature
+ if (isTupleLikeType(target) && !getPropertyOfType(target, ("" + i)))
+ return [3 /*break*/, 3];
+ elem = node.elements[i];
+ if (ts.isOmittedExpression(elem))
+ return [3 /*break*/, 3];
+ nameType = getLiteralType(i);
+ return [4 /*yield*/, { errorNode: elem, innerExpression: elem, nameType: nameType }];
+ case 2:
+ _a.sent();
+ _a.label = 3;
+ case 3:
+ i++;
+ return [3 /*break*/, 1];
+ case 4: return [2 /*return*/];
}
- members.set(name, literalProp);
});
- var indexInfo = type.flags & 4 /* String */ ? createIndexInfo(emptyObjectType, /*isReadonly*/ false) : undefined;
- return createAnonymousType(undefined, members, ts.emptyArray, ts.emptyArray, indexInfo, undefined);
- }
- /**
- * Infer a suitable input type for a homomorphic mapped type { [P in keyof T]: X }. We construct
- * an object type with the same set of properties as the source type, where the type of each
- * property is computed by inferring from the source property type to X for the type
- * variable T[P] (i.e. we treat the type T[P] as the type variable we're inferring for).
- */
- function inferTypeForHomomorphicMappedType(source, target, constraint) {
- var key = source.id + "," + target.id + "," + constraint.id;
- if (reverseMappedCache.has(key)) {
- return reverseMappedCache.get(key);
- }
- reverseMappedCache.set(key, undefined);
- var type = createReverseMappedType(source, target, constraint);
- reverseMappedCache.set(key, type);
- return type;
- }
- // We consider a type to be partially inferable if it isn't marked non-inferable or if it is
- // an object literal type with at least one property of an inferable type. For example, an object
- // literal { a: 123, b: x => true } is marked non-inferable because it contains a context sensitive
- // arrow function, but is considered partially inferable because property 'a' has an inferable type.
- function isPartiallyInferableType(type) {
- return !(ts.getObjectFlags(type) & 2097152 /* NonInferrableType */) ||
- isObjectLiteralType(type) && ts.some(getPropertiesOfType(type), function (prop) { return isPartiallyInferableType(getTypeOfSymbol(prop)); });
}
- function createReverseMappedType(source, target, constraint) {
- // We consider a source type reverse mappable if it has a string index signature or if
- // it has one or more properties and is of a partially inferable type.
- if (!(getIndexInfoOfType(source, 0 /* String */) || getPropertiesOfType(source).length !== 0 && isPartiallyInferableType(source))) {
- return undefined;
+ function elaborateArrayLiteral(node, source, target, relation, containingMessageChain, errorOutputContainer) {
+ if (target.flags & 131068 /* Primitive */)
+ return false;
+ if (isTupleLikeType(source)) {
+ return elaborateElementwise(generateLimitedTupleElements(node, target), source, target, relation, containingMessageChain, errorOutputContainer);
}
- // For arrays and tuples we infer new arrays and tuples where the reverse mapping has been
- // applied to the element type(s).
- if (isArrayType(source)) {
- return createArrayType(inferReverseMappedType(getTypeArguments(source)[0], target, constraint), isReadonlyArrayType(source));
+ // recreate a tuple from the elements, if possible
+ // Since we're re-doing the expression type, we need to reapply the contextual type
+ var oldContext = node.contextualType;
+ node.contextualType = target;
+ try {
+ var tupleizedType = checkArrayLiteral(node, 1 /* Contextual */, /*forceTuple*/ true);
+ node.contextualType = oldContext;
+ if (isTupleLikeType(tupleizedType)) {
+ return elaborateElementwise(generateLimitedTupleElements(node, target), tupleizedType, target, relation, containingMessageChain, errorOutputContainer);
+ }
+ return false;
}
- if (isTupleType(source)) {
- var elementTypes = ts.map(getTypeArguments(source), function (t) { return inferReverseMappedType(t, target, constraint); });
- var minLength = getMappedTypeModifiers(target) & 4 /* IncludeOptional */ ?
- getTypeReferenceArity(source) - (source.target.hasRestElement ? 1 : 0) : source.target.minLength;
- return createTupleType(elementTypes, minLength, source.target.hasRestElement, source.target.readonly, source.target.associatedNames);
+ finally {
+ node.contextualType = oldContext;
}
- // For all other object types we infer a new object type where the reverse mapping has been
- // applied to the type of each property.
- var reversed = createObjectType(2048 /* ReverseMapped */ | 16 /* Anonymous */, /*symbol*/ undefined);
- reversed.source = source;
- reversed.mappedType = target;
- reversed.constraintType = constraint;
- return reversed;
}
- function getTypeOfReverseMappedSymbol(symbol) {
- return inferReverseMappedType(symbol.propertyType, symbol.mappedType, symbol.constraintType);
- }
- function inferReverseMappedType(sourceType, target, constraint) {
- var typeParameter = getIndexedAccessType(constraint.type, getTypeParameterFromMappedType(target));
- var templateType = getTemplateTypeFromMappedType(target);
- var inference = createInferenceInfo(typeParameter);
- inferTypes([inference], sourceType, templateType);
- return getTypeFromInference(inference) || unknownType;
- }
- function getUnmatchedProperties(source, target, requireOptionalProperties, matchDiscriminantProperties) {
- var properties, _i, properties_2, targetProp, sourceProp, targetType, sourceType;
- return __generator(this, function (_a) {
- switch (_a.label) {
+ function generateObjectLiteralElements(node) {
+ var _i, _a, prop, type, _b;
+ return __generator(this, function (_c) {
+ switch (_c.label) {
case 0:
- properties = getPropertiesOfType(target);
- _i = 0, properties_2 = properties;
- _a.label = 1;
+ if (!ts.length(node.properties))
+ return [2 /*return*/];
+ _i = 0, _a = node.properties;
+ _c.label = 1;
case 1:
- if (!(_i < properties_2.length)) return [3 /*break*/, 6];
- targetProp = properties_2[_i];
- // TODO: remove this when we support static private identifier fields and find other solutions to get privateNamesAndStaticFields test to pass
- if (isStaticPrivateIdentifierProperty(targetProp)) {
- return [3 /*break*/, 5];
+ if (!(_i < _a.length)) return [3 /*break*/, 8];
+ prop = _a[_i];
+ if (ts.isSpreadAssignment(prop))
+ return [3 /*break*/, 7];
+ type = getLiteralTypeFromProperty(getSymbolOfNode(prop), 8576 /* StringOrNumberLiteralOrUnique */);
+ if (!type || (type.flags & 131072 /* Never */)) {
+ return [3 /*break*/, 7];
}
- if (!(requireOptionalProperties || !(targetProp.flags & 16777216 /* Optional */ || ts.getCheckFlags(targetProp) & 48 /* Partial */))) return [3 /*break*/, 5];
- sourceProp = getPropertyOfType(source, targetProp.escapedName);
- if (!!sourceProp) return [3 /*break*/, 3];
- return [4 /*yield*/, targetProp];
- case 2:
- _a.sent();
- return [3 /*break*/, 5];
+ _b = prop.kind;
+ switch (_b) {
+ case 168 /* SetAccessor */: return [3 /*break*/, 2];
+ case 167 /* GetAccessor */: return [3 /*break*/, 2];
+ case 165 /* MethodDeclaration */: return [3 /*break*/, 2];
+ case 289 /* ShorthandPropertyAssignment */: return [3 /*break*/, 2];
+ case 288 /* PropertyAssignment */: return [3 /*break*/, 4];
+ }
+ return [3 /*break*/, 6];
+ case 2: return [4 /*yield*/, { errorNode: prop.name, innerExpression: undefined, nameType: type }];
case 3:
- if (!matchDiscriminantProperties) return [3 /*break*/, 5];
- targetType = getTypeOfSymbol(targetProp);
- if (!(targetType.flags & 109440 /* Unit */)) return [3 /*break*/, 5];
- sourceType = getTypeOfSymbol(sourceProp);
- if (!!(sourceType.flags & 1 /* Any */ || getRegularTypeOfLiteralType(sourceType) === getRegularTypeOfLiteralType(targetType))) return [3 /*break*/, 5];
- return [4 /*yield*/, targetProp];
- case 4:
- _a.sent();
- _a.label = 5;
+ _c.sent();
+ return [3 /*break*/, 7];
+ case 4: return [4 /*yield*/, { errorNode: prop.name, innerExpression: prop.initializer, nameType: type, errorMessage: ts.isComputedNonLiteralName(prop.name) ? ts.Diagnostics.Type_of_computed_property_s_value_is_0_which_is_not_assignable_to_type_1 : undefined }];
case 5:
+ _c.sent();
+ return [3 /*break*/, 7];
+ case 6:
+ ts.Debug.assertNever(prop);
+ _c.label = 7;
+ case 7:
_i++;
return [3 /*break*/, 1];
- case 6: return [2 /*return*/];
+ case 8: return [2 /*return*/];
}
});
}
- function getUnmatchedProperty(source, target, requireOptionalProperties, matchDiscriminantProperties) {
- var result = getUnmatchedProperties(source, target, requireOptionalProperties, matchDiscriminantProperties).next();
- if (!result.done)
- return result.value;
- }
- function tupleTypesDefinitelyUnrelated(source, target) {
- return target.target.minLength > source.target.minLength ||
- !getRestTypeOfTupleType(target) && (!!getRestTypeOfTupleType(source) || getLengthOfTupleType(target) < getLengthOfTupleType(source));
- }
- function typesDefinitelyUnrelated(source, target) {
- // Two tuple types with incompatible arities are definitely unrelated.
- // Two object types that each have a property that is unmatched in the other are definitely unrelated.
- return isTupleType(source) && isTupleType(target) && tupleTypesDefinitelyUnrelated(source, target) ||
- !!getUnmatchedProperty(source, target, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ true) &&
- !!getUnmatchedProperty(target, source, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ true);
+ function elaborateObjectLiteral(node, source, target, relation, containingMessageChain, errorOutputContainer) {
+ if (target.flags & 131068 /* Primitive */)
+ return false;
+ return elaborateElementwise(generateObjectLiteralElements(node), source, target, relation, containingMessageChain, errorOutputContainer);
}
- function getTypeFromInference(inference) {
- return inference.candidates ? getUnionType(inference.candidates, 2 /* Subtype */) :
- inference.contraCandidates ? getIntersectionType(inference.contraCandidates) :
- undefined;
+ /**
+ * This is *not* a bi-directional relationship.
+ * If one needs to check both directions for comparability, use a second call to this function or 'isTypeComparableTo'.
+ */
+ function checkTypeComparableTo(source, target, errorNode, headMessage, containingMessageChain) {
+ return checkTypeRelatedTo(source, target, comparableRelation, errorNode, headMessage, containingMessageChain);
}
- function hasSkipDirectInferenceFlag(node) {
- return !!getNodeLinks(node).skipDirectInference;
+ function isSignatureAssignableTo(source, target, ignoreReturnTypes) {
+ return compareSignaturesRelated(source, target, ignoreReturnTypes ? 4 /* IgnoreReturnTypes */ : 0, /*reportErrors*/ false,
+ /*errorReporter*/ undefined, /*errorReporter*/ undefined, compareTypesAssignable, /*reportUnreliableMarkers*/ undefined) !== 0 /* False */;
}
- function isFromInferenceBlockedSource(type) {
- return !!(type.symbol && ts.some(type.symbol.declarations, hasSkipDirectInferenceFlag));
+ /**
+ * Returns true if `s` is `(...args: any[]) => any` or `(this: any, ...args: any[]) => any`
+ */
+ function isAnySignature(s) {
+ return !s.typeParameters && (!s.thisParameter || isTypeAny(getTypeOfParameter(s.thisParameter))) && s.parameters.length === 1 &&
+ signatureHasRestParameter(s) && (getTypeOfParameter(s.parameters[0]) === anyArrayType || isTypeAny(getTypeOfParameter(s.parameters[0]))) &&
+ isTypeAny(getReturnTypeOfSignature(s));
}
- function inferTypes(inferences, originalSource, originalTarget, priority, contravariant) {
- if (priority === void 0) { priority = 0; }
- if (contravariant === void 0) { contravariant = false; }
- var symbolOrTypeStack;
- var visited;
- var bivariant = false;
- var propagationType;
- var inferencePriority = 512 /* MaxValue */;
- var allowComplexConstraintInference = true;
- inferFromTypes(originalSource, originalTarget);
- function inferFromTypes(source, target) {
- if (!couldContainTypeVariables(target)) {
- return;
- }
- if (source === wildcardType) {
- // We are inferring from an 'any' type. We want to infer this type for every type parameter
- // referenced in the target type, so we record it as the propagation type and infer from the
- // target to itself. Then, as we find candidates we substitute the propagation type.
- var savePropagationType = propagationType;
- propagationType = source;
- inferFromTypes(target, target);
- propagationType = savePropagationType;
- return;
- }
- if (source.aliasSymbol && source.aliasTypeArguments && source.aliasSymbol === target.aliasSymbol) {
- // Source and target are types originating in the same generic type alias declaration.
- // Simply infer from source type arguments to target type arguments.
- inferFromTypeArguments(source.aliasTypeArguments, target.aliasTypeArguments, getAliasVariances(source.aliasSymbol));
- return;
- }
- if (source === target && source.flags & 3145728 /* UnionOrIntersection */) {
- // When source and target are the same union or intersection type, just relate each constituent
- // type to itself.
- for (var _i = 0, _a = source.types; _i < _a.length; _i++) {
- var t = _a[_i];
- inferFromTypes(t, t);
- }
- return;
- }
- if (target.flags & 1048576 /* Union */) {
- // First, infer between identically matching source and target constituents and remove the
- // matching types.
- var _b = inferFromMatchingTypes(source.flags & 1048576 /* Union */ ? source.types : [source], target.types, isTypeOrBaseIdenticalTo), tempSources = _b[0], tempTargets = _b[1];
- // Next, infer between closely matching source and target constituents and remove
- // the matching types. Types closely match when they are instantiations of the same
- // object type or instantiations of the same type alias.
- var _c = inferFromMatchingTypes(tempSources, tempTargets, isTypeCloselyMatchedBy), sources = _c[0], targets = _c[1];
- if (targets.length === 0) {
- return;
- }
- target = getUnionType(targets);
- if (sources.length === 0) {
- // All source constituents have been matched and there is nothing further to infer from.
- // However, simply making no inferences is undesirable because it could ultimately mean
- // inferring a type parameter constraint. Instead, make a lower priority inference from
- // the full source to whatever remains in the target. For example, when inferring from
- // string to 'string | T', make a lower priority inference of string for T.
- inferWithPriority(source, target, 1 /* NakedTypeVariable */);
- return;
- }
- source = getUnionType(sources);
- }
- else if (target.flags & 2097152 /* Intersection */ && ts.some(target.types, function (t) { return !!getInferenceInfoForType(t) || (isGenericMappedType(t) && !!getInferenceInfoForType(getHomomorphicTypeVariable(t) || neverType)); })) {
- // We reduce intersection types only when they contain naked type parameters. For example, when
- // inferring from 'string[] & { extra: any }' to 'string[] & T' we want to remove string[] and
- // infer { extra: any } for T. But when inferring to 'string[] & Iterable' we want to keep the
- // string[] on the source side and infer string for T.
- // Likewise, we consider a homomorphic mapped type constrainted to the target type parameter as similar to a "naked type variable"
- // in such scenarios.
- if (!(source.flags & 1048576 /* Union */)) {
- // Infer between identically matching source and target constituents and remove the matching types.
- var _d = inferFromMatchingTypes(source.flags & 2097152 /* Intersection */ ? source.types : [source], target.types, isTypeIdenticalTo), sources = _d[0], targets = _d[1];
- if (sources.length === 0 || targets.length === 0) {
- return;
+ /**
+ * See signatureRelatedTo, compareSignaturesIdentical
+ */
+ function compareSignaturesRelated(source, target, checkMode, reportErrors, errorReporter, incompatibleErrorReporter, compareTypes, reportUnreliableMarkers) {
+ // TODO (drosen): De-duplicate code between related functions.
+ if (source === target) {
+ return -1 /* True */;
+ }
+ if (isAnySignature(target)) {
+ return -1 /* True */;
+ }
+ var targetCount = getParameterCount(target);
+ var sourceHasMoreParameters = !hasEffectiveRestParameter(target) &&
+ (checkMode & 8 /* StrictArity */ ? hasEffectiveRestParameter(source) || getParameterCount(source) > targetCount : getMinArgumentCount(source) > targetCount);
+ if (sourceHasMoreParameters) {
+ return 0 /* False */;
+ }
+ if (source.typeParameters && source.typeParameters !== target.typeParameters) {
+ target = getCanonicalSignature(target);
+ source = instantiateSignatureInContextOf(source, target, /*inferenceContext*/ undefined, compareTypes);
+ }
+ var sourceCount = getParameterCount(source);
+ var sourceRestType = getNonArrayRestType(source);
+ var targetRestType = getNonArrayRestType(target);
+ if (sourceRestType || targetRestType) {
+ void instantiateType(sourceRestType || targetRestType, reportUnreliableMarkers);
+ }
+ if (sourceRestType && targetRestType && sourceCount !== targetCount) {
+ // We're not able to relate misaligned complex rest parameters
+ return 0 /* False */;
+ }
+ var kind = target.declaration ? target.declaration.kind : 0 /* Unknown */;
+ var strictVariance = !(checkMode & 3 /* Callback */) && strictFunctionTypes && kind !== 165 /* MethodDeclaration */ &&
+ kind !== 164 /* MethodSignature */ && kind !== 166 /* Constructor */;
+ var result = -1 /* True */;
+ var sourceThisType = getThisTypeOfSignature(source);
+ if (sourceThisType && sourceThisType !== voidType) {
+ var targetThisType = getThisTypeOfSignature(target);
+ if (targetThisType) {
+ // void sources are assignable to anything.
+ var related = !strictVariance && compareTypes(sourceThisType, targetThisType, /*reportErrors*/ false)
+ || compareTypes(targetThisType, sourceThisType, reportErrors);
+ if (!related) {
+ if (reportErrors) {
+ errorReporter(ts.Diagnostics.The_this_types_of_each_signature_are_incompatible);
}
- source = getIntersectionType(sources);
- target = getIntersectionType(targets);
+ return 0 /* False */;
}
+ result &= related;
}
- else if (target.flags & (8388608 /* IndexedAccess */ | 33554432 /* Substitution */)) {
- target = getActualTypeVariable(target);
- }
- if (target.flags & 8650752 /* TypeVariable */) {
- // If target is a type parameter, make an inference, unless the source type contains
- // the anyFunctionType (the wildcard type that's used to avoid contextually typing functions).
- // Because the anyFunctionType is internal, it should not be exposed to the user by adding
- // it as an inference candidate. Hopefully, a better candidate will come along that does
- // not contain anyFunctionType when we come back to this argument for its second round
- // of inference. Also, we exclude inferences for silentNeverType (which is used as a wildcard
- // when constructing types from type parameters that had no inference candidates).
- if (ts.getObjectFlags(source) & 2097152 /* NonInferrableType */ || source === nonInferrableAnyType || source === silentNeverType ||
- (priority & 32 /* ReturnType */ && (source === autoType || source === autoArrayType)) || isFromInferenceBlockedSource(source)) {
- return;
- }
- var inference = getInferenceInfoForType(target);
- if (inference) {
- if (!inference.isFixed) {
- if (inference.priority === undefined || priority < inference.priority) {
- inference.candidates = undefined;
- inference.contraCandidates = undefined;
- inference.topLevel = true;
- inference.priority = priority;
- }
- if (priority === inference.priority) {
- var candidate = propagationType || source;
- // We make contravariant inferences only if we are in a pure contravariant position,
- // i.e. only if we have not descended into a bivariant position.
- if (contravariant && !bivariant) {
- if (!ts.contains(inference.contraCandidates, candidate)) {
- inference.contraCandidates = ts.append(inference.contraCandidates, candidate);
- clearCachedInferences(inferences);
- }
- }
- else if (!ts.contains(inference.candidates, candidate)) {
- inference.candidates = ts.append(inference.candidates, candidate);
- clearCachedInferences(inferences);
- }
- }
- if (!(priority & 32 /* ReturnType */) && target.flags & 262144 /* TypeParameter */ && inference.topLevel && !isTypeParameterAtTopLevel(originalTarget, target)) {
- inference.topLevel = false;
- clearCachedInferences(inferences);
- }
- }
- inferencePriority = Math.min(inferencePriority, priority);
- return;
+ }
+ var paramCount = sourceRestType || targetRestType ? Math.min(sourceCount, targetCount) : Math.max(sourceCount, targetCount);
+ var restIndex = sourceRestType || targetRestType ? paramCount - 1 : -1;
+ for (var i = 0; i < paramCount; i++) {
+ var sourceType = i === restIndex ? getRestTypeAtPosition(source, i) : tryGetTypeAtPosition(source, i);
+ var targetType = i === restIndex ? getRestTypeAtPosition(target, i) : tryGetTypeAtPosition(target, i);
+ if (sourceType && targetType) {
+ // In order to ensure that any generic type Foo is at least co-variant with respect to T no matter
+ // how Foo uses T, we need to relate parameters bi-variantly (given that parameters are input positions,
+ // they naturally relate only contra-variantly). However, if the source and target parameters both have
+ // function types with a single call signature, we know we are relating two callback parameters. In
+ // that case it is sufficient to only relate the parameters of the signatures co-variantly because,
+ // similar to return values, callback parameters are output positions. This means that a Promise,
+ // where T is used only in callback parameter positions, will be co-variant (as opposed to bi-variant)
+ // with respect to T.
+ var sourceSig = checkMode & 3 /* Callback */ ? undefined : getSingleCallSignature(getNonNullableType(sourceType));
+ var targetSig = checkMode & 3 /* Callback */ ? undefined : getSingleCallSignature(getNonNullableType(targetType));
+ var callbacks = sourceSig && targetSig && !getTypePredicateOfSignature(sourceSig) && !getTypePredicateOfSignature(targetSig) &&
+ (getFalsyFlags(sourceType) & 98304 /* Nullable */) === (getFalsyFlags(targetType) & 98304 /* Nullable */);
+ var related = callbacks ?
+ compareSignaturesRelated(targetSig, sourceSig, (checkMode & 8 /* StrictArity */) | (strictVariance ? 2 /* StrictCallback */ : 1 /* BivariantCallback */), reportErrors, errorReporter, incompatibleErrorReporter, compareTypes, reportUnreliableMarkers) :
+ !(checkMode & 3 /* Callback */) && !strictVariance && compareTypes(sourceType, targetType, /*reportErrors*/ false) || compareTypes(targetType, sourceType, reportErrors);
+ // With strict arity, (x: number | undefined) => void is a subtype of (x?: number | undefined) => void
+ if (related && checkMode & 8 /* StrictArity */ && i >= getMinArgumentCount(source) && i < getMinArgumentCount(target) && compareTypes(sourceType, targetType, /*reportErrors*/ false)) {
+ related = 0 /* False */;
}
- else {
- // Infer to the simplified version of an indexed access, if possible, to (hopefully) expose more bare type parameters to the inference engine
- var simplified = getSimplifiedType(target, /*writing*/ false);
- if (simplified !== target) {
- invokeOnce(source, simplified, inferFromTypes);
- }
- else if (target.flags & 8388608 /* IndexedAccess */) {
- var indexType = getSimplifiedType(target.indexType, /*writing*/ false);
- // Generally simplifications of instantiable indexes are avoided to keep relationship checking correct, however if our target is an access, we can consider
- // that key of that access to be "instantiated", since we're looking to find the infernce goal in any way we can.
- if (indexType.flags & 63176704 /* Instantiable */) {
- var simplified_1 = distributeIndexOverObjectType(getSimplifiedType(target.objectType, /*writing*/ false), indexType, /*writing*/ false);
- if (simplified_1 && simplified_1 !== target) {
- invokeOnce(source, simplified_1, inferFromTypes);
- }
- }
+ if (!related) {
+ if (reportErrors) {
+ errorReporter(ts.Diagnostics.Types_of_parameters_0_and_1_are_incompatible, ts.unescapeLeadingUnderscores(getParameterNameAtPosition(source, i)), ts.unescapeLeadingUnderscores(getParameterNameAtPosition(target, i)));
}
+ return 0 /* False */;
}
+ result &= related;
}
- if (ts.getObjectFlags(source) & 4 /* Reference */ && ts.getObjectFlags(target) & 4 /* Reference */ && (source.target === target.target || isArrayType(source) && isArrayType(target)) &&
- !(source.node && target.node)) {
- // If source and target are references to the same generic type, infer from type arguments
- inferFromTypeArguments(getTypeArguments(source), getTypeArguments(target), getVariances(source.target));
- }
- else if (source.flags & 4194304 /* Index */ && target.flags & 4194304 /* Index */) {
- contravariant = !contravariant;
- inferFromTypes(source.type, target.type);
- contravariant = !contravariant;
- }
- else if ((isLiteralType(source) || source.flags & 4 /* String */) && target.flags & 4194304 /* Index */) {
- var empty = createEmptyObjectTypeFromStringLiteral(source);
- contravariant = !contravariant;
- inferWithPriority(empty, target.type, 64 /* LiteralKeyof */);
- contravariant = !contravariant;
- }
- else if (source.flags & 8388608 /* IndexedAccess */ && target.flags & 8388608 /* IndexedAccess */) {
- inferFromTypes(source.objectType, target.objectType);
- inferFromTypes(source.indexType, target.indexType);
- }
- else if (source.flags & 16777216 /* Conditional */ && target.flags & 16777216 /* Conditional */) {
- inferFromTypes(source.checkType, target.checkType);
- inferFromTypes(source.extendsType, target.extendsType);
- inferFromTypes(getTrueTypeFromConditionalType(source), getTrueTypeFromConditionalType(target));
- inferFromTypes(getFalseTypeFromConditionalType(source), getFalseTypeFromConditionalType(target));
- }
- else if (target.flags & 16777216 /* Conditional */) {
- var savePriority = priority;
- priority |= contravariant ? 16 /* ContravariantConditional */ : 0;
- var targetTypes = [getTrueTypeFromConditionalType(target), getFalseTypeFromConditionalType(target)];
- inferToMultipleTypes(source, targetTypes, target.flags);
- priority = savePriority;
- }
- else if (target.flags & 3145728 /* UnionOrIntersection */) {
- inferToMultipleTypes(source, target.types, target.flags);
+ }
+ if (!(checkMode & 4 /* IgnoreReturnTypes */)) {
+ // If a signature resolution is already in-flight, skip issuing a circularity error
+ // here and just use the `any` type directly
+ var targetReturnType = isResolvingReturnTypeOfSignature(target) ? anyType
+ : target.declaration && isJSConstructor(target.declaration) ? getDeclaredTypeOfClassOrInterface(getMergedSymbol(target.declaration.symbol))
+ : getReturnTypeOfSignature(target);
+ if (targetReturnType === voidType) {
+ return result;
}
- else if (source.flags & 1048576 /* Union */) {
- // Source is a union or intersection type, infer from each constituent type
- var sourceTypes = source.types;
- for (var _e = 0, sourceTypes_2 = sourceTypes; _e < sourceTypes_2.length; _e++) {
- var sourceType = sourceTypes_2[_e];
- inferFromTypes(sourceType, target);
+ var sourceReturnType = isResolvingReturnTypeOfSignature(source) ? anyType
+ : source.declaration && isJSConstructor(source.declaration) ? getDeclaredTypeOfClassOrInterface(getMergedSymbol(source.declaration.symbol))
+ : getReturnTypeOfSignature(source);
+ // The following block preserves behavior forbidding boolean returning functions from being assignable to type guard returning functions
+ var targetTypePredicate = getTypePredicateOfSignature(target);
+ if (targetTypePredicate) {
+ var sourceTypePredicate = getTypePredicateOfSignature(source);
+ if (sourceTypePredicate) {
+ result &= compareTypePredicateRelatedTo(sourceTypePredicate, targetTypePredicate, reportErrors, errorReporter, compareTypes);
}
- }
- else {
- source = getReducedType(source);
- if (!(priority & 128 /* NoConstraints */ && source.flags & (2097152 /* Intersection */ | 63176704 /* Instantiable */))) {
- var apparentSource = getApparentType(source);
- // getApparentType can return _any_ type, since an indexed access or conditional may simplify to any other type.
- // If that occurs and it doesn't simplify to an object or intersection, we'll need to restart `inferFromTypes`
- // with the simplified source.
- if (apparentSource !== source && allowComplexConstraintInference && !(apparentSource.flags & (524288 /* Object */ | 2097152 /* Intersection */))) {
- // TODO: The `allowComplexConstraintInference` flag is a hack! This forbids inference from complex constraints within constraints!
- // This isn't required algorithmically, but rather is used to lower the memory burden caused by performing inference
- // that is _too good_ in projects with complicated constraints (eg, fp-ts). In such cases, if we did not limit ourselves
- // here, we might produce more valid inferences for types, causing us to do more checks and perform more instantiations
- // (in addition to the extra stack depth here) which, in turn, can push the already close process over its limit.
- // TL;DR: If we ever become generally more memory efficient (or our resource budget ever increases), we should just
- // remove this `allowComplexConstraintInference` flag.
- allowComplexConstraintInference = false;
- return inferFromTypes(apparentSource, target);
+ else if (ts.isIdentifierTypePredicate(targetTypePredicate)) {
+ if (reportErrors) {
+ errorReporter(ts.Diagnostics.Signature_0_must_be_a_type_predicate, signatureToString(source));
}
- source = apparentSource;
- }
- if (source.flags & (524288 /* Object */ | 2097152 /* Intersection */)) {
- invokeOnce(source, target, inferFromObjectTypes);
+ return 0 /* False */;
}
}
- if (source.flags & 25165824 /* Simplifiable */) {
- var simplified = getSimplifiedType(source, contravariant);
- if (simplified !== source) {
- inferFromTypes(simplified, target);
+ else {
+ // When relating callback signatures, we still need to relate return types bi-variantly as otherwise
+ // the containing type wouldn't be co-variant. For example, interface Foo { add(cb: () => T): void }
+ // wouldn't be co-variant for T without this rule.
+ result &= checkMode & 1 /* BivariantCallback */ && compareTypes(targetReturnType, sourceReturnType, /*reportErrors*/ false) ||
+ compareTypes(sourceReturnType, targetReturnType, reportErrors);
+ if (!result && reportErrors && incompatibleErrorReporter) {
+ incompatibleErrorReporter(sourceReturnType, targetReturnType);
}
}
}
- function inferWithPriority(source, target, newPriority) {
- var savePriority = priority;
- priority |= newPriority;
- inferFromTypes(source, target);
- priority = savePriority;
- }
- function invokeOnce(source, target, action) {
- var key = source.id + "," + target.id;
- var status = visited && visited.get(key);
- if (status !== undefined) {
- inferencePriority = Math.min(inferencePriority, status);
- return;
+ return result;
+ }
+ function compareTypePredicateRelatedTo(source, target, reportErrors, errorReporter, compareTypes) {
+ if (source.kind !== target.kind) {
+ if (reportErrors) {
+ errorReporter(ts.Diagnostics.A_this_based_type_guard_is_not_compatible_with_a_parameter_based_type_guard);
+ errorReporter(ts.Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target));
}
- (visited || (visited = ts.createMap())).set(key, -1 /* Circularity */);
- var saveInferencePriority = inferencePriority;
- inferencePriority = 512 /* MaxValue */;
- action(source, target);
- visited.set(key, inferencePriority);
- inferencePriority = Math.min(inferencePriority, saveInferencePriority);
+ return 0 /* False */;
}
- function inferFromMatchingTypes(sources, targets, matches) {
- var matchedSources;
- var matchedTargets;
- for (var _i = 0, targets_1 = targets; _i < targets_1.length; _i++) {
- var t = targets_1[_i];
- for (var _a = 0, sources_1 = sources; _a < sources_1.length; _a++) {
- var s = sources_1[_a];
- if (matches(s, t)) {
- inferFromTypes(s, t);
- matchedSources = ts.appendIfUnique(matchedSources, s);
- matchedTargets = ts.appendIfUnique(matchedTargets, t);
- }
+ if (source.kind === 1 /* Identifier */ || source.kind === 3 /* AssertsIdentifier */) {
+ if (source.parameterIndex !== target.parameterIndex) {
+ if (reportErrors) {
+ errorReporter(ts.Diagnostics.Parameter_0_is_not_in_the_same_position_as_parameter_1, source.parameterName, target.parameterName);
+ errorReporter(ts.Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target));
}
+ return 0 /* False */;
}
- return [
- matchedSources ? ts.filter(sources, function (t) { return !ts.contains(matchedSources, t); }) : sources,
- matchedTargets ? ts.filter(targets, function (t) { return !ts.contains(matchedTargets, t); }) : targets,
- ];
}
- function inferFromTypeArguments(sourceTypes, targetTypes, variances) {
- var count = sourceTypes.length < targetTypes.length ? sourceTypes.length : targetTypes.length;
- for (var i = 0; i < count; i++) {
- if (i < variances.length && (variances[i] & 7 /* VarianceMask */) === 2 /* Contravariant */) {
- inferFromContravariantTypes(sourceTypes[i], targetTypes[i]);
- }
- else {
- inferFromTypes(sourceTypes[i], targetTypes[i]);
- }
- }
+ var related = source.type === target.type ? -1 /* True */ :
+ source.type && target.type ? compareTypes(source.type, target.type, reportErrors) :
+ 0 /* False */;
+ if (related === 0 /* False */ && reportErrors) {
+ errorReporter(ts.Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target));
}
- function inferFromContravariantTypes(source, target) {
- if (strictFunctionTypes || priority & 256 /* AlwaysStrict */) {
- contravariant = !contravariant;
- inferFromTypes(source, target);
- contravariant = !contravariant;
- }
- else {
- inferFromTypes(source, target);
- }
+ return related;
+ }
+ function isImplementationCompatibleWithOverload(implementation, overload) {
+ var erasedSource = getErasedSignature(implementation);
+ var erasedTarget = getErasedSignature(overload);
+ // First see if the return types are compatible in either direction.
+ var sourceReturnType = getReturnTypeOfSignature(erasedSource);
+ var targetReturnType = getReturnTypeOfSignature(erasedTarget);
+ if (targetReturnType === voidType
+ || isTypeRelatedTo(targetReturnType, sourceReturnType, assignableRelation)
+ || isTypeRelatedTo(sourceReturnType, targetReturnType, assignableRelation)) {
+ return isSignatureAssignableTo(erasedSource, erasedTarget, /*ignoreReturnTypes*/ true);
}
- function getInferenceInfoForType(type) {
- if (type.flags & 8650752 /* TypeVariable */) {
- for (var _i = 0, inferences_2 = inferences; _i < inferences_2.length; _i++) {
- var inference = inferences_2[_i];
- if (type === inference.typeParameter) {
- return inference;
- }
- }
- }
- return undefined;
+ return false;
+ }
+ function isEmptyResolvedType(t) {
+ return t !== anyFunctionType &&
+ t.properties.length === 0 &&
+ t.callSignatures.length === 0 &&
+ t.constructSignatures.length === 0 &&
+ !t.stringIndexInfo &&
+ !t.numberIndexInfo;
+ }
+ function isEmptyObjectType(type) {
+ return type.flags & 524288 /* Object */ ? !isGenericMappedType(type) && isEmptyResolvedType(resolveStructuredTypeMembers(type)) :
+ type.flags & 67108864 /* NonPrimitive */ ? true :
+ type.flags & 1048576 /* Union */ ? ts.some(type.types, isEmptyObjectType) :
+ type.flags & 2097152 /* Intersection */ ? ts.every(type.types, isEmptyObjectType) :
+ false;
+ }
+ function isEmptyAnonymousObjectType(type) {
+ return !!(ts.getObjectFlags(type) & 16 /* Anonymous */ && (type.members && isEmptyResolvedType(type) ||
+ type.symbol && type.symbol.flags & 2048 /* TypeLiteral */ && getMembersOfSymbol(type.symbol).size === 0));
+ }
+ function isStringIndexSignatureOnlyType(type) {
+ return type.flags & 524288 /* Object */ && !isGenericMappedType(type) && getPropertiesOfType(type).length === 0 && getIndexInfoOfType(type, 0 /* String */) && !getIndexInfoOfType(type, 1 /* Number */) ||
+ type.flags & 3145728 /* UnionOrIntersection */ && ts.every(type.types, isStringIndexSignatureOnlyType) ||
+ false;
+ }
+ function isEnumTypeRelatedTo(sourceSymbol, targetSymbol, errorReporter) {
+ if (sourceSymbol === targetSymbol) {
+ return true;
}
- function getSingleTypeVariableFromIntersectionTypes(types) {
- var typeVariable;
- for (var _i = 0, types_14 = types; _i < types_14.length; _i++) {
- var type = types_14[_i];
- var t = type.flags & 2097152 /* Intersection */ && ts.find(type.types, function (t) { return !!getInferenceInfoForType(t); });
- if (!t || typeVariable && t !== typeVariable) {
- return undefined;
- }
- typeVariable = t;
- }
- return typeVariable;
+ var id = getSymbolId(sourceSymbol) + "," + getSymbolId(targetSymbol);
+ var entry = enumRelation.get(id);
+ if (entry !== undefined && !(!(entry & 4 /* Reported */) && entry & 2 /* Failed */ && errorReporter)) {
+ return !!(entry & 1 /* Succeeded */);
}
- function inferToMultipleTypes(source, targets, targetFlags) {
- var typeVariableCount = 0;
- if (targetFlags & 1048576 /* Union */) {
- var nakedTypeVariable = void 0;
- var sources = source.flags & 1048576 /* Union */ ? source.types : [source];
- var matched_1 = new Array(sources.length);
- var inferenceCircularity = false;
- // First infer to types that are not naked type variables. For each source type we
- // track whether inferences were made from that particular type to some target with
- // equal priority (i.e. of equal quality) to what we would infer for a naked type
- // parameter.
- for (var _i = 0, targets_2 = targets; _i < targets_2.length; _i++) {
- var t = targets_2[_i];
- if (getInferenceInfoForType(t)) {
- nakedTypeVariable = t;
- typeVariableCount++;
+ if (sourceSymbol.escapedName !== targetSymbol.escapedName || !(sourceSymbol.flags & 256 /* RegularEnum */) || !(targetSymbol.flags & 256 /* RegularEnum */)) {
+ enumRelation.set(id, 2 /* Failed */ | 4 /* Reported */);
+ return false;
+ }
+ var targetEnumType = getTypeOfSymbol(targetSymbol);
+ for (var _i = 0, _a = getPropertiesOfType(getTypeOfSymbol(sourceSymbol)); _i < _a.length; _i++) {
+ var property = _a[_i];
+ if (property.flags & 8 /* EnumMember */) {
+ var targetProperty = getPropertyOfType(targetEnumType, property.escapedName);
+ if (!targetProperty || !(targetProperty.flags & 8 /* EnumMember */)) {
+ if (errorReporter) {
+ errorReporter(ts.Diagnostics.Property_0_is_missing_in_type_1, ts.symbolName(property), typeToString(getDeclaredTypeOfSymbol(targetSymbol), /*enclosingDeclaration*/ undefined, 64 /* UseFullyQualifiedType */));
+ enumRelation.set(id, 2 /* Failed */ | 4 /* Reported */);
}
else {
- for (var i = 0; i < sources.length; i++) {
- var saveInferencePriority = inferencePriority;
- inferencePriority = 512 /* MaxValue */;
- inferFromTypes(sources[i], t);
- if (inferencePriority === priority)
- matched_1[i] = true;
- inferenceCircularity = inferenceCircularity || inferencePriority === -1 /* Circularity */;
- inferencePriority = Math.min(inferencePriority, saveInferencePriority);
- }
- }
- }
- if (typeVariableCount === 0) {
- // If every target is an intersection of types containing a single naked type variable,
- // make a lower priority inference to that type variable. This handles inferring from
- // 'A | B' to 'T & (X | Y)' where we want to infer 'A | B' for T.
- var intersectionTypeVariable = getSingleTypeVariableFromIntersectionTypes(targets);
- if (intersectionTypeVariable) {
- inferWithPriority(source, intersectionTypeVariable, 1 /* NakedTypeVariable */);
- }
- return;
- }
- // If the target has a single naked type variable and no inference circularities were
- // encountered above (meaning we explored the types fully), create a union of the source
- // types from which no inferences have been made so far and infer from that union to the
- // naked type variable.
- if (typeVariableCount === 1 && !inferenceCircularity) {
- var unmatched = ts.flatMap(sources, function (s, i) { return matched_1[i] ? undefined : s; });
- if (unmatched.length) {
- inferFromTypes(getUnionType(unmatched), nakedTypeVariable);
- return;
+ enumRelation.set(id, 2 /* Failed */);
}
+ return false;
}
}
- else {
- // We infer from types that are not naked type variables first so that inferences we
- // make from nested naked type variables and given slightly higher priority by virtue
- // of being first in the candidates array.
- for (var _a = 0, targets_3 = targets; _a < targets_3.length; _a++) {
- var t = targets_3[_a];
- if (getInferenceInfoForType(t)) {
- typeVariableCount++;
- }
- else {
- inferFromTypes(source, t);
- }
- }
+ }
+ enumRelation.set(id, 1 /* Succeeded */);
+ return true;
+ }
+ function isSimpleTypeRelatedTo(source, target, relation, errorReporter) {
+ var s = source.flags;
+ var t = target.flags;
+ if (t & 3 /* AnyOrUnknown */ || s & 131072 /* Never */ || source === wildcardType)
+ return true;
+ if (t & 131072 /* Never */)
+ return false;
+ if (s & 402653316 /* StringLike */ && t & 4 /* String */)
+ return true;
+ if (s & 128 /* StringLiteral */ && s & 1024 /* EnumLiteral */ &&
+ t & 128 /* StringLiteral */ && !(t & 1024 /* EnumLiteral */) &&
+ source.value === target.value)
+ return true;
+ if (s & 296 /* NumberLike */ && t & 8 /* Number */)
+ return true;
+ if (s & 256 /* NumberLiteral */ && s & 1024 /* EnumLiteral */ &&
+ t & 256 /* NumberLiteral */ && !(t & 1024 /* EnumLiteral */) &&
+ source.value === target.value)
+ return true;
+ if (s & 2112 /* BigIntLike */ && t & 64 /* BigInt */)
+ return true;
+ if (s & 528 /* BooleanLike */ && t & 16 /* Boolean */)
+ return true;
+ if (s & 12288 /* ESSymbolLike */ && t & 4096 /* ESSymbol */)
+ return true;
+ if (s & 32 /* Enum */ && t & 32 /* Enum */ && isEnumTypeRelatedTo(source.symbol, target.symbol, errorReporter))
+ return true;
+ if (s & 1024 /* EnumLiteral */ && t & 1024 /* EnumLiteral */) {
+ if (s & 1048576 /* Union */ && t & 1048576 /* Union */ && isEnumTypeRelatedTo(source.symbol, target.symbol, errorReporter))
+ return true;
+ if (s & 2944 /* Literal */ && t & 2944 /* Literal */ &&
+ source.value === target.value &&
+ isEnumTypeRelatedTo(getParentOfSymbol(source.symbol), getParentOfSymbol(target.symbol), errorReporter))
+ return true;
+ }
+ if (s & 32768 /* Undefined */ && (!strictNullChecks || t & (32768 /* Undefined */ | 16384 /* Void */)))
+ return true;
+ if (s & 65536 /* Null */ && (!strictNullChecks || t & 65536 /* Null */))
+ return true;
+ if (s & 524288 /* Object */ && t & 67108864 /* NonPrimitive */)
+ return true;
+ if (relation === assignableRelation || relation === comparableRelation) {
+ if (s & 1 /* Any */)
+ return true;
+ // Type number or any numeric literal type is assignable to any numeric enum type or any
+ // numeric enum literal type. This rule exists for backwards compatibility reasons because
+ // bit-flag enum types sometimes look like literal enum types with numeric literal values.
+ if (s & (8 /* Number */ | 256 /* NumberLiteral */) && !(s & 1024 /* EnumLiteral */) && (t & 32 /* Enum */ || t & 256 /* NumberLiteral */ && t & 1024 /* EnumLiteral */))
+ return true;
+ }
+ return false;
+ }
+ function isTypeRelatedTo(source, target, relation) {
+ if (isFreshLiteralType(source)) {
+ source = source.regularType;
+ }
+ if (isFreshLiteralType(target)) {
+ target = target.regularType;
+ }
+ if (source === target) {
+ return true;
+ }
+ if (relation !== identityRelation) {
+ if (relation === comparableRelation && !(target.flags & 131072 /* Never */) && isSimpleTypeRelatedTo(target, source, relation) || isSimpleTypeRelatedTo(source, target, relation)) {
+ return true;
}
- // Inferences directly to naked type variables are given lower priority as they are
- // less specific. For example, when inferring from Promise to T | Promise,
- // we want to infer string for T, not Promise | string. For intersection types
- // we only infer to single naked type variables.
- if (targetFlags & 2097152 /* Intersection */ ? typeVariableCount === 1 : typeVariableCount > 0) {
- for (var _b = 0, targets_4 = targets; _b < targets_4.length; _b++) {
- var t = targets_4[_b];
- if (getInferenceInfoForType(t)) {
- inferWithPriority(source, t, 1 /* NakedTypeVariable */);
- }
- }
+ }
+ else {
+ if (!(source.flags & 3145728 /* UnionOrIntersection */) && !(target.flags & 3145728 /* UnionOrIntersection */) &&
+ source.flags !== target.flags && !(source.flags & 469237760 /* Substructure */))
+ return false;
+ }
+ if (source.flags & 524288 /* Object */ && target.flags & 524288 /* Object */) {
+ var related = relation.get(getRelationKey(source, target, 0 /* None */, relation));
+ if (related !== undefined) {
+ return !!(related & 1 /* Succeeded */);
+ }
+ }
+ if (source.flags & 469499904 /* StructuredOrInstantiable */ || target.flags & 469499904 /* StructuredOrInstantiable */) {
+ return checkTypeRelatedTo(source, target, relation, /*errorNode*/ undefined);
+ }
+ return false;
+ }
+ function isIgnoredJsxProperty(source, sourceProp) {
+ return ts.getObjectFlags(source) & 4096 /* JsxAttributes */ && !isUnhyphenatedJsxName(sourceProp.escapedName);
+ }
+ function getNormalizedType(type, writing) {
+ while (true) {
+ var t = isFreshLiteralType(type) ? type.regularType :
+ ts.getObjectFlags(type) & 4 /* Reference */ && type.node ? createTypeReference(type.target, getTypeArguments(type)) :
+ type.flags & 3145728 /* UnionOrIntersection */ ? getReducedType(type) :
+ type.flags & 33554432 /* Substitution */ ? writing ? type.baseType : type.substitute :
+ type.flags & 25165824 /* Simplifiable */ ? getSimplifiedType(type, writing) :
+ type;
+ if (t === type)
+ break;
+ type = t;
+ }
+ return type;
+ }
+ /**
+ * Checks if 'source' is related to 'target' (e.g.: is a assignable to).
+ * @param source The left-hand-side of the relation.
+ * @param target The right-hand-side of the relation.
+ * @param relation The relation considered. One of 'identityRelation', 'subtypeRelation', 'assignableRelation', or 'comparableRelation'.
+ * Used as both to determine which checks are performed and as a cache of previously computed results.
+ * @param errorNode The suggested node upon which all errors will be reported, if defined. This may or may not be the actual node used.
+ * @param headMessage If the error chain should be prepended by a head message, then headMessage will be used.
+ * @param containingMessageChain A chain of errors to prepend any new errors found.
+ * @param errorOutputContainer Return the diagnostic. Do not log if 'skipLogging' is truthy.
+ */
+ function checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain, errorOutputContainer) {
+ var errorInfo;
+ var relatedInfo;
+ var maybeKeys;
+ var sourceStack;
+ var targetStack;
+ var maybeCount = 0;
+ var depth = 0;
+ var expandingFlags = 0 /* None */;
+ var overflow = false;
+ var overrideNextErrorInfo = 0; // How many `reportRelationError` calls should be skipped in the elaboration pyramid
+ var lastSkippedInfo;
+ var incompatibleStack = [];
+ var inPropertyCheck = false;
+ ts.Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking");
+ var result = isRelatedTo(source, target, /*reportErrors*/ !!errorNode, headMessage);
+ if (incompatibleStack.length) {
+ reportIncompatibleStack();
+ }
+ if (overflow) {
+ ts.tracing.instant("check" /* Check */, "checkTypeRelatedTo_DepthLimit", { sourceId: source.id, targetId: target.id, depth: depth });
+ var diag = error(errorNode || currentNode, ts.Diagnostics.Excessive_stack_depth_comparing_types_0_and_1, typeToString(source), typeToString(target));
+ if (errorOutputContainer) {
+ (errorOutputContainer.errors || (errorOutputContainer.errors = [])).push(diag);
}
}
- function inferToMappedType(source, target, constraintType) {
- if (constraintType.flags & 1048576 /* Union */) {
- var result = false;
- for (var _i = 0, _a = constraintType.types; _i < _a.length; _i++) {
- var type = _a[_i];
- result = inferToMappedType(source, target, type) || result;
+ else if (errorInfo) {
+ if (containingMessageChain) {
+ var chain = containingMessageChain();
+ if (chain) {
+ ts.concatenateDiagnosticMessageChains(chain, errorInfo);
+ errorInfo = chain;
}
- return result;
}
- if (constraintType.flags & 4194304 /* Index */) {
- // We're inferring from some source type S to a homomorphic mapped type { [P in keyof T]: X },
- // where T is a type variable. Use inferTypeForHomomorphicMappedType to infer a suitable source
- // type and then make a secondary inference from that type to T. We make a secondary inference
- // such that direct inferences to T get priority over inferences to Partial, for example.
- var inference = getInferenceInfoForType(constraintType.type);
- if (inference && !inference.isFixed && !isFromInferenceBlockedSource(source)) {
- var inferredType = inferTypeForHomomorphicMappedType(source, target, constraintType);
- if (inferredType) {
- // We assign a lower priority to inferences made from types containing non-inferrable
- // types because we may only have a partial result (i.e. we may have failed to make
- // reverse inferences for some properties).
- inferWithPriority(inferredType, inference.typeParameter, ts.getObjectFlags(source) & 2097152 /* NonInferrableType */ ?
- 4 /* PartialHomomorphicMappedType */ :
- 2 /* HomomorphicMappedType */);
+ var relatedInformation = void 0;
+ // Check if we should issue an extra diagnostic to produce a quickfix for a slightly incorrect import statement
+ if (headMessage && errorNode && !result && source.symbol) {
+ var links = getSymbolLinks(source.symbol);
+ if (links.originatingImport && !ts.isImportCall(links.originatingImport)) {
+ var helpfulRetry = checkTypeRelatedTo(getTypeOfSymbol(links.target), target, relation, /*errorNode*/ undefined);
+ if (helpfulRetry) {
+ // Likely an incorrect import. Issue a helpful diagnostic to produce a quickfix to change the import
+ var diag_1 = ts.createDiagnosticForNode(links.originatingImport, ts.Diagnostics.Type_originates_at_this_import_A_namespace_style_import_cannot_be_called_or_constructed_and_will_cause_a_failure_at_runtime_Consider_using_a_default_import_or_import_require_here_instead);
+ relatedInformation = ts.append(relatedInformation, diag_1); // Cause the error to appear with the error that triggered it
}
}
- return true;
}
- if (constraintType.flags & 262144 /* TypeParameter */) {
- // We're inferring from some source type S to a mapped type { [P in K]: X }, where K is a type
- // parameter. First infer from 'keyof S' to K.
- inferWithPriority(getIndexType(source), constraintType, 8 /* MappedTypeConstraint */);
- // If K is constrained to a type C, also infer to C. Thus, for a mapped type { [P in K]: X },
- // where K extends keyof T, we make the same inferences as for a homomorphic mapped type
- // { [P in keyof T]: X }. This enables us to make meaningful inferences when the target is a
- // Pick.
- var extendedConstraint = getConstraintOfType(constraintType);
- if (extendedConstraint && inferToMappedType(source, target, extendedConstraint)) {
- return true;
- }
- // If no inferences can be made to K's constraint, infer from a union of the property types
- // in the source to the template type X.
- var propTypes = ts.map(getPropertiesOfType(source), getTypeOfSymbol);
- var stringIndexType = getIndexTypeOfType(source, 0 /* String */);
- var numberIndexInfo = getNonEnumNumberIndexInfo(source);
- var numberIndexType = numberIndexInfo && numberIndexInfo.type;
- inferFromTypes(getUnionType(ts.append(ts.append(propTypes, stringIndexType), numberIndexType)), getTemplateTypeFromMappedType(target));
- return true;
+ var diag = ts.createDiagnosticForNodeFromMessageChain(errorNode, errorInfo, relatedInformation);
+ if (relatedInfo) {
+ ts.addRelatedInfo.apply(void 0, __spreadArrays([diag], relatedInfo));
}
- return false;
- }
- function inferFromObjectTypes(source, target) {
- // If we are already processing another target type with the same associated symbol (such as
- // an instantiation of the same generic type), we do not explore this target as it would yield
- // no further inferences. We exclude the static side of classes from this check since it shares
- // its symbol with the instance side which would lead to false positives.
- var isNonConstructorObject = target.flags & 524288 /* Object */ &&
- !(ts.getObjectFlags(target) & 16 /* Anonymous */ && target.symbol && target.symbol.flags & 32 /* Class */);
- var symbolOrType = isNonConstructorObject ? isTupleType(target) ? target.target : target.symbol : undefined;
- if (symbolOrType) {
- if (ts.contains(symbolOrTypeStack, symbolOrType)) {
- inferencePriority = -1 /* Circularity */;
- return;
- }
- (symbolOrTypeStack || (symbolOrTypeStack = [])).push(symbolOrType);
- inferFromObjectTypesWorker(source, target);
- symbolOrTypeStack.pop();
+ if (errorOutputContainer) {
+ (errorOutputContainer.errors || (errorOutputContainer.errors = [])).push(diag);
}
- else {
- inferFromObjectTypesWorker(source, target);
+ if (!errorOutputContainer || !errorOutputContainer.skipLogging) {
+ diagnostics.add(diag);
}
}
- function inferFromObjectTypesWorker(source, target) {
- if (ts.getObjectFlags(source) & 4 /* Reference */ && ts.getObjectFlags(target) & 4 /* Reference */ && (source.target === target.target || isArrayType(source) && isArrayType(target))) {
- // If source and target are references to the same generic type, infer from type arguments
- inferFromTypeArguments(getTypeArguments(source), getTypeArguments(target), getVariances(source.target));
- return;
- }
- if (isGenericMappedType(source) && isGenericMappedType(target)) {
- // The source and target types are generic types { [P in S]: X } and { [P in T]: Y }, so we infer
- // from S to T and from X to Y.
- inferFromTypes(getConstraintTypeFromMappedType(source), getConstraintTypeFromMappedType(target));
- inferFromTypes(getTemplateTypeFromMappedType(source), getTemplateTypeFromMappedType(target));
- }
- if (ts.getObjectFlags(target) & 32 /* Mapped */) {
- var constraintType = getConstraintTypeFromMappedType(target);
- if (inferToMappedType(source, target, constraintType)) {
- return;
+ if (errorNode && errorOutputContainer && errorOutputContainer.skipLogging && result === 0 /* False */) {
+ ts.Debug.assert(!!errorOutputContainer.errors, "missed opportunity to interact with error.");
+ }
+ return result !== 0 /* False */;
+ function resetErrorInfo(saved) {
+ errorInfo = saved.errorInfo;
+ lastSkippedInfo = saved.lastSkippedInfo;
+ incompatibleStack = saved.incompatibleStack;
+ overrideNextErrorInfo = saved.overrideNextErrorInfo;
+ relatedInfo = saved.relatedInfo;
+ }
+ function captureErrorCalculationState() {
+ return {
+ errorInfo: errorInfo,
+ lastSkippedInfo: lastSkippedInfo,
+ incompatibleStack: incompatibleStack.slice(),
+ overrideNextErrorInfo: overrideNextErrorInfo,
+ relatedInfo: !relatedInfo ? undefined : relatedInfo.slice()
+ };
+ }
+ function reportIncompatibleError(message, arg0, arg1, arg2, arg3) {
+ overrideNextErrorInfo++; // Suppress the next relation error
+ lastSkippedInfo = undefined; // Reset skipped info cache
+ incompatibleStack.push([message, arg0, arg1, arg2, arg3]);
+ }
+ function reportIncompatibleStack() {
+ var stack = incompatibleStack;
+ incompatibleStack = [];
+ var info = lastSkippedInfo;
+ lastSkippedInfo = undefined;
+ if (stack.length === 1) {
+ reportError.apply(void 0, stack[0]);
+ if (info) {
+ // Actually do the last relation error
+ reportRelationError.apply(void 0, __spreadArrays([/*headMessage*/ undefined], info));
}
+ return;
}
- // Infer from the members of source and target only if the two types are possibly related
- if (!typesDefinitelyUnrelated(source, target)) {
- if (isArrayType(source) || isTupleType(source)) {
- if (isTupleType(target)) {
- var sourceLength = isTupleType(source) ? getLengthOfTupleType(source) : 0;
- var targetLength = getLengthOfTupleType(target);
- var sourceRestType = isTupleType(source) ? getRestTypeOfTupleType(source) : getElementTypeOfArrayType(source);
- var targetRestType = getRestTypeOfTupleType(target);
- var fixedLength = targetLength < sourceLength || sourceRestType ? targetLength : sourceLength;
- for (var i = 0; i < fixedLength; i++) {
- inferFromTypes(i < sourceLength ? getTypeArguments(source)[i] : sourceRestType, getTypeArguments(target)[i]);
- }
- if (targetRestType) {
- var types = fixedLength < sourceLength ? getTypeArguments(source).slice(fixedLength, sourceLength) : [];
- if (sourceRestType) {
- types.push(sourceRestType);
+ // The first error will be the innermost, while the last will be the outermost - so by popping off the end,
+ // we can build from left to right
+ var path = "";
+ var secondaryRootErrors = [];
+ while (stack.length) {
+ var _a = stack.pop(), msg = _a[0], args = _a.slice(1);
+ switch (msg.code) {
+ case ts.Diagnostics.Types_of_property_0_are_incompatible.code: {
+ // Parenthesize a `new` if there is one
+ if (path.indexOf("new ") === 0) {
+ path = "(" + path + ")";
+ }
+ var str = "" + args[0];
+ // If leading, just print back the arg (irrespective of if it's a valid identifier)
+ if (path.length === 0) {
+ path = "" + str;
+ }
+ // Otherwise write a dotted name if possible
+ else if (ts.isIdentifierText(str, compilerOptions.target)) {
+ path = path + "." + str;
+ }
+ // Failing that, check if the name is already a computed name
+ else if (str[0] === "[" && str[str.length - 1] === "]") {
+ path = "" + path + str;
+ }
+ // And finally write out a computed name as a last resort
+ else {
+ path = path + "[" + str + "]";
+ }
+ break;
+ }
+ case ts.Diagnostics.Call_signature_return_types_0_and_1_are_incompatible.code:
+ case ts.Diagnostics.Construct_signature_return_types_0_and_1_are_incompatible.code:
+ case ts.Diagnostics.Call_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code:
+ case ts.Diagnostics.Construct_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code: {
+ if (path.length === 0) {
+ // Don't flatten signature compatability errors at the start of a chain - instead prefer
+ // to unify (the with no arguments bit is excessive for printback) and print them back
+ var mappedMsg = msg;
+ if (msg.code === ts.Diagnostics.Call_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code) {
+ mappedMsg = ts.Diagnostics.Call_signature_return_types_0_and_1_are_incompatible;
}
- if (types.length) {
- inferFromTypes(getUnionType(types), targetRestType);
+ else if (msg.code === ts.Diagnostics.Construct_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code) {
+ mappedMsg = ts.Diagnostics.Construct_signature_return_types_0_and_1_are_incompatible;
}
+ secondaryRootErrors.unshift([mappedMsg, args[0], args[1]]);
}
- return;
- }
- if (isArrayType(target)) {
- inferFromIndexTypes(source, target);
- return;
+ else {
+ var prefix = (msg.code === ts.Diagnostics.Construct_signature_return_types_0_and_1_are_incompatible.code ||
+ msg.code === ts.Diagnostics.Construct_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code)
+ ? "new "
+ : "";
+ var params = (msg.code === ts.Diagnostics.Call_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code ||
+ msg.code === ts.Diagnostics.Construct_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code)
+ ? ""
+ : "...";
+ path = "" + prefix + path + "(" + params + ")";
+ }
+ break;
}
+ default:
+ return ts.Debug.fail("Unhandled Diagnostic: " + msg.code);
}
- inferFromProperties(source, target);
- inferFromSignatures(source, target, 0 /* Call */);
- inferFromSignatures(source, target, 1 /* Construct */);
- inferFromIndexTypes(source, target);
+ }
+ if (path) {
+ reportError(path[path.length - 1] === ")"
+ ? ts.Diagnostics.The_types_returned_by_0_are_incompatible_between_these_types
+ : ts.Diagnostics.The_types_of_0_are_incompatible_between_these_types, path);
+ }
+ else {
+ // Remove the innermost secondary error as it will duplicate the error already reported by `reportRelationError` on entry
+ secondaryRootErrors.shift();
+ }
+ for (var _i = 0, secondaryRootErrors_1 = secondaryRootErrors; _i < secondaryRootErrors_1.length; _i++) {
+ var _b = secondaryRootErrors_1[_i], msg = _b[0], args = _b.slice(1);
+ var originalValue = msg.elidedInCompatabilityPyramid;
+ msg.elidedInCompatabilityPyramid = false; // Teporarily override elision to ensure error is reported
+ reportError.apply(void 0, __spreadArrays([msg], args));
+ msg.elidedInCompatabilityPyramid = originalValue;
+ }
+ if (info) {
+ // Actually do the last relation error
+ reportRelationError.apply(void 0, __spreadArrays([/*headMessage*/ undefined], info));
}
}
- function inferFromProperties(source, target) {
- var properties = getPropertiesOfObjectType(target);
- for (var _i = 0, properties_3 = properties; _i < properties_3.length; _i++) {
- var targetProp = properties_3[_i];
- var sourceProp = getPropertyOfType(source, targetProp.escapedName);
- if (sourceProp) {
- inferFromTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp));
- }
+ function reportError(message, arg0, arg1, arg2, arg3) {
+ ts.Debug.assert(!!errorNode);
+ if (incompatibleStack.length)
+ reportIncompatibleStack();
+ if (message.elidedInCompatabilityPyramid)
+ return;
+ errorInfo = ts.chainDiagnosticMessages(errorInfo, message, arg0, arg1, arg2, arg3);
+ }
+ function associateRelatedInfo(info) {
+ ts.Debug.assert(!!errorInfo);
+ if (!relatedInfo) {
+ relatedInfo = [info];
+ }
+ else {
+ relatedInfo.push(info);
}
}
- function inferFromSignatures(source, target, kind) {
- var sourceSignatures = getSignaturesOfType(source, kind);
- var targetSignatures = getSignaturesOfType(target, kind);
- var sourceLen = sourceSignatures.length;
- var targetLen = targetSignatures.length;
- var len = sourceLen < targetLen ? sourceLen : targetLen;
- var skipParameters = !!(ts.getObjectFlags(source) & 2097152 /* NonInferrableType */);
- for (var i = 0; i < len; i++) {
- inferFromSignature(getBaseSignature(sourceSignatures[sourceLen - len + i]), getErasedSignature(targetSignatures[targetLen - len + i]), skipParameters);
+ function reportRelationError(message, source, target) {
+ if (incompatibleStack.length)
+ reportIncompatibleStack();
+ var _a = getTypeNamesForErrorDisplay(source, target), sourceType = _a[0], targetType = _a[1];
+ var generalizedSource = source;
+ var generalizedSourceType = sourceType;
+ if (isLiteralType(source) && !typeCouldHaveTopLevelSingletonTypes(target)) {
+ generalizedSource = getBaseTypeOfLiteralType(source);
+ ts.Debug.assert(!isTypeAssignableTo(generalizedSource, target), "generalized source shouldn't be assignable");
+ generalizedSourceType = getTypeNameForErrorDisplay(generalizedSource);
+ }
+ if (target.flags & 262144 /* TypeParameter */) {
+ var constraint = getBaseConstraintOfType(target);
+ var needsOriginalSource = void 0;
+ if (constraint && (isTypeAssignableTo(generalizedSource, constraint) || (needsOriginalSource = isTypeAssignableTo(source, constraint)))) {
+ reportError(ts.Diagnostics._0_is_assignable_to_the_constraint_of_type_1_but_1_could_be_instantiated_with_a_different_subtype_of_constraint_2, needsOriginalSource ? sourceType : generalizedSourceType, targetType, typeToString(constraint));
+ }
+ else {
+ reportError(ts.Diagnostics._0_could_be_instantiated_with_an_arbitrary_type_which_could_be_unrelated_to_1, targetType, generalizedSourceType);
+ }
+ }
+ if (!message) {
+ if (relation === comparableRelation) {
+ message = ts.Diagnostics.Type_0_is_not_comparable_to_type_1;
+ }
+ else if (sourceType === targetType) {
+ message = ts.Diagnostics.Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated;
+ }
+ else {
+ message = ts.Diagnostics.Type_0_is_not_assignable_to_type_1;
+ }
}
+ reportError(message, generalizedSourceType, targetType);
}
- function inferFromSignature(source, target, skipParameters) {
- if (!skipParameters) {
- var saveBivariant = bivariant;
- var kind = target.declaration ? target.declaration.kind : 0 /* Unknown */;
- // Once we descend into a bivariant signature we remain bivariant for all nested inferences
- bivariant = bivariant || kind === 161 /* MethodDeclaration */ || kind === 160 /* MethodSignature */ || kind === 162 /* Constructor */;
- applyToParameterTypes(source, target, inferFromContravariantTypes);
- bivariant = saveBivariant;
+ function tryElaborateErrorsForPrimitivesAndObjects(source, target) {
+ var sourceType = symbolValueDeclarationIsContextSensitive(source.symbol) ? typeToString(source, source.symbol.valueDeclaration) : typeToString(source);
+ var targetType = symbolValueDeclarationIsContextSensitive(target.symbol) ? typeToString(target, target.symbol.valueDeclaration) : typeToString(target);
+ if ((globalStringType === source && stringType === target) ||
+ (globalNumberType === source && numberType === target) ||
+ (globalBooleanType === source && booleanType === target) ||
+ (getGlobalESSymbolType(/*reportErrors*/ false) === source && esSymbolType === target)) {
+ reportError(ts.Diagnostics._0_is_a_primitive_but_1_is_a_wrapper_object_Prefer_using_0_when_possible, targetType, sourceType);
}
- applyToReturnTypes(source, target, inferFromTypes);
}
- function inferFromIndexTypes(source, target) {
- var targetStringIndexType = getIndexTypeOfType(target, 0 /* String */);
- if (targetStringIndexType) {
- var sourceIndexType = getIndexTypeOfType(source, 0 /* String */) ||
- getImplicitIndexTypeOfType(source, 0 /* String */);
- if (sourceIndexType) {
- inferFromTypes(sourceIndexType, targetStringIndexType);
+ /**
+ * Try and elaborate array and tuple errors. Returns false
+ * if we have found an elaboration, or we should ignore
+ * any other elaborations when relating the `source` and
+ * `target` types.
+ */
+ function tryElaborateArrayLikeErrors(source, target, reportErrors) {
+ /**
+ * The spec for elaboration is:
+ * - If the source is a readonly tuple and the target is a mutable array or tuple, elaborate on mutability and skip property elaborations.
+ * - If the source is a tuple then skip property elaborations if the target is an array or tuple.
+ * - If the source is a readonly array and the target is a mutable array or tuple, elaborate on mutability and skip property elaborations.
+ * - If the source an array then skip property elaborations if the target is a tuple.
+ */
+ if (isTupleType(source)) {
+ if (source.target.readonly && isMutableArrayOrTuple(target)) {
+ if (reportErrors) {
+ reportError(ts.Diagnostics.The_type_0_is_readonly_and_cannot_be_assigned_to_the_mutable_type_1, typeToString(source), typeToString(target));
+ }
+ return false;
}
+ return isTupleType(target) || isArrayType(target);
}
- var targetNumberIndexType = getIndexTypeOfType(target, 1 /* Number */);
- if (targetNumberIndexType) {
- var sourceIndexType = getIndexTypeOfType(source, 1 /* Number */) ||
- getIndexTypeOfType(source, 0 /* String */) ||
- getImplicitIndexTypeOfType(source, 1 /* Number */);
- if (sourceIndexType) {
- inferFromTypes(sourceIndexType, targetNumberIndexType);
+ if (isReadonlyArrayType(source) && isMutableArrayOrTuple(target)) {
+ if (reportErrors) {
+ reportError(ts.Diagnostics.The_type_0_is_readonly_and_cannot_be_assigned_to_the_mutable_type_1, typeToString(source), typeToString(target));
}
+ return false;
}
- }
- }
- function isTypeOrBaseIdenticalTo(s, t) {
- return isTypeIdenticalTo(s, t) || !!(t.flags & 4 /* String */ && s.flags & 128 /* StringLiteral */ || t.flags & 8 /* Number */ && s.flags & 256 /* NumberLiteral */);
- }
- function isTypeCloselyMatchedBy(s, t) {
- return !!(s.flags & 524288 /* Object */ && t.flags & 524288 /* Object */ && s.symbol && s.symbol === t.symbol ||
- s.aliasSymbol && s.aliasTypeArguments && s.aliasSymbol === t.aliasSymbol);
- }
- function hasPrimitiveConstraint(type) {
- var constraint = getConstraintOfTypeParameter(type);
- return !!constraint && maybeTypeOfKind(constraint.flags & 16777216 /* Conditional */ ? getDefaultConstraintOfConditionalType(constraint) : constraint, 131068 /* Primitive */ | 4194304 /* Index */);
- }
- function isObjectLiteralType(type) {
- return !!(ts.getObjectFlags(type) & 128 /* ObjectLiteral */);
- }
- function isObjectOrArrayLiteralType(type) {
- return !!(ts.getObjectFlags(type) & (128 /* ObjectLiteral */ | 65536 /* ArrayLiteral */));
- }
- function unionObjectAndArrayLiteralCandidates(candidates) {
- if (candidates.length > 1) {
- var objectLiterals = ts.filter(candidates, isObjectOrArrayLiteralType);
- if (objectLiterals.length) {
- var literalsType = getUnionType(objectLiterals, 2 /* Subtype */);
- return ts.concatenate(ts.filter(candidates, function (t) { return !isObjectOrArrayLiteralType(t); }), [literalsType]);
+ if (isTupleType(target)) {
+ return isArrayType(source);
}
+ return true;
}
- return candidates;
- }
- function getContravariantInference(inference) {
- return inference.priority & 104 /* PriorityImpliesCombination */ ? getIntersectionType(inference.contraCandidates) : getCommonSubtype(inference.contraCandidates);
- }
- function getCovariantInference(inference, signature) {
- // Extract all object and array literal types and replace them with a single widened and normalized type.
- var candidates = unionObjectAndArrayLiteralCandidates(inference.candidates);
- // We widen inferred literal types if
- // all inferences were made to top-level occurrences of the type parameter, and
- // the type parameter has no constraint or its constraint includes no primitive or literal types, and
- // the type parameter was fixed during inference or does not occur at top-level in the return type.
- var primitiveConstraint = hasPrimitiveConstraint(inference.typeParameter);
- var widenLiteralTypes = !primitiveConstraint && inference.topLevel &&
- (inference.isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), inference.typeParameter));
- var baseCandidates = primitiveConstraint ? ts.sameMap(candidates, getRegularTypeOfLiteralType) :
- widenLiteralTypes ? ts.sameMap(candidates, getWidenedLiteralType) :
- candidates;
- // If all inferences were made from a position that implies a combined result, infer a union type.
- // Otherwise, infer a common supertype.
- var unwidenedType = inference.priority & 104 /* PriorityImpliesCombination */ ?
- getUnionType(baseCandidates, 2 /* Subtype */) :
- getCommonSupertype(baseCandidates);
- return getWidenedType(unwidenedType);
- }
- function getInferredType(context, index) {
- var inference = context.inferences[index];
- if (!inference.inferredType) {
- var inferredType = void 0;
- var signature = context.signature;
- if (signature) {
- var inferredCovariantType = inference.candidates ? getCovariantInference(inference, signature) : undefined;
- if (inference.contraCandidates) {
- var inferredContravariantType = getContravariantInference(inference);
- // If we have both co- and contra-variant inferences, we prefer the contra-variant inference
- // unless the co-variant inference is a subtype and not 'never'.
- inferredType = inferredCovariantType && !(inferredCovariantType.flags & 131072 /* Never */) &&
- isTypeSubtypeOf(inferredCovariantType, inferredContravariantType) ?
- inferredCovariantType : inferredContravariantType;
+ /**
+ * Compare two types and return
+ * * Ternary.True if they are related with no assumptions,
+ * * Ternary.Maybe if they are related with assumptions of other relationships, or
+ * * Ternary.False if they are not related.
+ */
+ function isRelatedTo(originalSource, originalTarget, reportErrors, headMessage, intersectionState) {
+ if (reportErrors === void 0) { reportErrors = false; }
+ if (intersectionState === void 0) { intersectionState = 0 /* None */; }
+ // Before normalization: if `source` is type an object type, and `target` is primitive,
+ // skip all the checks we don't need and just return `isSimpleTypeRelatedTo` result
+ if (originalSource.flags & 524288 /* Object */ && originalTarget.flags & 131068 /* Primitive */) {
+ if (isSimpleTypeRelatedTo(originalSource, originalTarget, relation, reportErrors ? reportError : undefined)) {
+ return -1 /* True */;
}
- else if (inferredCovariantType) {
- inferredType = inferredCovariantType;
+ reportErrorResults(originalSource, originalTarget, 0 /* False */, !!(ts.getObjectFlags(originalSource) & 4096 /* JsxAttributes */));
+ return 0 /* False */;
+ }
+ // Normalize the source and target types: Turn fresh literal types into regular literal types,
+ // turn deferred type references into regular type references, simplify indexed access and
+ // conditional types, and resolve substitution types to either the substitution (on the source
+ // side) or the type variable (on the target side).
+ var source = getNormalizedType(originalSource, /*writing*/ false);
+ var target = getNormalizedType(originalTarget, /*writing*/ true);
+ if (source === target)
+ return -1 /* True */;
+ if (relation === identityRelation) {
+ return isIdenticalTo(source, target);
+ }
+ // We fastpath comparing a type parameter to exactly its constraint, as this is _super_ common,
+ // and otherwise, for type parameters in large unions, causes us to need to compare the union to itself,
+ // as we break down the _target_ union first, _then_ get the source constraint - so for every
+ // member of the target, we attempt to find a match in the source. This avoids that in cases where
+ // the target is exactly the constraint.
+ if (source.flags & 262144 /* TypeParameter */ && getConstraintOfType(source) === target) {
+ return -1 /* True */;
+ }
+ // Try to see if we're relating something like `Foo` -> `Bar | null | undefined`.
+ // If so, reporting the `null` and `undefined` in the type is hardly useful.
+ // First, see if we're even relating an object type to a union.
+ // Then see if the target is stripped down to a single non-union type.
+ // Note
+ // * We actually want to remove null and undefined naively here (rather than using getNonNullableType),
+ // since we don't want to end up with a worse error like "`Foo` is not assignable to `NonNullable`"
+ // when dealing with generics.
+ // * We also don't deal with primitive source types, since we already halt elaboration below.
+ if (target.flags & 1048576 /* Union */ && source.flags & 524288 /* Object */ &&
+ target.types.length <= 3 && maybeTypeOfKind(target, 98304 /* Nullable */)) {
+ var nullStrippedTarget = extractTypesOfKind(target, ~98304 /* Nullable */);
+ if (!(nullStrippedTarget.flags & (1048576 /* Union */ | 131072 /* Never */))) {
+ if (source === nullStrippedTarget)
+ return -1 /* True */;
+ target = nullStrippedTarget;
}
- else if (context.flags & 1 /* NoDefault */) {
- // We use silentNeverType as the wildcard that signals no inferences.
- inferredType = silentNeverType;
+ }
+ if (relation === comparableRelation && !(target.flags & 131072 /* Never */) && isSimpleTypeRelatedTo(target, source, relation) ||
+ isSimpleTypeRelatedTo(source, target, relation, reportErrors ? reportError : undefined))
+ return -1 /* True */;
+ var isComparingJsxAttributes = !!(ts.getObjectFlags(source) & 4096 /* JsxAttributes */);
+ var isPerformingExcessPropertyChecks = !(intersectionState & 2 /* Target */) && (isObjectLiteralType(source) && ts.getObjectFlags(source) & 32768 /* FreshLiteral */);
+ if (isPerformingExcessPropertyChecks) {
+ if (hasExcessProperties(source, target, reportErrors)) {
+ if (reportErrors) {
+ reportRelationError(headMessage, source, target);
+ }
+ return 0 /* False */;
}
- else {
- // Infer either the default or the empty object type when no inferences were
- // made. It is important to remember that in this case, inference still
- // succeeds, meaning there is no error for not having inference candidates. An
- // inference error only occurs when there are *conflicting* candidates, i.e.
- // candidates with no common supertype.
- var defaultType = getDefaultFromTypeParameter(inference.typeParameter);
- if (defaultType) {
- // Instantiate the default type. Any forward reference to a type
- // parameter should be instantiated to the empty object type.
- inferredType = instantiateType(defaultType, mergeTypeMappers(createBackreferenceMapper(context, index), context.nonFixingMapper));
+ }
+ var isPerformingCommonPropertyChecks = relation !== comparableRelation && !(intersectionState & 2 /* Target */) &&
+ source.flags & (131068 /* Primitive */ | 524288 /* Object */ | 2097152 /* Intersection */) && source !== globalObjectType &&
+ target.flags & (524288 /* Object */ | 2097152 /* Intersection */) && isWeakType(target) &&
+ (getPropertiesOfType(source).length > 0 || typeHasCallOrConstructSignatures(source));
+ if (isPerformingCommonPropertyChecks && !hasCommonProperties(source, target, isComparingJsxAttributes)) {
+ if (reportErrors) {
+ var calls = getSignaturesOfType(source, 0 /* Call */);
+ var constructs = getSignaturesOfType(source, 1 /* Construct */);
+ if (calls.length > 0 && isRelatedTo(getReturnTypeOfSignature(calls[0]), target, /*reportErrors*/ false) ||
+ constructs.length > 0 && isRelatedTo(getReturnTypeOfSignature(constructs[0]), target, /*reportErrors*/ false)) {
+ reportError(ts.Diagnostics.Value_of_type_0_has_no_properties_in_common_with_type_1_Did_you_mean_to_call_it, typeToString(source), typeToString(target));
+ }
+ else {
+ reportError(ts.Diagnostics.Type_0_has_no_properties_in_common_with_type_1, typeToString(source), typeToString(target));
}
}
+ return 0 /* False */;
}
- else {
- inferredType = getTypeFromInference(inference);
+ var result = 0 /* False */;
+ var saveErrorInfo = captureErrorCalculationState();
+ // Note that these checks are specifically ordered to produce correct results. In particular,
+ // we need to deconstruct unions before intersections (because unions are always at the top),
+ // and we need to handle "each" relations before "some" relations for the same kind of type.
+ if (source.flags & 1048576 /* Union */) {
+ result = relation === comparableRelation ?
+ someTypeRelatedToType(source, target, reportErrors && !(source.flags & 131068 /* Primitive */), intersectionState) :
+ eachTypeRelatedToType(source, target, reportErrors && !(source.flags & 131068 /* Primitive */), intersectionState);
}
- inference.inferredType = inferredType || getDefaultTypeArgumentType(!!(context.flags & 2 /* AnyDefault */));
- var constraint = getConstraintOfTypeParameter(inference.typeParameter);
- if (constraint) {
- var instantiatedConstraint = instantiateType(constraint, context.nonFixingMapper);
- if (!inferredType || !context.compareTypes(inferredType, getTypeWithThisArgument(instantiatedConstraint, inferredType))) {
- inference.inferredType = inferredType = instantiatedConstraint;
+ else {
+ if (target.flags & 1048576 /* Union */) {
+ result = typeRelatedToSomeType(getRegularTypeOfObjectLiteral(source), target, reportErrors && !(source.flags & 131068 /* Primitive */) && !(target.flags & 131068 /* Primitive */));
}
- }
- }
- return inference.inferredType;
- }
- function getDefaultTypeArgumentType(isInJavaScriptFile) {
- return isInJavaScriptFile ? anyType : unknownType;
- }
- function getInferredTypes(context) {
- var result = [];
- for (var i = 0; i < context.inferences.length; i++) {
- result.push(getInferredType(context, i));
- }
- return result;
- }
- // EXPRESSION TYPE CHECKING
- function getCannotFindNameDiagnosticForName(node) {
- switch (node.escapedText) {
- case "document":
- case "console":
- return ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_include_dom;
- case "$":
- return compilerOptions.types
- ? ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_types_Slashjquery_and_then_add_jquery_to_the_types_field_in_your_tsconfig
- : ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_types_Slashjquery;
- case "describe":
- case "suite":
- case "it":
- case "test":
- return compilerOptions.types
- ? ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_a_test_runner_Try_npm_i_types_Slashjest_or_npm_i_types_Slashmocha_and_then_add_jest_or_mocha_to_the_types_field_in_your_tsconfig
- : ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_a_test_runner_Try_npm_i_types_Slashjest_or_npm_i_types_Slashmocha;
- case "process":
- case "require":
- case "Buffer":
- case "module":
- return compilerOptions.types
- ? ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_types_Slashnode_and_then_add_node_to_the_types_field_in_your_tsconfig
- : ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_types_Slashnode;
- case "Map":
- case "Set":
- case "Promise":
- case "Symbol":
- case "WeakMap":
- case "WeakSet":
- case "Iterator":
- case "AsyncIterator":
- return ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_es2015_or_later;
- default:
- if (node.parent.kind === 282 /* ShorthandPropertyAssignment */) {
- return ts.Diagnostics.No_value_exists_in_scope_for_the_shorthand_property_0_Either_declare_one_or_provide_an_initializer;
+ else if (target.flags & 2097152 /* Intersection */) {
+ result = typeRelatedToEachType(getRegularTypeOfObjectLiteral(source), target, reportErrors, 2 /* Target */);
}
- else {
- return ts.Diagnostics.Cannot_find_name_0;
+ else if (source.flags & 2097152 /* Intersection */) {
+ // Check to see if any constituents of the intersection are immediately related to the target.
+ //
+ // Don't report errors though. Checking whether a constituent is related to the source is not actually
+ // useful and leads to some confusing error messages. Instead it is better to let the below checks
+ // take care of this, or to not elaborate at all. For instance,
+ //
+ // - For an object type (such as 'C = A & B'), users are usually more interested in structural errors.
+ //
+ // - For a union type (such as '(A | B) = (C & D)'), it's better to hold onto the whole intersection
+ // than to report that 'D' is not assignable to 'A' or 'B'.
+ //
+ // - For a primitive type or type parameter (such as 'number = A & B') there is no point in
+ // breaking the intersection apart.
+ result = someTypeRelatedToType(source, target, /*reportErrors*/ false, 1 /* Source */);
}
- }
- }
- function getResolvedSymbol(node) {
- var links = getNodeLinks(node);
- if (!links.resolvedSymbol) {
- links.resolvedSymbol = !ts.nodeIsMissing(node) &&
- resolveName(node, node.escapedText, 111551 /* Value */ | 1048576 /* ExportValue */, getCannotFindNameDiagnosticForName(node), node, !ts.isWriteOnlyAccess(node),
- /*excludeGlobals*/ false, ts.Diagnostics.Cannot_find_name_0_Did_you_mean_1) || unknownSymbol;
- }
- return links.resolvedSymbol;
- }
- function isInTypeQuery(node) {
- // TypeScript 1.0 spec (April 2014): 3.6.3
- // A type query consists of the keyword typeof followed by an expression.
- // The expression is restricted to a single identifier or a sequence of identifiers separated by periods
- return !!ts.findAncestor(node, function (n) { return n.kind === 172 /* TypeQuery */ ? true : n.kind === 75 /* Identifier */ || n.kind === 153 /* QualifiedName */ ? false : "quit"; });
- }
- // Return the flow cache key for a "dotted name" (i.e. a sequence of identifiers
- // separated by dots). The key consists of the id of the symbol referenced by the
- // leftmost identifier followed by zero or more property names separated by dots.
- // The result is undefined if the reference isn't a dotted name. We prefix nodes
- // occurring in an apparent type position with '@' because the control flow type
- // of such nodes may be based on the apparent type instead of the declared type.
- function getFlowCacheKey(node, declaredType, initialType, flowContainer) {
- switch (node.kind) {
- case 75 /* Identifier */:
- var symbol = getResolvedSymbol(node);
- return symbol !== unknownSymbol ? (flowContainer ? getNodeId(flowContainer) : "-1") + "|" + getTypeId(declaredType) + "|" + getTypeId(initialType) + "|" + (isConstraintPosition(node) ? "@" : "") + getSymbolId(symbol) : undefined;
- case 104 /* ThisKeyword */:
- return "0";
- case 218 /* NonNullExpression */:
- case 200 /* ParenthesizedExpression */:
- return getFlowCacheKey(node.expression, declaredType, initialType, flowContainer);
- case 194 /* PropertyAccessExpression */:
- case 195 /* ElementAccessExpression */:
- var propName = getAccessedPropertyName(node);
- if (propName !== undefined) {
- var key = getFlowCacheKey(node.expression, declaredType, initialType, flowContainer);
- return key && key + "." + propName;
+ if (!result && (source.flags & 469499904 /* StructuredOrInstantiable */ || target.flags & 469499904 /* StructuredOrInstantiable */)) {
+ if (result = recursiveTypeRelatedTo(source, target, reportErrors, intersectionState)) {
+ resetErrorInfo(saveErrorInfo);
+ }
}
- }
- return undefined;
- }
- function isMatchingReference(source, target) {
- switch (target.kind) {
- case 200 /* ParenthesizedExpression */:
- case 218 /* NonNullExpression */:
- return isMatchingReference(source, target.expression);
- }
- switch (source.kind) {
- case 75 /* Identifier */:
- return target.kind === 75 /* Identifier */ && getResolvedSymbol(source) === getResolvedSymbol(target) ||
- (target.kind === 242 /* VariableDeclaration */ || target.kind === 191 /* BindingElement */) &&
- getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(source)) === getSymbolOfNode(target);
- case 104 /* ThisKeyword */:
- return target.kind === 104 /* ThisKeyword */;
- case 102 /* SuperKeyword */:
- return target.kind === 102 /* SuperKeyword */;
- case 218 /* NonNullExpression */:
- case 200 /* ParenthesizedExpression */:
- return isMatchingReference(source.expression, target);
- case 194 /* PropertyAccessExpression */:
- case 195 /* ElementAccessExpression */:
- return ts.isAccessExpression(target) &&
- getAccessedPropertyName(source) === getAccessedPropertyName(target) &&
- isMatchingReference(source.expression, target.expression);
- }
- return false;
- }
- // Given a source x, check if target matches x or is an && operation with an operand that matches x.
- function containsTruthyCheck(source, target) {
- return isMatchingReference(source, target) ||
- (target.kind === 209 /* BinaryExpression */ && target.operatorToken.kind === 55 /* AmpersandAmpersandToken */ &&
- (containsTruthyCheck(source, target.left) || containsTruthyCheck(source, target.right)));
- }
- function getAccessedPropertyName(access) {
- return access.kind === 194 /* PropertyAccessExpression */ ? access.name.escapedText :
- ts.isStringOrNumericLiteralLike(access.argumentExpression) ? ts.escapeLeadingUnderscores(access.argumentExpression.text) :
- undefined;
- }
- function containsMatchingReference(source, target) {
- while (ts.isAccessExpression(source)) {
- source = source.expression;
- if (isMatchingReference(source, target)) {
- return true;
- }
- }
- return false;
- }
- function optionalChainContainsReference(source, target) {
- while (ts.isOptionalChain(source)) {
- source = source.expression;
- if (isMatchingReference(source, target)) {
- return true;
}
- }
- return false;
- }
- function isDiscriminantProperty(type, name) {
- if (type && type.flags & 1048576 /* Union */) {
- var prop = getUnionOrIntersectionProperty(type, name);
- if (prop && ts.getCheckFlags(prop) & 2 /* SyntheticProperty */) {
- if (prop.isDiscriminantProperty === undefined) {
- prop.isDiscriminantProperty =
- (prop.checkFlags & 192 /* Discriminant */) === 192 /* Discriminant */ &&
- !maybeTypeOfKind(getTypeOfSymbol(prop), 63176704 /* Instantiable */);
+ if (!result && source.flags & (2097152 /* Intersection */ | 262144 /* TypeParameter */)) {
+ // The combined constraint of an intersection type is the intersection of the constraints of
+ // the constituents. When an intersection type contains instantiable types with union type
+ // constraints, there are situations where we need to examine the combined constraint. One is
+ // when the target is a union type. Another is when the intersection contains types belonging
+ // to one of the disjoint domains. For example, given type variables T and U, each with the
+ // constraint 'string | number', the combined constraint of 'T & U' is 'string | number' and
+ // we need to check this constraint against a union on the target side. Also, given a type
+ // variable V constrained to 'string | number', 'V & number' has a combined constraint of
+ // 'string & number | number & number' which reduces to just 'number'.
+ // This also handles type parameters, as a type parameter with a union constraint compared against a union
+ // needs to have its constraint hoisted into an intersection with said type parameter, this way
+ // the type param can be compared with itself in the target (with the influence of its constraint to match other parts)
+ // For example, if `T extends 1 | 2` and `U extends 2 | 3` and we compare `T & U` to `T & U & (1 | 2 | 3)`
+ var constraint = getEffectiveConstraintOfIntersection(source.flags & 2097152 /* Intersection */ ? source.types : [source], !!(target.flags & 1048576 /* Union */));
+ if (constraint && (source.flags & 2097152 /* Intersection */ || target.flags & 1048576 /* Union */)) {
+ if (everyType(constraint, function (c) { return c !== source; })) { // Skip comparison if expansion contains the source itself
+ // TODO: Stack errors so we get a pyramid for the "normal" comparison above, _and_ a second for this
+ if (result = isRelatedTo(constraint, target, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState)) {
+ resetErrorInfo(saveErrorInfo);
+ }
+ }
}
- return !!prop.isDiscriminantProperty;
}
- }
- return false;
- }
- function findDiscriminantProperties(sourceProperties, target) {
- var result;
- for (var _i = 0, sourceProperties_2 = sourceProperties; _i < sourceProperties_2.length; _i++) {
- var sourceProperty = sourceProperties_2[_i];
- if (isDiscriminantProperty(target, sourceProperty.escapedName)) {
- if (result) {
- result.push(sourceProperty);
- continue;
+ // For certain combinations involving intersections and optional, excess, or mismatched properties we need
+ // an extra property check where the intersection is viewed as a single object. The following are motivating
+ // examples that all should be errors, but aren't without this extra property check:
+ //
+ // let obj: { a: { x: string } } & { c: number } = { a: { x: 'hello', y: 2 }, c: 5 }; // Nested excess property
+ //
+ // declare let wrong: { a: { y: string } };
+ // let weak: { a?: { x?: number } } & { c?: string } = wrong; // Nested weak object type
+ //
+ // function foo(x: { a?: string }, y: T & { a: boolean }) {
+ // x = y; // Mismatched property in source intersection
+ // }
+ //
+ // We suppress recursive intersection property checks because they can generate lots of work when relating
+ // recursive intersections that are structurally similar but not exactly identical. See #37854.
+ if (result && !inPropertyCheck && (target.flags & 2097152 /* Intersection */ && (isPerformingExcessPropertyChecks || isPerformingCommonPropertyChecks) ||
+ isNonGenericObjectType(target) && !isArrayType(target) && !isTupleType(target) && source.flags & 2097152 /* Intersection */ && getApparentType(source).flags & 3670016 /* StructuredType */ && !ts.some(source.types, function (t) { return !!(ts.getObjectFlags(t) & 2097152 /* NonInferrableType */); }))) {
+ inPropertyCheck = true;
+ result &= recursiveTypeRelatedTo(source, target, reportErrors, 4 /* PropertyCheck */);
+ inPropertyCheck = false;
+ }
+ reportErrorResults(source, target, result, isComparingJsxAttributes);
+ return result;
+ function reportErrorResults(source, target, result, isComparingJsxAttributes) {
+ if (!result && reportErrors) {
+ source = originalSource.aliasSymbol ? originalSource : source;
+ target = originalTarget.aliasSymbol ? originalTarget : target;
+ var maybeSuppress = overrideNextErrorInfo > 0;
+ if (maybeSuppress) {
+ overrideNextErrorInfo--;
+ }
+ if (source.flags & 524288 /* Object */ && target.flags & 524288 /* Object */) {
+ var currentError = errorInfo;
+ tryElaborateArrayLikeErrors(source, target, reportErrors);
+ if (errorInfo !== currentError) {
+ maybeSuppress = !!errorInfo;
+ }
+ }
+ if (source.flags & 524288 /* Object */ && target.flags & 131068 /* Primitive */) {
+ tryElaborateErrorsForPrimitivesAndObjects(source, target);
+ }
+ else if (source.symbol && source.flags & 524288 /* Object */ && globalObjectType === source) {
+ reportError(ts.Diagnostics.The_Object_type_is_assignable_to_very_few_other_types_Did_you_mean_to_use_the_any_type_instead);
+ }
+ else if (isComparingJsxAttributes && target.flags & 2097152 /* Intersection */) {
+ var targetTypes = target.types;
+ var intrinsicAttributes = getJsxType(JsxNames.IntrinsicAttributes, errorNode);
+ var intrinsicClassAttributes = getJsxType(JsxNames.IntrinsicClassAttributes, errorNode);
+ if (intrinsicAttributes !== errorType && intrinsicClassAttributes !== errorType &&
+ (ts.contains(targetTypes, intrinsicAttributes) || ts.contains(targetTypes, intrinsicClassAttributes))) {
+ // do not report top error
+ return result;
+ }
+ }
+ else {
+ errorInfo = elaborateNeverIntersection(errorInfo, originalTarget);
+ }
+ if (!headMessage && maybeSuppress) {
+ lastSkippedInfo = [source, target];
+ // Used by, eg, missing property checking to replace the top-level message with a more informative one
+ return result;
+ }
+ reportRelationError(headMessage, source, target);
}
- result = [sourceProperty];
}
}
- return result;
- }
- function isOrContainsMatchingReference(source, target) {
- return isMatchingReference(source, target) || containsMatchingReference(source, target);
- }
- function hasMatchingArgument(callExpression, reference) {
- if (callExpression.arguments) {
- for (var _i = 0, _a = callExpression.arguments; _i < _a.length; _i++) {
- var argument = _a[_i];
- if (isOrContainsMatchingReference(reference, argument)) {
- return true;
+ function isIdenticalTo(source, target) {
+ var flags = source.flags & target.flags;
+ if (!(flags & 469237760 /* Substructure */)) {
+ return 0 /* False */;
+ }
+ if (flags & 3145728 /* UnionOrIntersection */) {
+ var result_6 = eachTypeRelatedToSomeType(source, target);
+ if (result_6) {
+ result_6 &= eachTypeRelatedToSomeType(target, source);
}
+ return result_6;
}
+ return recursiveTypeRelatedTo(source, target, /*reportErrors*/ false, 0 /* None */);
}
- if (callExpression.expression.kind === 194 /* PropertyAccessExpression */ &&
- isOrContainsMatchingReference(reference, callExpression.expression.expression)) {
- return true;
- }
- return false;
- }
- function getFlowNodeId(flow) {
- if (!flow.id || flow.id < 0) {
- flow.id = nextFlowId;
- nextFlowId++;
- }
- return flow.id;
- }
- function typeMaybeAssignableTo(source, target) {
- if (!(source.flags & 1048576 /* Union */)) {
- return isTypeAssignableTo(source, target);
- }
- for (var _i = 0, _a = source.types; _i < _a.length; _i++) {
- var t = _a[_i];
- if (isTypeAssignableTo(t, target)) {
- return true;
- }
+ function getTypeOfPropertyInTypes(types, name) {
+ var appendPropType = function (propTypes, type) {
+ type = getApparentType(type);
+ var prop = type.flags & 3145728 /* UnionOrIntersection */ ? getPropertyOfUnionOrIntersectionType(type, name) : getPropertyOfObjectType(type, name);
+ var propType = prop && getTypeOfSymbol(prop) || isNumericLiteralName(name) && getIndexTypeOfType(type, 1 /* Number */) || getIndexTypeOfType(type, 0 /* String */) || undefinedType;
+ return ts.append(propTypes, propType);
+ };
+ return getUnionType(ts.reduceLeft(types, appendPropType, /*initial*/ undefined) || ts.emptyArray);
}
- return false;
- }
- // Remove those constituent types of declaredType to which no constituent type of assignedType is assignable.
- // For example, when a variable of type number | string | boolean is assigned a value of type number | boolean,
- // we remove type string.
- function getAssignmentReducedType(declaredType, assignedType) {
- if (declaredType !== assignedType) {
- if (assignedType.flags & 131072 /* Never */) {
- return assignedType;
+ function hasExcessProperties(source, target, reportErrors) {
+ if (!isExcessPropertyCheckTarget(target) || !noImplicitAny && ts.getObjectFlags(target) & 16384 /* JSLiteral */) {
+ return false; // Disable excess property checks on JS literals to simulate having an implicit "index signature" - but only outside of noImplicitAny
}
- var reducedType = filterType(declaredType, function (t) { return typeMaybeAssignableTo(assignedType, t); });
- if (assignedType.flags & 512 /* BooleanLiteral */ && isFreshLiteralType(assignedType)) {
- reducedType = mapType(reducedType, getFreshTypeOfLiteralType); // Ensure that if the assignment is a fresh type, that we narrow to fresh types
+ var isComparingJsxAttributes = !!(ts.getObjectFlags(source) & 4096 /* JsxAttributes */);
+ if ((relation === assignableRelation || relation === comparableRelation) &&
+ (isTypeSubsetOf(globalObjectType, target) || (!isComparingJsxAttributes && isEmptyObjectType(target)))) {
+ return false;
}
- // Our crude heuristic produces an invalid result in some cases: see GH#26130.
- // For now, when that happens, we give up and don't narrow at all. (This also
- // means we'll never narrow for erroneous assignments where the assigned type
- // is not assignable to the declared type.)
- if (isTypeAssignableTo(assignedType, reducedType)) {
- return reducedType;
+ var reducedTarget = target;
+ var checkTypes;
+ if (target.flags & 1048576 /* Union */) {
+ reducedTarget = findMatchingDiscriminantType(source, target, isRelatedTo) || filterPrimitivesIfContainsNonPrimitive(target);
+ checkTypes = reducedTarget.flags & 1048576 /* Union */ ? reducedTarget.types : [reducedTarget];
}
- }
- return declaredType;
- }
- function getTypeFactsOfTypes(types) {
- var result = 0 /* None */;
- for (var _i = 0, types_15 = types; _i < types_15.length; _i++) {
- var t = types_15[_i];
- result |= getTypeFacts(t);
- }
- return result;
- }
- function isFunctionObjectType(type) {
- // We do a quick check for a "bind" property before performing the more expensive subtype
- // check. This gives us a quicker out in the common case where an object type is not a function.
- var resolved = resolveStructuredTypeMembers(type);
- return !!(resolved.callSignatures.length || resolved.constructSignatures.length ||
- resolved.members.get("bind") && isTypeSubtypeOf(type, globalFunctionType));
- }
- function getTypeFacts(type) {
- var flags = type.flags;
- if (flags & 4 /* String */) {
- return strictNullChecks ? 16317953 /* StringStrictFacts */ : 16776705 /* StringFacts */;
- }
- if (flags & 128 /* StringLiteral */) {
- var isEmpty = type.value === "";
- return strictNullChecks ?
- isEmpty ? 12123649 /* EmptyStringStrictFacts */ : 7929345 /* NonEmptyStringStrictFacts */ :
- isEmpty ? 12582401 /* EmptyStringFacts */ : 16776705 /* NonEmptyStringFacts */;
- }
- if (flags & (8 /* Number */ | 32 /* Enum */)) {
- return strictNullChecks ? 16317698 /* NumberStrictFacts */ : 16776450 /* NumberFacts */;
- }
- if (flags & 256 /* NumberLiteral */) {
- var isZero = type.value === 0;
- return strictNullChecks ?
- isZero ? 12123394 /* ZeroNumberStrictFacts */ : 7929090 /* NonZeroNumberStrictFacts */ :
- isZero ? 12582146 /* ZeroNumberFacts */ : 16776450 /* NonZeroNumberFacts */;
- }
- if (flags & 64 /* BigInt */) {
- return strictNullChecks ? 16317188 /* BigIntStrictFacts */ : 16775940 /* BigIntFacts */;
- }
- if (flags & 2048 /* BigIntLiteral */) {
- var isZero = isZeroBigInt(type);
- return strictNullChecks ?
- isZero ? 12122884 /* ZeroBigIntStrictFacts */ : 7928580 /* NonZeroBigIntStrictFacts */ :
- isZero ? 12581636 /* ZeroBigIntFacts */ : 16775940 /* NonZeroBigIntFacts */;
- }
- if (flags & 16 /* Boolean */) {
- return strictNullChecks ? 16316168 /* BooleanStrictFacts */ : 16774920 /* BooleanFacts */;
- }
- if (flags & 528 /* BooleanLike */) {
- return strictNullChecks ?
- (type === falseType || type === regularFalseType) ? 12121864 /* FalseStrictFacts */ : 7927560 /* TrueStrictFacts */ :
- (type === falseType || type === regularFalseType) ? 12580616 /* FalseFacts */ : 16774920 /* TrueFacts */;
- }
- if (flags & 524288 /* Object */) {
- return ts.getObjectFlags(type) & 16 /* Anonymous */ && isEmptyObjectType(type) ?
- strictNullChecks ? 16318463 /* EmptyObjectStrictFacts */ : 16777215 /* EmptyObjectFacts */ :
- isFunctionObjectType(type) ?
- strictNullChecks ? 7880640 /* FunctionStrictFacts */ : 16728000 /* FunctionFacts */ :
- strictNullChecks ? 7888800 /* ObjectStrictFacts */ : 16736160 /* ObjectFacts */;
- }
- if (flags & (16384 /* Void */ | 32768 /* Undefined */)) {
- return 9830144 /* UndefinedFacts */;
- }
- if (flags & 65536 /* Null */) {
- return 9363232 /* NullFacts */;
- }
- if (flags & 12288 /* ESSymbolLike */) {
- return strictNullChecks ? 7925520 /* SymbolStrictFacts */ : 16772880 /* SymbolFacts */;
- }
- if (flags & 67108864 /* NonPrimitive */) {
- return strictNullChecks ? 7888800 /* ObjectStrictFacts */ : 16736160 /* ObjectFacts */;
- }
- if (flags & 131072 /* Never */) {
- return 0 /* None */;
- }
- if (flags & 63176704 /* Instantiable */) {
- return getTypeFacts(getBaseConstraintOfType(type) || unknownType);
- }
- if (flags & 3145728 /* UnionOrIntersection */) {
- return getTypeFactsOfTypes(type.types);
- }
- return 16777215 /* All */;
- }
- function getTypeWithFacts(type, include) {
- return filterType(type, function (t) { return (getTypeFacts(t) & include) !== 0; });
- }
- function getTypeWithDefault(type, defaultExpression) {
- if (defaultExpression) {
- var defaultType = getTypeOfExpression(defaultExpression);
- return getUnionType([getTypeWithFacts(type, 524288 /* NEUndefined */), defaultType]);
- }
- return type;
- }
- function getTypeOfDestructuredProperty(type, name) {
- var nameType = getLiteralTypeFromPropertyName(name);
- if (!isTypeUsableAsPropertyName(nameType))
- return errorType;
- var text = getPropertyNameFromType(nameType);
- return getConstraintForLocation(getTypeOfPropertyOfType(type, text), name) ||
- isNumericLiteralName(text) && getIndexTypeOfType(type, 1 /* Number */) ||
- getIndexTypeOfType(type, 0 /* String */) ||
- errorType;
- }
- function getTypeOfDestructuredArrayElement(type, index) {
- return everyType(type, isTupleLikeType) && getTupleElementType(type, index) ||
- checkIteratedTypeOrElementType(65 /* Destructuring */, type, undefinedType, /*errorNode*/ undefined) ||
- errorType;
- }
- function getTypeOfDestructuredSpreadExpression(type) {
- return createArrayType(checkIteratedTypeOrElementType(65 /* Destructuring */, type, undefinedType, /*errorNode*/ undefined) || errorType);
- }
- function getAssignedTypeOfBinaryExpression(node) {
- var isDestructuringDefaultAssignment = node.parent.kind === 192 /* ArrayLiteralExpression */ && isDestructuringAssignmentTarget(node.parent) ||
- node.parent.kind === 281 /* PropertyAssignment */ && isDestructuringAssignmentTarget(node.parent.parent);
- return isDestructuringDefaultAssignment ?
- getTypeWithDefault(getAssignedType(node), node.right) :
- getTypeOfExpression(node.right);
- }
- function isDestructuringAssignmentTarget(parent) {
- return parent.parent.kind === 209 /* BinaryExpression */ && parent.parent.left === parent ||
- parent.parent.kind === 232 /* ForOfStatement */ && parent.parent.initializer === parent;
- }
- function getAssignedTypeOfArrayLiteralElement(node, element) {
- return getTypeOfDestructuredArrayElement(getAssignedType(node), node.elements.indexOf(element));
- }
- function getAssignedTypeOfSpreadExpression(node) {
- return getTypeOfDestructuredSpreadExpression(getAssignedType(node.parent));
- }
- function getAssignedTypeOfPropertyAssignment(node) {
- return getTypeOfDestructuredProperty(getAssignedType(node.parent), node.name);
- }
- function getAssignedTypeOfShorthandPropertyAssignment(node) {
- return getTypeWithDefault(getAssignedTypeOfPropertyAssignment(node), node.objectAssignmentInitializer);
- }
- function getAssignedType(node) {
- var parent = node.parent;
- switch (parent.kind) {
- case 231 /* ForInStatement */:
- return stringType;
- case 232 /* ForOfStatement */:
- return checkRightHandSideOfForOf(parent) || errorType;
- case 209 /* BinaryExpression */:
- return getAssignedTypeOfBinaryExpression(parent);
- case 203 /* DeleteExpression */:
- return undefinedType;
- case 192 /* ArrayLiteralExpression */:
- return getAssignedTypeOfArrayLiteralElement(parent, node);
- case 213 /* SpreadElement */:
- return getAssignedTypeOfSpreadExpression(parent);
- case 281 /* PropertyAssignment */:
- return getAssignedTypeOfPropertyAssignment(parent);
- case 282 /* ShorthandPropertyAssignment */:
- return getAssignedTypeOfShorthandPropertyAssignment(parent);
- }
- return errorType;
- }
- function getInitialTypeOfBindingElement(node) {
- var pattern = node.parent;
- var parentType = getInitialType(pattern.parent);
- var type = pattern.kind === 189 /* ObjectBindingPattern */ ?
- getTypeOfDestructuredProperty(parentType, node.propertyName || node.name) :
- !node.dotDotDotToken ?
- getTypeOfDestructuredArrayElement(parentType, pattern.elements.indexOf(node)) :
- getTypeOfDestructuredSpreadExpression(parentType);
- return getTypeWithDefault(type, node.initializer);
- }
- function getTypeOfInitializer(node) {
- // Return the cached type if one is available. If the type of the variable was inferred
- // from its initializer, we'll already have cached the type. Otherwise we compute it now
- // without caching such that transient types are reflected.
- var links = getNodeLinks(node);
- return links.resolvedType || getTypeOfExpression(node);
- }
- function getInitialTypeOfVariableDeclaration(node) {
- if (node.initializer) {
- return getTypeOfInitializer(node.initializer);
- }
- if (node.parent.parent.kind === 231 /* ForInStatement */) {
- return stringType;
- }
- if (node.parent.parent.kind === 232 /* ForOfStatement */) {
- return checkRightHandSideOfForOf(node.parent.parent) || errorType;
- }
- return errorType;
- }
- function getInitialType(node) {
- return node.kind === 242 /* VariableDeclaration */ ?
- getInitialTypeOfVariableDeclaration(node) :
- getInitialTypeOfBindingElement(node);
- }
- function isEmptyArrayAssignment(node) {
- return node.kind === 242 /* VariableDeclaration */ && node.initializer &&
- isEmptyArrayLiteral(node.initializer) ||
- node.kind !== 191 /* BindingElement */ && node.parent.kind === 209 /* BinaryExpression */ &&
- isEmptyArrayLiteral(node.parent.right);
- }
- function getReferenceCandidate(node) {
- switch (node.kind) {
- case 200 /* ParenthesizedExpression */:
- return getReferenceCandidate(node.expression);
- case 209 /* BinaryExpression */:
- switch (node.operatorToken.kind) {
- case 62 /* EqualsToken */:
- return getReferenceCandidate(node.left);
- case 27 /* CommaToken */:
- return getReferenceCandidate(node.right);
+ var _loop_16 = function (prop) {
+ if (shouldCheckAsExcessProperty(prop, source.symbol) && !isIgnoredJsxProperty(source, prop)) {
+ if (!isKnownProperty(reducedTarget, prop.escapedName, isComparingJsxAttributes)) {
+ if (reportErrors) {
+ // Report error in terms of object types in the target as those are the only ones
+ // we check in isKnownProperty.
+ var errorTarget = filterType(reducedTarget, isExcessPropertyCheckTarget);
+ // We know *exactly* where things went wrong when comparing the types.
+ // Use this property as the error node as this will be more helpful in
+ // reasoning about what went wrong.
+ if (!errorNode)
+ return { value: ts.Debug.fail() };
+ if (ts.isJsxAttributes(errorNode) || ts.isJsxOpeningLikeElement(errorNode) || ts.isJsxOpeningLikeElement(errorNode.parent)) {
+ // JsxAttributes has an object-literal flag and undergo same type-assignablity check as normal object-literal.
+ // However, using an object-literal error message will be very confusing to the users so we give different a message.
+ if (prop.valueDeclaration && ts.isJsxAttribute(prop.valueDeclaration) && ts.getSourceFileOfNode(errorNode) === ts.getSourceFileOfNode(prop.valueDeclaration.name)) {
+ // Note that extraneous children (as in `extra`) don't pass this check,
+ // since `children` is a SyntaxKind.PropertySignature instead of a SyntaxKind.JsxAttribute.
+ errorNode = prop.valueDeclaration.name;
+ }
+ var propName = symbolToString(prop);
+ var suggestionSymbol = getSuggestedSymbolForNonexistentJSXAttribute(propName, errorTarget);
+ var suggestion = suggestionSymbol ? symbolToString(suggestionSymbol) : undefined;
+ if (suggestion) {
+ reportError(ts.Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2, propName, typeToString(errorTarget), suggestion);
+ }
+ else {
+ reportError(ts.Diagnostics.Property_0_does_not_exist_on_type_1, propName, typeToString(errorTarget));
+ }
+ }
+ else {
+ // use the property's value declaration if the property is assigned inside the literal itself
+ var objectLiteralDeclaration_1 = source.symbol && ts.firstOrUndefined(source.symbol.declarations);
+ var suggestion = void 0;
+ if (prop.valueDeclaration && ts.findAncestor(prop.valueDeclaration, function (d) { return d === objectLiteralDeclaration_1; }) && ts.getSourceFileOfNode(objectLiteralDeclaration_1) === ts.getSourceFileOfNode(errorNode)) {
+ var propDeclaration = prop.valueDeclaration;
+ ts.Debug.assertNode(propDeclaration, ts.isObjectLiteralElementLike);
+ errorNode = propDeclaration;
+ var name = propDeclaration.name;
+ if (ts.isIdentifier(name)) {
+ suggestion = getSuggestionForNonexistentProperty(name, errorTarget);
+ }
+ }
+ if (suggestion !== undefined) {
+ reportError(ts.Diagnostics.Object_literal_may_only_specify_known_properties_but_0_does_not_exist_in_type_1_Did_you_mean_to_write_2, symbolToString(prop), typeToString(errorTarget), suggestion);
+ }
+ else {
+ reportError(ts.Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, symbolToString(prop), typeToString(errorTarget));
+ }
+ }
+ }
+ return { value: true };
+ }
+ if (checkTypes && !isRelatedTo(getTypeOfSymbol(prop), getTypeOfPropertyInTypes(checkTypes, prop.escapedName), reportErrors)) {
+ if (reportErrors) {
+ reportIncompatibleError(ts.Diagnostics.Types_of_property_0_are_incompatible, symbolToString(prop));
+ }
+ return { value: true };
+ }
}
+ };
+ for (var _i = 0, _a = getPropertiesOfType(source); _i < _a.length; _i++) {
+ var prop = _a[_i];
+ var state_5 = _loop_16(prop);
+ if (typeof state_5 === "object")
+ return state_5.value;
+ }
+ return false;
}
- return node;
- }
- function getReferenceRoot(node) {
- var parent = node.parent;
- return parent.kind === 200 /* ParenthesizedExpression */ ||
- parent.kind === 209 /* BinaryExpression */ && parent.operatorToken.kind === 62 /* EqualsToken */ && parent.left === node ||
- parent.kind === 209 /* BinaryExpression */ && parent.operatorToken.kind === 27 /* CommaToken */ && parent.right === node ?
- getReferenceRoot(parent) : node;
- }
- function getTypeOfSwitchClause(clause) {
- if (clause.kind === 277 /* CaseClause */) {
- return getRegularTypeOfLiteralType(getTypeOfExpression(clause.expression));
+ function shouldCheckAsExcessProperty(prop, container) {
+ return prop.valueDeclaration && container.valueDeclaration && prop.valueDeclaration.parent === container.valueDeclaration;
}
- return neverType;
- }
- function getSwitchClauseTypes(switchStatement) {
- var links = getNodeLinks(switchStatement);
- if (!links.switchTypes) {
- links.switchTypes = [];
- for (var _i = 0, _a = switchStatement.caseBlock.clauses; _i < _a.length; _i++) {
- var clause = _a[_i];
- links.switchTypes.push(getTypeOfSwitchClause(clause));
+ function eachTypeRelatedToSomeType(source, target) {
+ var result = -1 /* True */;
+ var sourceTypes = source.types;
+ for (var _i = 0, sourceTypes_1 = sourceTypes; _i < sourceTypes_1.length; _i++) {
+ var sourceType = sourceTypes_1[_i];
+ var related = typeRelatedToSomeType(sourceType, target, /*reportErrors*/ false);
+ if (!related) {
+ return 0 /* False */;
+ }
+ result &= related;
}
+ return result;
}
- return links.switchTypes;
- }
- function getSwitchClauseTypeOfWitnesses(switchStatement, retainDefault) {
- var witnesses = [];
- for (var _i = 0, _a = switchStatement.caseBlock.clauses; _i < _a.length; _i++) {
- var clause = _a[_i];
- if (clause.kind === 277 /* CaseClause */) {
- if (ts.isStringLiteralLike(clause.expression)) {
- witnesses.push(clause.expression.text);
- continue;
+ function typeRelatedToSomeType(source, target, reportErrors) {
+ var targetTypes = target.types;
+ if (target.flags & 1048576 /* Union */ && containsType(targetTypes, source)) {
+ return -1 /* True */;
+ }
+ for (var _i = 0, targetTypes_1 = targetTypes; _i < targetTypes_1.length; _i++) {
+ var type = targetTypes_1[_i];
+ var related = isRelatedTo(source, type, /*reportErrors*/ false);
+ if (related) {
+ return related;
}
- return ts.emptyArray;
}
- if (retainDefault)
- witnesses.push(/*explicitDefaultStatement*/ undefined);
+ if (reportErrors) {
+ var bestMatchingType = getBestMatchingType(source, target, isRelatedTo);
+ isRelatedTo(source, bestMatchingType || targetTypes[targetTypes.length - 1], /*reportErrors*/ true);
+ }
+ return 0 /* False */;
}
- return witnesses;
- }
- function eachTypeContainedIn(source, types) {
- return source.flags & 1048576 /* Union */ ? !ts.forEach(source.types, function (t) { return !ts.contains(types, t); }) : ts.contains(types, source);
- }
- function isTypeSubsetOf(source, target) {
- return source === target || target.flags & 1048576 /* Union */ && isTypeSubsetOfUnion(source, target);
- }
- function isTypeSubsetOfUnion(source, target) {
- if (source.flags & 1048576 /* Union */) {
- for (var _i = 0, _a = source.types; _i < _a.length; _i++) {
- var t = _a[_i];
- if (!containsType(target.types, t)) {
- return false;
+ function typeRelatedToEachType(source, target, reportErrors, intersectionState) {
+ var result = -1 /* True */;
+ var targetTypes = target.types;
+ for (var _i = 0, targetTypes_2 = targetTypes; _i < targetTypes_2.length; _i++) {
+ var targetType = targetTypes_2[_i];
+ var related = isRelatedTo(source, targetType, reportErrors, /*headMessage*/ undefined, intersectionState);
+ if (!related) {
+ return 0 /* False */;
}
+ result &= related;
}
- return true;
- }
- if (source.flags & 1024 /* EnumLiteral */ && getBaseTypeOfEnumLiteralType(source) === target) {
- return true;
+ return result;
}
- return containsType(target.types, source);
- }
- function forEachType(type, f) {
- return type.flags & 1048576 /* Union */ ? ts.forEach(type.types, f) : f(type);
- }
- function everyType(type, f) {
- return type.flags & 1048576 /* Union */ ? ts.every(type.types, f) : f(type);
- }
- function filterType(type, f) {
- if (type.flags & 1048576 /* Union */) {
- var types = type.types;
- var filtered = ts.filter(types, f);
- return filtered === types ? type : getUnionTypeFromSortedList(filtered, type.objectFlags);
+ function someTypeRelatedToType(source, target, reportErrors, intersectionState) {
+ var sourceTypes = source.types;
+ if (source.flags & 1048576 /* Union */ && containsType(sourceTypes, target)) {
+ return -1 /* True */;
+ }
+ var len = sourceTypes.length;
+ for (var i = 0; i < len; i++) {
+ var related = isRelatedTo(sourceTypes[i], target, reportErrors && i === len - 1, /*headMessage*/ undefined, intersectionState);
+ if (related) {
+ return related;
+ }
+ }
+ return 0 /* False */;
}
- return type.flags & 131072 /* Never */ || f(type) ? type : neverType;
- }
- function countTypes(type) {
- return type.flags & 1048576 /* Union */ ? type.types.length : 1;
- }
- function mapType(type, mapper, noReductions) {
- if (type.flags & 131072 /* Never */) {
- return type;
+ function eachTypeRelatedToType(source, target, reportErrors, intersectionState) {
+ var result = -1 /* True */;
+ var sourceTypes = source.types;
+ for (var i = 0; i < sourceTypes.length; i++) {
+ var sourceType = sourceTypes[i];
+ if (target.flags & 1048576 /* Union */ && target.types.length === sourceTypes.length) {
+ // many unions are mappings of one another; in such cases, simply comparing members at the same index can shortcut the comparison
+ var related_1 = isRelatedTo(sourceType, target.types[i], /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState);
+ if (related_1) {
+ result &= related_1;
+ continue;
+ }
+ }
+ var related = isRelatedTo(sourceType, target, reportErrors, /*headMessage*/ undefined, intersectionState);
+ if (!related) {
+ return 0 /* False */;
+ }
+ result &= related;
+ }
+ return result;
}
- if (!(type.flags & 1048576 /* Union */)) {
- return mapper(type);
+ function typeArgumentsRelatedTo(sources, targets, variances, reportErrors, intersectionState) {
+ if (sources === void 0) { sources = ts.emptyArray; }
+ if (targets === void 0) { targets = ts.emptyArray; }
+ if (variances === void 0) { variances = ts.emptyArray; }
+ if (sources.length !== targets.length && relation === identityRelation) {
+ return 0 /* False */;
+ }
+ var length = sources.length <= targets.length ? sources.length : targets.length;
+ var result = -1 /* True */;
+ for (var i = 0; i < length; i++) {
+ // When variance information isn't available we default to covariance. This happens
+ // in the process of computing variance information for recursive types and when
+ // comparing 'this' type arguments.
+ var varianceFlags = i < variances.length ? variances[i] : 1 /* Covariant */;
+ var variance = varianceFlags & 7 /* VarianceMask */;
+ // We ignore arguments for independent type parameters (because they're never witnessed).
+ if (variance !== 4 /* Independent */) {
+ var s = sources[i];
+ var t = targets[i];
+ var related = -1 /* True */;
+ if (varianceFlags & 8 /* Unmeasurable */) {
+ // Even an `Unmeasurable` variance works out without a structural check if the source and target are _identical_.
+ // We can't simply assume invariance, because `Unmeasurable` marks nonlinear relations, for example, a relation tained by
+ // the `-?` modifier in a mapped type (where, no matter how the inputs are related, the outputs still might not be)
+ related = relation === identityRelation ? isRelatedTo(s, t, /*reportErrors*/ false) : compareTypesIdentical(s, t);
+ }
+ else if (variance === 1 /* Covariant */) {
+ related = isRelatedTo(s, t, reportErrors, /*headMessage*/ undefined, intersectionState);
+ }
+ else if (variance === 2 /* Contravariant */) {
+ related = isRelatedTo(t, s, reportErrors, /*headMessage*/ undefined, intersectionState);
+ }
+ else if (variance === 3 /* Bivariant */) {
+ // In the bivariant case we first compare contravariantly without reporting
+ // errors. Then, if that doesn't succeed, we compare covariantly with error
+ // reporting. Thus, error elaboration will be based on the the covariant check,
+ // which is generally easier to reason about.
+ related = isRelatedTo(t, s, /*reportErrors*/ false);
+ if (!related) {
+ related = isRelatedTo(s, t, reportErrors, /*headMessage*/ undefined, intersectionState);
+ }
+ }
+ else {
+ // In the invariant case we first compare covariantly, and only when that
+ // succeeds do we proceed to compare contravariantly. Thus, error elaboration
+ // will typically be based on the covariant check.
+ related = isRelatedTo(s, t, reportErrors, /*headMessage*/ undefined, intersectionState);
+ if (related) {
+ related &= isRelatedTo(t, s, reportErrors, /*headMessage*/ undefined, intersectionState);
+ }
+ }
+ if (!related) {
+ return 0 /* False */;
+ }
+ result &= related;
+ }
+ }
+ return result;
}
- var mappedTypes;
- for (var _i = 0, _a = type.types; _i < _a.length; _i++) {
- var t = _a[_i];
- var mapped = mapper(t);
- if (mapped) {
- if (!mappedTypes) {
- mappedTypes = [mapped];
+ // Determine if possibly recursive types are related. First, check if the result is already available in the global cache.
+ // Second, check if we have already started a comparison of the given two types in which case we assume the result to be true.
+ // Third, check if both types are part of deeply nested chains of generic type instantiations and if so assume the types are
+ // equal and infinitely expanding. Fourth, if we have reached a depth of 100 nested comparisons, assume we have runaway recursion
+ // and issue an error. Otherwise, actually compare the structure of the two types.
+ function recursiveTypeRelatedTo(source, target, reportErrors, intersectionState) {
+ if (overflow) {
+ return 0 /* False */;
+ }
+ var id = getRelationKey(source, target, intersectionState | (inPropertyCheck ? 8 /* InPropertyCheck */ : 0), relation);
+ var entry = relation.get(id);
+ if (entry !== undefined) {
+ if (reportErrors && entry & 2 /* Failed */ && !(entry & 4 /* Reported */)) {
+ // We are elaborating errors and the cached result is an unreported failure. The result will be reported
+ // as a failure, and should be updated as a reported failure by the bottom of this function.
}
else {
- mappedTypes.push(mapped);
+ if (outofbandVarianceMarkerHandler) {
+ // We're in the middle of variance checking - integrate any unmeasurable/unreliable flags from this cached component
+ var saved = entry & 24 /* ReportsMask */;
+ if (saved & 8 /* ReportsUnmeasurable */) {
+ instantiateType(source, makeFunctionTypeMapper(reportUnmeasurableMarkers));
+ }
+ if (saved & 16 /* ReportsUnreliable */) {
+ instantiateType(source, makeFunctionTypeMapper(reportUnreliableMarkers));
+ }
+ }
+ return entry & 1 /* Succeeded */ ? -1 /* True */ : 0 /* False */;
}
}
- }
- return mappedTypes && getUnionType(mappedTypes, noReductions ? 0 /* None */ : 1 /* Literal */);
- }
- function extractTypesOfKind(type, kind) {
- return filterType(type, function (t) { return (t.flags & kind) !== 0; });
- }
- // Return a new type in which occurrences of the string and number primitive types in
- // typeWithPrimitives have been replaced with occurrences of string literals and numeric
- // literals in typeWithLiterals, respectively.
- function replacePrimitivesWithLiterals(typeWithPrimitives, typeWithLiterals) {
- if (isTypeSubsetOf(stringType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 128 /* StringLiteral */) ||
- isTypeSubsetOf(numberType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 256 /* NumberLiteral */) ||
- isTypeSubsetOf(bigintType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 2048 /* BigIntLiteral */)) {
- return mapType(typeWithPrimitives, function (t) {
- return t.flags & 4 /* String */ ? extractTypesOfKind(typeWithLiterals, 4 /* String */ | 128 /* StringLiteral */) :
- t.flags & 8 /* Number */ ? extractTypesOfKind(typeWithLiterals, 8 /* Number */ | 256 /* NumberLiteral */) :
- t.flags & 64 /* BigInt */ ? extractTypesOfKind(typeWithLiterals, 64 /* BigInt */ | 2048 /* BigIntLiteral */) : t;
- });
- }
- return typeWithPrimitives;
- }
- function isIncomplete(flowType) {
- return flowType.flags === 0;
- }
- function getTypeFromFlowType(flowType) {
- return flowType.flags === 0 ? flowType.type : flowType;
- }
- function createFlowType(type, incomplete) {
- return incomplete ? { flags: 0, type: type } : type;
- }
- // An evolving array type tracks the element types that have so far been seen in an
- // 'x.push(value)' or 'x[n] = value' operation along the control flow graph. Evolving
- // array types are ultimately converted into manifest array types (using getFinalArrayType)
- // and never escape the getFlowTypeOfReference function.
- function createEvolvingArrayType(elementType) {
- var result = createObjectType(256 /* EvolvingArray */);
- result.elementType = elementType;
- return result;
- }
- function getEvolvingArrayType(elementType) {
- return evolvingArrayTypes[elementType.id] || (evolvingArrayTypes[elementType.id] = createEvolvingArrayType(elementType));
- }
- // When adding evolving array element types we do not perform subtype reduction. Instead,
- // we defer subtype reduction until the evolving array type is finalized into a manifest
- // array type.
- function addEvolvingArrayElementType(evolvingArrayType, node) {
- var elementType = getBaseTypeOfLiteralType(getContextFreeTypeOfExpression(node));
- return isTypeSubsetOf(elementType, evolvingArrayType.elementType) ? evolvingArrayType : getEvolvingArrayType(getUnionType([evolvingArrayType.elementType, elementType]));
- }
- function createFinalArrayType(elementType) {
- return elementType.flags & 131072 /* Never */ ?
- autoArrayType :
- createArrayType(elementType.flags & 1048576 /* Union */ ?
- getUnionType(elementType.types, 2 /* Subtype */) :
- elementType);
- }
- // We perform subtype reduction upon obtaining the final array type from an evolving array type.
- function getFinalArrayType(evolvingArrayType) {
- return evolvingArrayType.finalArrayType || (evolvingArrayType.finalArrayType = createFinalArrayType(evolvingArrayType.elementType));
- }
- function finalizeEvolvingArrayType(type) {
- return ts.getObjectFlags(type) & 256 /* EvolvingArray */ ? getFinalArrayType(type) : type;
- }
- function getElementTypeOfEvolvingArrayType(type) {
- return ts.getObjectFlags(type) & 256 /* EvolvingArray */ ? type.elementType : neverType;
- }
- function isEvolvingArrayTypeList(types) {
- var hasEvolvingArrayType = false;
- for (var _i = 0, types_16 = types; _i < types_16.length; _i++) {
- var t = types_16[_i];
- if (!(t.flags & 131072 /* Never */)) {
- if (!(ts.getObjectFlags(t) & 256 /* EvolvingArray */)) {
- return false;
+ if (!maybeKeys) {
+ maybeKeys = [];
+ sourceStack = [];
+ targetStack = [];
+ }
+ else {
+ for (var i = 0; i < maybeCount; i++) {
+ // If source and target are already being compared, consider them related with assumptions
+ if (id === maybeKeys[i]) {
+ return 3 /* Maybe */;
+ }
}
- hasEvolvingArrayType = true;
+ if (depth === 100) {
+ overflow = true;
+ return 0 /* False */;
+ }
+ }
+ var maybeStart = maybeCount;
+ maybeKeys[maybeCount] = id;
+ maybeCount++;
+ sourceStack[depth] = source;
+ targetStack[depth] = target;
+ depth++;
+ var saveExpandingFlags = expandingFlags;
+ if (!(expandingFlags & 1 /* Source */) && isDeeplyNestedType(source, sourceStack, depth))
+ expandingFlags |= 1 /* Source */;
+ if (!(expandingFlags & 2 /* Target */) && isDeeplyNestedType(target, targetStack, depth))
+ expandingFlags |= 2 /* Target */;
+ var originalHandler;
+ var propagatingVarianceFlags = 0;
+ if (outofbandVarianceMarkerHandler) {
+ originalHandler = outofbandVarianceMarkerHandler;
+ outofbandVarianceMarkerHandler = function (onlyUnreliable) {
+ propagatingVarianceFlags |= onlyUnreliable ? 16 /* ReportsUnreliable */ : 8 /* ReportsUnmeasurable */;
+ return originalHandler(onlyUnreliable);
+ };
+ }
+ if (expandingFlags === 3 /* Both */) {
+ ts.tracing.instant("check" /* Check */, "recursiveTypeRelatedTo_DepthLimit", {
+ sourceId: source.id,
+ sourceIdStack: sourceStack.map(function (t) { return t.id; }),
+ targetId: target.id,
+ targetIdStack: targetStack.map(function (t) { return t.id; }),
+ depth: depth,
+ });
+ }
+ var result = expandingFlags !== 3 /* Both */ ? structuredTypeRelatedTo(source, target, reportErrors, intersectionState) : 3 /* Maybe */;
+ if (outofbandVarianceMarkerHandler) {
+ outofbandVarianceMarkerHandler = originalHandler;
+ }
+ expandingFlags = saveExpandingFlags;
+ depth--;
+ if (result) {
+ if (result === -1 /* True */ || depth === 0) {
+ if (result === -1 /* True */ || result === 3 /* Maybe */) {
+ // If result is definitely true, record all maybe keys as having succeeded. Also, record Ternary.Maybe
+ // results as having succeeded once we reach depth 0, but never record Ternary.Unknown results.
+ for (var i = maybeStart; i < maybeCount; i++) {
+ relation.set(maybeKeys[i], 1 /* Succeeded */ | propagatingVarianceFlags);
+ }
+ }
+ maybeCount = maybeStart;
+ }
+ }
+ else {
+ // A false result goes straight into global cache (when something is false under
+ // assumptions it will also be false without assumptions)
+ relation.set(id, (reportErrors ? 4 /* Reported */ : 0) | 2 /* Failed */ | propagatingVarianceFlags);
+ maybeCount = maybeStart;
}
+ return result;
}
- return hasEvolvingArrayType;
- }
- // At flow control branch or loop junctions, if the type along every antecedent code path
- // is an evolving array type, we construct a combined evolving array type. Otherwise we
- // finalize all evolving array types.
- function getUnionOrEvolvingArrayType(types, subtypeReduction) {
- return isEvolvingArrayTypeList(types) ?
- getEvolvingArrayType(getUnionType(ts.map(types, getElementTypeOfEvolvingArrayType))) :
- getUnionType(ts.sameMap(types, finalizeEvolvingArrayType), subtypeReduction);
- }
- // Return true if the given node is 'x' in an 'x.length', x.push(value)', 'x.unshift(value)' or
- // 'x[n] = value' operation, where 'n' is an expression of type any, undefined, or a number-like type.
- function isEvolvingArrayOperationTarget(node) {
- var root = getReferenceRoot(node);
- var parent = root.parent;
- var isLengthPushOrUnshift = ts.isPropertyAccessExpression(parent) && (parent.name.escapedText === "length" ||
- parent.parent.kind === 196 /* CallExpression */
- && ts.isIdentifier(parent.name)
- && ts.isPushOrUnshiftIdentifier(parent.name));
- var isElementAssignment = parent.kind === 195 /* ElementAccessExpression */ &&
- parent.expression === root &&
- parent.parent.kind === 209 /* BinaryExpression */ &&
- parent.parent.operatorToken.kind === 62 /* EqualsToken */ &&
- parent.parent.left === parent &&
- !ts.isAssignmentTarget(parent.parent) &&
- isTypeAssignableToKind(getTypeOfExpression(parent.argumentExpression), 296 /* NumberLike */);
- return isLengthPushOrUnshift || isElementAssignment;
- }
- function isDeclarationWithExplicitTypeAnnotation(declaration) {
- return (declaration.kind === 242 /* VariableDeclaration */ || declaration.kind === 156 /* Parameter */ ||
- declaration.kind === 159 /* PropertyDeclaration */ || declaration.kind === 158 /* PropertySignature */) &&
- !!ts.getEffectiveTypeAnnotationNode(declaration);
- }
- function getExplicitTypeOfSymbol(symbol, diagnostic) {
- if (symbol.flags & (16 /* Function */ | 8192 /* Method */ | 32 /* Class */ | 512 /* ValueModule */)) {
- return getTypeOfSymbol(symbol);
+ function structuredTypeRelatedTo(source, target, reportErrors, intersectionState) {
+ ts.tracing.push("check" /* Check */, "structuredTypeRelatedTo", { sourceId: source.id, targetId: target.id });
+ var result = structuredTypeRelatedToWorker(source, target, reportErrors, intersectionState);
+ ts.tracing.pop();
+ return result;
}
- if (symbol.flags & (3 /* Variable */ | 4 /* Property */)) {
- var declaration = symbol.valueDeclaration;
- if (declaration) {
- if (isDeclarationWithExplicitTypeAnnotation(declaration)) {
- return getTypeOfSymbol(symbol);
+ function structuredTypeRelatedToWorker(source, target, reportErrors, intersectionState) {
+ if (intersectionState & 4 /* PropertyCheck */) {
+ return propertiesRelatedTo(source, target, reportErrors, /*excludedProperties*/ undefined, 0 /* None */);
+ }
+ var flags = source.flags & target.flags;
+ if (relation === identityRelation && !(flags & 524288 /* Object */)) {
+ if (flags & 4194304 /* Index */) {
+ return isRelatedTo(source.type, target.type, /*reportErrors*/ false);
+ }
+ var result_7 = 0 /* False */;
+ if (flags & 8388608 /* IndexedAccess */) {
+ if (result_7 = isRelatedTo(source.objectType, target.objectType, /*reportErrors*/ false)) {
+ if (result_7 &= isRelatedTo(source.indexType, target.indexType, /*reportErrors*/ false)) {
+ return result_7;
+ }
+ }
+ }
+ if (flags & 16777216 /* Conditional */) {
+ if (source.root.isDistributive === target.root.isDistributive) {
+ if (result_7 = isRelatedTo(source.checkType, target.checkType, /*reportErrors*/ false)) {
+ if (result_7 &= isRelatedTo(source.extendsType, target.extendsType, /*reportErrors*/ false)) {
+ if (result_7 &= isRelatedTo(getTrueTypeFromConditionalType(source), getTrueTypeFromConditionalType(target), /*reportErrors*/ false)) {
+ if (result_7 &= isRelatedTo(getFalseTypeFromConditionalType(source), getFalseTypeFromConditionalType(target), /*reportErrors*/ false)) {
+ return result_7;
+ }
+ }
+ }
+ }
+ }
+ }
+ if (flags & 33554432 /* Substitution */) {
+ return isRelatedTo(source.substitute, target.substitute, /*reportErrors*/ false);
+ }
+ return 0 /* False */;
+ }
+ var result;
+ var originalErrorInfo;
+ var varianceCheckFailed = false;
+ var saveErrorInfo = captureErrorCalculationState();
+ // We limit alias variance probing to only object and conditional types since their alias behavior
+ // is more predictable than other, interned types, which may or may not have an alias depending on
+ // the order in which things were checked.
+ if (source.flags & (524288 /* Object */ | 16777216 /* Conditional */) && source.aliasSymbol &&
+ source.aliasTypeArguments && source.aliasSymbol === target.aliasSymbol &&
+ !(source.aliasTypeArgumentsContainsMarker || target.aliasTypeArgumentsContainsMarker)) {
+ var variances = getAliasVariances(source.aliasSymbol);
+ if (variances === ts.emptyArray) {
+ return 1 /* Unknown */;
+ }
+ var varianceResult = relateVariances(source.aliasTypeArguments, target.aliasTypeArguments, variances, intersectionState);
+ if (varianceResult !== undefined) {
+ return varianceResult;
+ }
+ }
+ // For a generic type T and a type U that is assignable to T, [...U] is assignable to T, U is assignable to readonly [...T],
+ // and U is assignable to [...T] when U is constrained to a mutable array or tuple type.
+ if (isSingleElementGenericTupleType(source) && !source.target.readonly && (result = isRelatedTo(getTypeArguments(source)[0], target)) ||
+ isSingleElementGenericTupleType(target) && (target.target.readonly || isMutableArrayOrTuple(getBaseConstraintOfType(source) || source)) && (result = isRelatedTo(source, getTypeArguments(target)[0]))) {
+ return result;
+ }
+ if (target.flags & 262144 /* TypeParameter */) {
+ // A source type { [P in Q]: X } is related to a target type T if keyof T is related to Q and X is related to T[Q].
+ if (ts.getObjectFlags(source) & 32 /* Mapped */ && !source.declaration.nameType && isRelatedTo(getIndexType(target), getConstraintTypeFromMappedType(source))) {
+ if (!(getMappedTypeModifiers(source) & 4 /* IncludeOptional */)) {
+ var templateType = getTemplateTypeFromMappedType(source);
+ var indexedAccessType = getIndexedAccessType(target, getTypeParameterFromMappedType(source));
+ if (result = isRelatedTo(templateType, indexedAccessType, reportErrors)) {
+ return result;
+ }
+ }
+ }
+ }
+ else if (target.flags & 4194304 /* Index */) {
+ var targetType = target.type;
+ // A keyof S is related to a keyof T if T is related to S.
+ if (source.flags & 4194304 /* Index */) {
+ if (result = isRelatedTo(targetType, source.type, /*reportErrors*/ false)) {
+ return result;
+ }
+ }
+ if (isTupleType(targetType)) {
+ // An index type can have a tuple type target when the tuple type contains variadic elements.
+ // Check if the source is related to the known keys of the tuple type.
+ if (result = isRelatedTo(source, getKnownKeysOfTupleType(targetType), reportErrors)) {
+ return result;
+ }
}
- if (ts.isVariableDeclaration(declaration) && declaration.parent.parent.kind === 232 /* ForOfStatement */) {
- var statement = declaration.parent.parent;
- var expressionType = getTypeOfDottedName(statement.expression, /*diagnostic*/ undefined);
- if (expressionType) {
- var use = statement.awaitModifier ? 15 /* ForAwaitOf */ : 13 /* ForOf */;
- return checkIteratedTypeOrElementType(use, expressionType, undefinedType, /*errorNode*/ undefined);
+ else {
+ // A type S is assignable to keyof T if S is assignable to keyof C, where C is the
+ // simplified form of T or, if T doesn't simplify, the constraint of T.
+ var constraint = getSimplifiedTypeOrConstraint(targetType);
+ if (constraint) {
+ // We require Ternary.True here such that circular constraints don't cause
+ // false positives. For example, given 'T extends { [K in keyof T]: string }',
+ // 'keyof T' has itself as its constraint and produces a Ternary.Maybe when
+ // related to other types.
+ if (isRelatedTo(source, getIndexType(constraint, target.stringsOnly), reportErrors) === -1 /* True */) {
+ return -1 /* True */;
+ }
}
}
- if (diagnostic) {
- ts.addRelatedInfo(diagnostic, ts.createDiagnosticForNode(declaration, ts.Diagnostics._0_needs_an_explicit_type_annotation, symbolToString(symbol)));
+ }
+ else if (target.flags & 8388608 /* IndexedAccess */) {
+ // A type S is related to a type T[K] if S is related to C, where C is the base
+ // constraint of T[K] for writing.
+ if (relation === assignableRelation || relation === comparableRelation) {
+ var objectType = target.objectType;
+ var indexType = target.indexType;
+ var baseObjectType = getBaseConstraintOfType(objectType) || objectType;
+ var baseIndexType = getBaseConstraintOfType(indexType) || indexType;
+ if (!isGenericObjectType(baseObjectType) && !isGenericIndexType(baseIndexType)) {
+ var accessFlags = 2 /* Writing */ | (baseObjectType !== objectType ? 1 /* NoIndexSignatures */ : 0);
+ var constraint = getIndexedAccessTypeOrUndefined(baseObjectType, baseIndexType, target.noUncheckedIndexedAccessCandidate, /*accessNode*/ undefined, accessFlags);
+ if (constraint && (result = isRelatedTo(source, constraint, reportErrors))) {
+ return result;
+ }
+ }
}
}
- }
- }
- // We require the dotted function name in an assertion expression to be comprised of identifiers
- // that reference function, method, class or value module symbols; or variable, property or
- // parameter symbols with declarations that have explicit type annotations. Such references are
- // resolvable with no possibility of triggering circularities in control flow analysis.
- function getTypeOfDottedName(node, diagnostic) {
- if (!(node.flags & 16777216 /* InWithStatement */)) {
- switch (node.kind) {
- case 75 /* Identifier */:
- var symbol = getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(node));
- return getExplicitTypeOfSymbol(symbol.flags & 2097152 /* Alias */ ? resolveAlias(symbol) : symbol, diagnostic);
- case 104 /* ThisKeyword */:
- return getExplicitThisType(node);
- case 102 /* SuperKeyword */:
- return checkSuperExpression(node);
- case 194 /* PropertyAccessExpression */:
- var type = getTypeOfDottedName(node.expression, diagnostic);
- var prop = type && getPropertyOfType(type, node.name.escapedText);
- return prop && getExplicitTypeOfSymbol(prop, diagnostic);
- case 200 /* ParenthesizedExpression */:
- return getTypeOfDottedName(node.expression, diagnostic);
+ else if (isGenericMappedType(target) && !target.declaration.nameType) {
+ // A source type T is related to a target type { [P in X]: T[P] }
+ var template = getTemplateTypeFromMappedType(target);
+ var modifiers = getMappedTypeModifiers(target);
+ if (!(modifiers & 8 /* ExcludeOptional */)) {
+ if (template.flags & 8388608 /* IndexedAccess */ && template.objectType === source &&
+ template.indexType === getTypeParameterFromMappedType(target)) {
+ return -1 /* True */;
+ }
+ if (!isGenericMappedType(source)) {
+ var targetConstraint = getConstraintTypeFromMappedType(target);
+ var sourceKeys = getIndexType(source, /*stringsOnly*/ undefined, /*noIndexSignatures*/ true);
+ var includeOptional = modifiers & 4 /* IncludeOptional */;
+ var filteredByApplicability = includeOptional ? intersectTypes(targetConstraint, sourceKeys) : undefined;
+ // A source type T is related to a target type { [P in Q]: X } if Q is related to keyof T and T[Q] is related to X.
+ // A source type T is related to a target type { [P in Q]?: X } if some constituent Q' of Q is related to keyof T and T[Q'] is related to X.
+ if (includeOptional
+ ? !(filteredByApplicability.flags & 131072 /* Never */)
+ : isRelatedTo(targetConstraint, sourceKeys)) {
+ var templateType = getTemplateTypeFromMappedType(target);
+ var typeParameter = getTypeParameterFromMappedType(target);
+ // Fastpath: When the template has the form Obj[P] where P is the mapped type parameter, directly compare `source` with `Obj`
+ // to avoid creating the (potentially very large) number of new intermediate types made by manufacturing `source[P]`
+ var nonNullComponent = extractTypesOfKind(templateType, ~98304 /* Nullable */);
+ if (nonNullComponent.flags & 8388608 /* IndexedAccess */ && nonNullComponent.indexType === typeParameter) {
+ if (result = isRelatedTo(source, nonNullComponent.objectType, reportErrors)) {
+ return result;
+ }
+ }
+ else {
+ var indexingType = filteredByApplicability ? getIntersectionType([filteredByApplicability, typeParameter]) : typeParameter;
+ var indexedAccessType = getIndexedAccessType(source, indexingType);
+ if (result = isRelatedTo(indexedAccessType, templateType, reportErrors)) {
+ return result;
+ }
+ }
+ }
+ originalErrorInfo = errorInfo;
+ resetErrorInfo(saveErrorInfo);
+ }
+ }
}
- }
- }
- function getEffectsSignature(node) {
- var links = getNodeLinks(node);
- var signature = links.effectsSignature;
- if (signature === undefined) {
- // A call expression parented by an expression statement is a potential assertion. Other call
- // expressions are potential type predicate function calls. In order to avoid triggering
- // circularities in control flow analysis, we use getTypeOfDottedName when resolving the call
- // target expression of an assertion.
- var funcType = void 0;
- if (node.parent.kind === 226 /* ExpressionStatement */) {
- funcType = getTypeOfDottedName(node.expression, /*diagnostic*/ undefined);
+ else if (target.flags & 134217728 /* TemplateLiteral */ && source.flags & 128 /* StringLiteral */) {
+ if (isPatternLiteralType(target)) {
+ // match all non-`string` segments
+ var result_8 = inferLiteralsFromTemplateLiteralType(source, target);
+ if (result_8 && ts.every(result_8, function (r, i) { return isStringLiteralTypeValueParsableAsType(r, target.types[i]); })) {
+ return -1 /* True */;
+ }
+ }
}
- else if (node.expression.kind !== 102 /* SuperKeyword */) {
- if (ts.isOptionalChain(node)) {
- funcType = checkNonNullType(getOptionalExpressionType(checkExpression(node.expression), node.expression), node.expression);
+ if (source.flags & 8650752 /* TypeVariable */) {
+ if (source.flags & 8388608 /* IndexedAccess */ && target.flags & 8388608 /* IndexedAccess */) {
+ // A type S[K] is related to a type T[J] if S is related to T and K is related to J.
+ if (result = isRelatedTo(source.objectType, target.objectType, reportErrors)) {
+ result &= isRelatedTo(source.indexType, target.indexType, reportErrors);
+ }
+ if (result) {
+ resetErrorInfo(saveErrorInfo);
+ return result;
+ }
}
else {
- funcType = checkNonNullExpression(node.expression);
+ var constraint = getConstraintOfType(source);
+ if (!constraint || (source.flags & 262144 /* TypeParameter */ && constraint.flags & 1 /* Any */)) {
+ // A type variable with no constraint is not related to the non-primitive object type.
+ if (result = isRelatedTo(emptyObjectType, extractTypesOfKind(target, ~67108864 /* NonPrimitive */))) {
+ resetErrorInfo(saveErrorInfo);
+ return result;
+ }
+ }
+ // hi-speed no-this-instantiation check (less accurate, but avoids costly `this`-instantiation when the constraint will suffice), see #28231 for report on why this is needed
+ else if (result = isRelatedTo(constraint, target, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState)) {
+ resetErrorInfo(saveErrorInfo);
+ return result;
+ }
+ // slower, fuller, this-instantiated check (necessary when comparing raw `this` types from base classes), see `subclassWithPolymorphicThisIsAssignable.ts` test for example
+ else if (result = isRelatedTo(getTypeWithThisArgument(constraint, source), target, reportErrors, /*headMessage*/ undefined, intersectionState)) {
+ resetErrorInfo(saveErrorInfo);
+ return result;
+ }
}
}
- var signatures = getSignaturesOfType(funcType && getApparentType(funcType) || unknownType, 0 /* Call */);
- var candidate = signatures.length === 1 && !signatures[0].typeParameters ? signatures[0] :
- ts.some(signatures, hasTypePredicateOrNeverReturnType) ? getResolvedSignature(node) :
- undefined;
- signature = links.effectsSignature = candidate && hasTypePredicateOrNeverReturnType(candidate) ? candidate : unknownSignature;
- }
- return signature === unknownSignature ? undefined : signature;
- }
- function hasTypePredicateOrNeverReturnType(signature) {
- return !!(getTypePredicateOfSignature(signature) ||
- signature.declaration && (getReturnTypeFromAnnotation(signature.declaration) || unknownType).flags & 131072 /* Never */);
- }
- function getTypePredicateArgument(predicate, callExpression) {
- if (predicate.kind === 1 /* Identifier */ || predicate.kind === 3 /* AssertsIdentifier */) {
- return callExpression.arguments[predicate.parameterIndex];
- }
- var invokedExpression = ts.skipParentheses(callExpression.expression);
- return ts.isAccessExpression(invokedExpression) ? ts.skipParentheses(invokedExpression.expression) : undefined;
- }
- function reportFlowControlError(node) {
- var block = ts.findAncestor(node, ts.isFunctionOrModuleBlock);
- var sourceFile = ts.getSourceFileOfNode(node);
- var span = ts.getSpanOfTokenAtPosition(sourceFile, block.statements.pos);
- diagnostics.add(ts.createFileDiagnostic(sourceFile, span.start, span.length, ts.Diagnostics.The_containing_function_or_module_body_is_too_large_for_control_flow_analysis));
- }
- function isReachableFlowNode(flow) {
- var result = isReachableFlowNodeWorker(flow, /*noCacheCheck*/ false);
- lastFlowNode = flow;
- lastFlowNodeReachable = result;
- return result;
- }
- function isFalseExpression(expr) {
- var node = ts.skipParentheses(expr);
- return node.kind === 91 /* FalseKeyword */ || node.kind === 209 /* BinaryExpression */ && (node.operatorToken.kind === 55 /* AmpersandAmpersandToken */ && (isFalseExpression(node.left) || isFalseExpression(node.right)) ||
- node.operatorToken.kind === 56 /* BarBarToken */ && isFalseExpression(node.left) && isFalseExpression(node.right));
- }
- function isReachableFlowNodeWorker(flow, noCacheCheck) {
- while (true) {
- if (flow === lastFlowNode) {
- return lastFlowNodeReachable;
+ else if (source.flags & 4194304 /* Index */) {
+ if (result = isRelatedTo(keyofConstraintType, target, reportErrors)) {
+ resetErrorInfo(saveErrorInfo);
+ return result;
+ }
}
- var flags = flow.flags;
- if (flags & 4096 /* Shared */) {
- if (!noCacheCheck) {
- var id = getFlowNodeId(flow);
- var reachable = flowNodeReachable[id];
- return reachable !== undefined ? reachable : (flowNodeReachable[id] = isReachableFlowNodeWorker(flow, /*noCacheCheck*/ true));
+ else if (source.flags & 134217728 /* TemplateLiteral */) {
+ if (target.flags & 134217728 /* TemplateLiteral */ &&
+ source.texts.length === target.texts.length &&
+ source.types.length === target.types.length &&
+ ts.every(source.texts, function (t, i) { return t === target.texts[i]; }) &&
+ ts.every(instantiateType(source, makeFunctionTypeMapper(reportUnreliableMarkers)).types, function (t, i) { return !!(target.types[i].flags & (1 /* Any */ | 4 /* String */)) || !!isRelatedTo(t, target.types[i], /*reportErrors*/ false); })) {
+ return -1 /* True */;
+ }
+ var constraint = getBaseConstraintOfType(source);
+ if (constraint && constraint !== source && (result = isRelatedTo(constraint, target, reportErrors))) {
+ resetErrorInfo(saveErrorInfo);
+ return result;
}
- noCacheCheck = false;
}
- if (flags & (16 /* Assignment */ | 96 /* Condition */ | 256 /* ArrayMutation */)) {
- flow = flow.antecedent;
+ else if (source.flags & 268435456 /* StringMapping */) {
+ if (target.flags & 268435456 /* StringMapping */ && source.symbol === target.symbol) {
+ if (result = isRelatedTo(source.type, target.type, reportErrors)) {
+ resetErrorInfo(saveErrorInfo);
+ return result;
+ }
+ }
+ else {
+ var constraint = getBaseConstraintOfType(source);
+ if (constraint && (result = isRelatedTo(constraint, target, reportErrors))) {
+ resetErrorInfo(saveErrorInfo);
+ return result;
+ }
+ }
}
- else if (flags & 512 /* Call */) {
- var signature = getEffectsSignature(flow.node);
- if (signature) {
- var predicate = getTypePredicateOfSignature(signature);
- if (predicate && predicate.kind === 3 /* AssertsIdentifier */) {
- var predicateArgument = flow.node.arguments[predicate.parameterIndex];
- if (predicateArgument && isFalseExpression(predicateArgument)) {
- return false;
+ else if (source.flags & 16777216 /* Conditional */) {
+ if (target.flags & 16777216 /* Conditional */) {
+ // Two conditional types 'T1 extends U1 ? X1 : Y1' and 'T2 extends U2 ? X2 : Y2' are related if
+ // one of T1 and T2 is related to the other, U1 and U2 are identical types, X1 is related to X2,
+ // and Y1 is related to Y2.
+ var sourceParams = source.root.inferTypeParameters;
+ var sourceExtends = source.extendsType;
+ var mapper = void 0;
+ if (sourceParams) {
+ // If the source has infer type parameters, we instantiate them in the context of the target
+ var ctx = createInferenceContext(sourceParams, /*signature*/ undefined, 0 /* None */, isRelatedTo);
+ inferTypes(ctx.inferences, target.extendsType, sourceExtends, 256 /* NoConstraints */ | 512 /* AlwaysStrict */);
+ sourceExtends = instantiateType(sourceExtends, ctx.mapper);
+ mapper = ctx.mapper;
+ }
+ if (isTypeIdenticalTo(sourceExtends, target.extendsType) &&
+ (isRelatedTo(source.checkType, target.checkType) || isRelatedTo(target.checkType, source.checkType))) {
+ if (result = isRelatedTo(instantiateType(getTrueTypeFromConditionalType(source), mapper), getTrueTypeFromConditionalType(target), reportErrors)) {
+ result &= isRelatedTo(getFalseTypeFromConditionalType(source), getFalseTypeFromConditionalType(target), reportErrors);
+ }
+ if (result) {
+ resetErrorInfo(saveErrorInfo);
+ return result;
}
}
- if (getReturnTypeOfSignature(signature).flags & 131072 /* Never */) {
- return false;
+ }
+ else {
+ // conditionals aren't related to one another via distributive constraint as it is much too inaccurate and allows way
+ // more assignments than are desirable (since it maps the source check type to its constraint, it loses information)
+ var distributiveConstraint = getConstraintOfDistributiveConditionalType(source);
+ if (distributiveConstraint) {
+ if (result = isRelatedTo(distributiveConstraint, target, reportErrors)) {
+ resetErrorInfo(saveErrorInfo);
+ return result;
+ }
}
}
- flow = flow.antecedent;
- }
- else if (flags & 4 /* BranchLabel */) {
- // A branching point is reachable if any branch is reachable.
- return ts.some(flow.antecedents, function (f) { return isReachableFlowNodeWorker(f, /*noCacheCheck*/ false); });
- }
- else if (flags & 8 /* LoopLabel */) {
- // A loop is reachable if the control flow path that leads to the top is reachable.
- flow = flow.antecedents[0];
- }
- else if (flags & 128 /* SwitchClause */) {
- // The control flow path representing an unmatched value in a switch statement with
- // no default clause is unreachable if the switch statement is exhaustive.
- if (flow.clauseStart === flow.clauseEnd && isExhaustiveSwitchStatement(flow.switchStatement)) {
- return false;
+ // conditionals _can_ be related to one another via normal constraint, as, eg, `A extends B ? O : never` should be assignable to `O`
+ // when `O` is a conditional (`never` is trivially aissgnable to `O`, as is `O`!).
+ var defaultConstraint = getDefaultConstraintOfConditionalType(source);
+ if (defaultConstraint) {
+ if (result = isRelatedTo(defaultConstraint, target, reportErrors)) {
+ resetErrorInfo(saveErrorInfo);
+ return result;
+ }
}
- flow = flow.antecedent;
- }
- else if (flags & 1024 /* ReduceLabel */) {
- // Cache is unreliable once we start adjusting labels
- lastFlowNode = undefined;
- var target = flow.target;
- var saveAntecedents = target.antecedents;
- target.antecedents = flow.antecedents;
- var result = isReachableFlowNodeWorker(flow.antecedent, /*noCacheCheck*/ false);
- target.antecedents = saveAntecedents;
- return result;
}
else {
- return !(flags & 1 /* Unreachable */);
+ // An empty object type is related to any mapped type that includes a '?' modifier.
+ if (relation !== subtypeRelation && relation !== strictSubtypeRelation && isPartialMappedType(target) && isEmptyObjectType(source)) {
+ return -1 /* True */;
+ }
+ if (isGenericMappedType(target)) {
+ if (isGenericMappedType(source)) {
+ if (result = mappedTypeRelatedTo(source, target, reportErrors)) {
+ resetErrorInfo(saveErrorInfo);
+ return result;
+ }
+ }
+ return 0 /* False */;
+ }
+ var sourceIsPrimitive = !!(source.flags & 131068 /* Primitive */);
+ if (relation !== identityRelation) {
+ source = getApparentType(source);
+ }
+ else if (isGenericMappedType(source)) {
+ return 0 /* False */;
+ }
+ if (ts.getObjectFlags(source) & 4 /* Reference */ && ts.getObjectFlags(target) & 4 /* Reference */ && source.target === target.target &&
+ !(ts.getObjectFlags(source) & 8192 /* MarkerType */ || ts.getObjectFlags(target) & 8192 /* MarkerType */)) {
+ // We have type references to the same generic type, and the type references are not marker
+ // type references (which are intended by be compared structurally). Obtain the variance
+ // information for the type parameters and relate the type arguments accordingly.
+ var variances = getVariances(source.target);
+ // We return Ternary.Maybe for a recursive invocation of getVariances (signalled by emptyArray). This
+ // effectively means we measure variance only from type parameter occurrences that aren't nested in
+ // recursive instantiations of the generic type.
+ if (variances === ts.emptyArray) {
+ return 1 /* Unknown */;
+ }
+ var varianceResult = relateVariances(getTypeArguments(source), getTypeArguments(target), variances, intersectionState);
+ if (varianceResult !== undefined) {
+ return varianceResult;
+ }
+ }
+ else if (isReadonlyArrayType(target) ? isArrayType(source) || isTupleType(source) : isArrayType(target) && isTupleType(source) && !source.target.readonly) {
+ if (relation !== identityRelation) {
+ return isRelatedTo(getIndexTypeOfType(source, 1 /* Number */) || anyType, getIndexTypeOfType(target, 1 /* Number */) || anyType, reportErrors);
+ }
+ else {
+ // By flags alone, we know that the `target` is a readonly array while the source is a normal array or tuple
+ // or `target` is an array and source is a tuple - in both cases the types cannot be identical, by construction
+ return 0 /* False */;
+ }
+ }
+ // Consider a fresh empty object literal type "closed" under the subtype relationship - this way `{} <- {[idx: string]: any} <- fresh({})`
+ // and not `{} <- fresh({}) <- {[idx: string]: any}`
+ else if ((relation === subtypeRelation || relation === strictSubtypeRelation) && isEmptyObjectType(target) && ts.getObjectFlags(target) & 32768 /* FreshLiteral */ && !isEmptyObjectType(source)) {
+ return 0 /* False */;
+ }
+ // Even if relationship doesn't hold for unions, intersections, or generic type references,
+ // it may hold in a structural comparison.
+ // In a check of the form X = A & B, we will have previously checked if A relates to X or B relates
+ // to X. Failing both of those we want to check if the aggregation of A and B's members structurally
+ // relates to X. Thus, we include intersection types on the source side here.
+ if (source.flags & (524288 /* Object */ | 2097152 /* Intersection */) && target.flags & 524288 /* Object */) {
+ // Report structural errors only if we haven't reported any errors yet
+ var reportStructuralErrors = reportErrors && errorInfo === saveErrorInfo.errorInfo && !sourceIsPrimitive;
+ result = propertiesRelatedTo(source, target, reportStructuralErrors, /*excludedProperties*/ undefined, intersectionState);
+ if (result) {
+ result &= signaturesRelatedTo(source, target, 0 /* Call */, reportStructuralErrors);
+ if (result) {
+ result &= signaturesRelatedTo(source, target, 1 /* Construct */, reportStructuralErrors);
+ if (result) {
+ result &= indexTypesRelatedTo(source, target, 0 /* String */, sourceIsPrimitive, reportStructuralErrors, intersectionState);
+ if (result) {
+ result &= indexTypesRelatedTo(source, target, 1 /* Number */, sourceIsPrimitive, reportStructuralErrors, intersectionState);
+ }
+ }
+ }
+ }
+ if (varianceCheckFailed && result) {
+ errorInfo = originalErrorInfo || errorInfo || saveErrorInfo.errorInfo; // Use variance error (there is no structural one) and return false
+ }
+ else if (result) {
+ return result;
+ }
+ }
+ // If S is an object type and T is a discriminated union, S may be related to T if
+ // there exists a constituent of T for every combination of the discriminants of S
+ // with respect to T. We do not report errors here, as we will use the existing
+ // error result from checking each constituent of the union.
+ if (source.flags & (524288 /* Object */ | 2097152 /* Intersection */) && target.flags & 1048576 /* Union */) {
+ var objectOnlyTarget = extractTypesOfKind(target, 524288 /* Object */ | 2097152 /* Intersection */ | 33554432 /* Substitution */);
+ if (objectOnlyTarget.flags & 1048576 /* Union */) {
+ var result_9 = typeRelatedToDiscriminatedType(source, objectOnlyTarget);
+ if (result_9) {
+ return result_9;
+ }
+ }
+ }
+ }
+ return 0 /* False */;
+ function relateVariances(sourceTypeArguments, targetTypeArguments, variances, intersectionState) {
+ if (result = typeArgumentsRelatedTo(sourceTypeArguments, targetTypeArguments, variances, reportErrors, intersectionState)) {
+ return result;
+ }
+ if (ts.some(variances, function (v) { return !!(v & 24 /* AllowsStructuralFallback */); })) {
+ // If some type parameter was `Unmeasurable` or `Unreliable`, and we couldn't pass by assuming it was identical, then we
+ // have to allow a structural fallback check
+ // We elide the variance-based error elaborations, since those might not be too helpful, since we'll potentially
+ // be assuming identity of the type parameter.
+ originalErrorInfo = undefined;
+ resetErrorInfo(saveErrorInfo);
+ return undefined;
+ }
+ var allowStructuralFallback = targetTypeArguments && hasCovariantVoidArgument(targetTypeArguments, variances);
+ varianceCheckFailed = !allowStructuralFallback;
+ // The type arguments did not relate appropriately, but it may be because we have no variance
+ // information (in which case typeArgumentsRelatedTo defaulted to covariance for all type
+ // arguments). It might also be the case that the target type has a 'void' type argument for
+ // a covariant type parameter that is only used in return positions within the generic type
+ // (in which case any type argument is permitted on the source side). In those cases we proceed
+ // with a structural comparison. Otherwise, we know for certain the instantiations aren't
+ // related and we can return here.
+ if (variances !== ts.emptyArray && !allowStructuralFallback) {
+ // In some cases generic types that are covariant in regular type checking mode become
+ // invariant in --strictFunctionTypes mode because one or more type parameters are used in
+ // both co- and contravariant positions. In order to make it easier to diagnose *why* such
+ // types are invariant, if any of the type parameters are invariant we reset the reported
+ // errors and instead force a structural comparison (which will include elaborations that
+ // reveal the reason).
+ // We can switch on `reportErrors` here, since varianceCheckFailed guarantees we return `False`,
+ // we can return `False` early here to skip calculating the structural error message we don't need.
+ if (varianceCheckFailed && !(reportErrors && ts.some(variances, function (v) { return (v & 7 /* VarianceMask */) === 0 /* Invariant */; }))) {
+ return 0 /* False */;
+ }
+ // We remember the original error information so we can restore it in case the structural
+ // comparison unexpectedly succeeds. This can happen when the structural comparison result
+ // is a Ternary.Maybe for example caused by the recursion depth limiter.
+ originalErrorInfo = errorInfo;
+ resetErrorInfo(saveErrorInfo);
+ }
}
}
- }
- function getFlowTypeOfReference(reference, declaredType, initialType, flowContainer, couldBeUninitialized) {
- if (initialType === void 0) { initialType = declaredType; }
- var key;
- var keySet = false;
- var flowDepth = 0;
- if (flowAnalysisDisabled) {
- return errorType;
- }
- if (!reference.flowNode || !couldBeUninitialized && !(declaredType.flags & 133970943 /* Narrowable */)) {
- return declaredType;
+ function reportUnmeasurableMarkers(p) {
+ if (outofbandVarianceMarkerHandler && (p === markerSuperType || p === markerSubType || p === markerOtherType)) {
+ outofbandVarianceMarkerHandler(/*onlyUnreliable*/ false);
+ }
+ return p;
}
- flowInvocationCount++;
- var sharedFlowStart = sharedFlowCount;
- var evolvedType = getTypeFromFlowType(getTypeAtFlowNode(reference.flowNode));
- sharedFlowCount = sharedFlowStart;
- // When the reference is 'x' in an 'x.length', 'x.push(value)', 'x.unshift(value)' or x[n] = value' operation,
- // we give type 'any[]' to 'x' instead of using the type determined by control flow analysis such that operations
- // on empty arrays are possible without implicit any errors and new element types can be inferred without
- // type mismatch errors.
- var resultType = ts.getObjectFlags(evolvedType) & 256 /* EvolvingArray */ && isEvolvingArrayOperationTarget(reference) ? autoArrayType : finalizeEvolvingArrayType(evolvedType);
- if (resultType === unreachableNeverType || reference.parent && reference.parent.kind === 218 /* NonNullExpression */ && getTypeWithFacts(resultType, 2097152 /* NEUndefinedOrNull */).flags & 131072 /* Never */) {
- return declaredType;
+ function reportUnreliableMarkers(p) {
+ if (outofbandVarianceMarkerHandler && (p === markerSuperType || p === markerSubType || p === markerOtherType)) {
+ outofbandVarianceMarkerHandler(/*onlyUnreliable*/ true);
+ }
+ return p;
}
- return resultType;
- function getOrSetCacheKey() {
- if (keySet) {
- return key;
+ // A type [P in S]: X is related to a type [Q in T]: Y if T is related to S and X' is
+ // related to Y, where X' is an instantiation of X in which P is replaced with Q. Notice
+ // that S and T are contra-variant whereas X and Y are co-variant.
+ function mappedTypeRelatedTo(source, target, reportErrors) {
+ var modifiersRelated = relation === comparableRelation || (relation === identityRelation ? getMappedTypeModifiers(source) === getMappedTypeModifiers(target) :
+ getCombinedMappedTypeOptionality(source) <= getCombinedMappedTypeOptionality(target));
+ if (modifiersRelated) {
+ var result_10;
+ var targetConstraint = getConstraintTypeFromMappedType(target);
+ var sourceConstraint = instantiateType(getConstraintTypeFromMappedType(source), makeFunctionTypeMapper(getCombinedMappedTypeOptionality(source) < 0 ? reportUnmeasurableMarkers : reportUnreliableMarkers));
+ if (result_10 = isRelatedTo(targetConstraint, sourceConstraint, reportErrors)) {
+ var mapper = createTypeMapper([getTypeParameterFromMappedType(source)], [getTypeParameterFromMappedType(target)]);
+ if (instantiateType(getNameTypeFromMappedType(source), mapper) === instantiateType(getNameTypeFromMappedType(target), mapper)) {
+ return result_10 & isRelatedTo(instantiateType(getTemplateTypeFromMappedType(source), mapper), getTemplateTypeFromMappedType(target), reportErrors);
+ }
+ }
}
- keySet = true;
- return key = getFlowCacheKey(reference, declaredType, initialType, flowContainer);
+ return 0 /* False */;
}
- function getTypeAtFlowNode(flow) {
- if (flowDepth === 2000) {
- // We have made 2000 recursive invocations. To avoid overflowing the call stack we report an error
- // and disable further control flow analysis in the containing function or module body.
- flowAnalysisDisabled = true;
- reportFlowControlError(reference);
- return errorType;
+ function typeRelatedToDiscriminatedType(source, target) {
+ // 1. Generate the combinations of discriminant properties & types 'source' can satisfy.
+ // a. If the number of combinations is above a set limit, the comparison is too complex.
+ // 2. Filter 'target' to the subset of types whose discriminants exist in the matrix.
+ // a. If 'target' does not satisfy all discriminants in the matrix, 'source' is not related.
+ // 3. For each type in the filtered 'target', determine if all non-discriminant properties of
+ // 'target' are related to a property in 'source'.
+ //
+ // NOTE: See ~/tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithDiscriminatedUnion.ts
+ // for examples.
+ var sourceProperties = getPropertiesOfType(source);
+ var sourcePropertiesFiltered = findDiscriminantProperties(sourceProperties, target);
+ if (!sourcePropertiesFiltered)
+ return 0 /* False */;
+ // Though we could compute the number of combinations as we generate
+ // the matrix, this would incur additional memory overhead due to
+ // array allocations. To reduce this overhead, we first compute
+ // the number of combinations to ensure we will not surpass our
+ // fixed limit before incurring the cost of any allocations:
+ var numCombinations = 1;
+ for (var _i = 0, sourcePropertiesFiltered_1 = sourcePropertiesFiltered; _i < sourcePropertiesFiltered_1.length; _i++) {
+ var sourceProperty = sourcePropertiesFiltered_1[_i];
+ numCombinations *= countTypes(getTypeOfSymbol(sourceProperty));
+ if (numCombinations > 25) {
+ // We've reached the complexity limit.
+ ts.tracing.instant("check" /* Check */, "typeRelatedToDiscriminatedType_DepthLimit", { sourceId: source.id, targetId: target.id, numCombinations: numCombinations });
+ return 0 /* False */;
+ }
}
- flowDepth++;
- while (true) {
- var flags = flow.flags;
- if (flags & 4096 /* Shared */) {
- // We cache results of flow type resolution for shared nodes that were previously visited in
- // the same getFlowTypeOfReference invocation. A node is considered shared when it is the
- // antecedent of more than one node.
- for (var i = sharedFlowStart; i < sharedFlowCount; i++) {
- if (sharedFlowNodes[i] === flow) {
- flowDepth--;
- return sharedFlowTypes[i];
+ // Compute the set of types for each discriminant property.
+ var sourceDiscriminantTypes = new Array(sourcePropertiesFiltered.length);
+ var excludedProperties = new ts.Set();
+ for (var i = 0; i < sourcePropertiesFiltered.length; i++) {
+ var sourceProperty = sourcePropertiesFiltered[i];
+ var sourcePropertyType = getTypeOfSymbol(sourceProperty);
+ sourceDiscriminantTypes[i] = sourcePropertyType.flags & 1048576 /* Union */
+ ? sourcePropertyType.types
+ : [sourcePropertyType];
+ excludedProperties.add(sourceProperty.escapedName);
+ }
+ // Match each combination of the cartesian product of discriminant properties to one or more
+ // constituents of 'target'. If any combination does not have a match then 'source' is not relatable.
+ var discriminantCombinations = ts.cartesianProduct(sourceDiscriminantTypes);
+ var matchingTypes = [];
+ var _loop_17 = function (combination) {
+ var hasMatch = false;
+ outer: for (var _i = 0, _a = target.types; _i < _a.length; _i++) {
+ var type = _a[_i];
+ var _loop_18 = function (i) {
+ var sourceProperty = sourcePropertiesFiltered[i];
+ var targetProperty = getPropertyOfType(type, sourceProperty.escapedName);
+ if (!targetProperty)
+ return "continue-outer";
+ if (sourceProperty === targetProperty)
+ return "continue";
+ // We compare the source property to the target in the context of a single discriminant type.
+ var related = propertyRelatedTo(source, target, sourceProperty, targetProperty, function (_) { return combination[i]; }, /*reportErrors*/ false, 0 /* None */, /*skipOptional*/ strictNullChecks || relation === comparableRelation);
+ // If the target property could not be found, or if the properties were not related,
+ // then this constituent is not a match.
+ if (!related) {
+ return "continue-outer";
+ }
+ };
+ for (var i = 0; i < sourcePropertiesFiltered.length; i++) {
+ var state_7 = _loop_18(i);
+ switch (state_7) {
+ case "continue-outer": continue outer;
}
}
+ ts.pushIfUnique(matchingTypes, type, ts.equateValues);
+ hasMatch = true;
}
- var type = void 0;
- if (flags & 16 /* Assignment */) {
- type = getTypeAtFlowAssignment(flow);
- if (!type) {
- flow = flow.antecedent;
- continue;
- }
+ if (!hasMatch) {
+ return { value: 0 /* False */ };
}
- else if (flags & 512 /* Call */) {
- type = getTypeAtFlowCall(flow);
- if (!type) {
- flow = flow.antecedent;
- continue;
+ };
+ for (var _a = 0, discriminantCombinations_1 = discriminantCombinations; _a < discriminantCombinations_1.length; _a++) {
+ var combination = discriminantCombinations_1[_a];
+ var state_6 = _loop_17(combination);
+ if (typeof state_6 === "object")
+ return state_6.value;
+ }
+ // Compare the remaining non-discriminant properties of each match.
+ var result = -1 /* True */;
+ for (var _b = 0, matchingTypes_1 = matchingTypes; _b < matchingTypes_1.length; _b++) {
+ var type = matchingTypes_1[_b];
+ result &= propertiesRelatedTo(source, type, /*reportErrors*/ false, excludedProperties, 0 /* None */);
+ if (result) {
+ result &= signaturesRelatedTo(source, type, 0 /* Call */, /*reportStructuralErrors*/ false);
+ if (result) {
+ result &= signaturesRelatedTo(source, type, 1 /* Construct */, /*reportStructuralErrors*/ false);
+ if (result) {
+ result &= indexTypesRelatedTo(source, type, 0 /* String */, /*sourceIsPrimitive*/ false, /*reportStructuralErrors*/ false, 0 /* None */);
+ // Comparing numeric index types when both `source` and `type` are tuples is unnecessary as the
+ // element types should be sufficiently covered by `propertiesRelatedTo`. It also causes problems
+ // with index type assignability as the types for the excluded discriminants are still included
+ // in the index type.
+ if (result && !(isTupleType(source) && isTupleType(type))) {
+ result &= indexTypesRelatedTo(source, type, 1 /* Number */, /*sourceIsPrimitive*/ false, /*reportStructuralErrors*/ false, 0 /* None */);
+ }
+ }
}
}
- else if (flags & 96 /* Condition */) {
- type = getTypeAtFlowCondition(flow);
- }
- else if (flags & 128 /* SwitchClause */) {
- type = getTypeAtSwitchClause(flow);
- }
- else if (flags & 12 /* Label */) {
- if (flow.antecedents.length === 1) {
- flow = flow.antecedents[0];
- continue;
- }
- type = flags & 4 /* BranchLabel */ ?
- getTypeAtFlowBranchLabel(flow) :
- getTypeAtFlowLoopLabel(flow);
+ if (!result) {
+ return result;
}
- else if (flags & 256 /* ArrayMutation */) {
- type = getTypeAtFlowArrayMutation(flow);
- if (!type) {
- flow = flow.antecedent;
- continue;
+ }
+ return result;
+ }
+ function excludeProperties(properties, excludedProperties) {
+ if (!excludedProperties || properties.length === 0)
+ return properties;
+ var result;
+ for (var i = 0; i < properties.length; i++) {
+ if (!excludedProperties.has(properties[i].escapedName)) {
+ if (result) {
+ result.push(properties[i]);
}
}
- else if (flags & 1024 /* ReduceLabel */) {
- var target = flow.target;
- var saveAntecedents = target.antecedents;
- target.antecedents = flow.antecedents;
- type = getTypeAtFlowNode(flow.antecedent);
- target.antecedents = saveAntecedents;
+ else if (!result) {
+ result = properties.slice(0, i);
}
- else if (flags & 2 /* Start */) {
- // Check if we should continue with the control flow of the containing function.
- var container = flow.node;
- if (container && container !== flowContainer &&
- reference.kind !== 194 /* PropertyAccessExpression */ &&
- reference.kind !== 195 /* ElementAccessExpression */ &&
- reference.kind !== 104 /* ThisKeyword */) {
- flow = container.flowNode;
- continue;
+ }
+ return result || properties;
+ }
+ function isPropertySymbolTypeRelated(sourceProp, targetProp, getTypeOfSourceProperty, reportErrors, intersectionState) {
+ var targetIsOptional = strictNullChecks && !!(ts.getCheckFlags(targetProp) & 48 /* Partial */);
+ var source = getTypeOfSourceProperty(sourceProp);
+ if (ts.getCheckFlags(targetProp) & 65536 /* DeferredType */ && !getSymbolLinks(targetProp).type) {
+ // Rather than resolving (and normalizing) the type, relate constituent-by-constituent without performing normalization or seconadary passes
+ var links = getSymbolLinks(targetProp);
+ ts.Debug.assertIsDefined(links.deferralParent);
+ ts.Debug.assertIsDefined(links.deferralConstituents);
+ var unionParent = !!(links.deferralParent.flags & 1048576 /* Union */);
+ var result_11 = unionParent ? 0 /* False */ : -1 /* True */;
+ var targetTypes = links.deferralConstituents;
+ for (var _i = 0, targetTypes_3 = targetTypes; _i < targetTypes_3.length; _i++) {
+ var targetType = targetTypes_3[_i];
+ var related = isRelatedTo(source, targetType, /*reportErrors*/ false, /*headMessage*/ undefined, unionParent ? 0 : 2 /* Target */);
+ if (!unionParent) {
+ if (!related) {
+ // Can't assign to a target individually - have to fallback to assigning to the _whole_ intersection (which forces normalization)
+ return isRelatedTo(source, addOptionality(getTypeOfSymbol(targetProp), targetIsOptional), reportErrors);
+ }
+ result_11 &= related;
+ }
+ else {
+ if (related) {
+ return related;
+ }
}
- // At the top of the flow we have the initial type.
- type = initialType;
}
- else {
- // Unreachable code errors are reported in the binding phase. Here we
- // simply return the non-auto declared type to reduce follow-on errors.
- type = convertAutoToAny(declaredType);
+ if (unionParent && !result_11 && targetIsOptional) {
+ result_11 = isRelatedTo(source, undefinedType);
}
- if (flags & 4096 /* Shared */) {
- // Record visited node and the associated type in the cache.
- sharedFlowNodes[sharedFlowCount] = flow;
- sharedFlowTypes[sharedFlowCount] = type;
- sharedFlowCount++;
+ if (unionParent && !result_11 && reportErrors) {
+ // The easiest way to get the right errors here is to un-defer (which may be costly)
+ // If it turns out this is too costly too often, we can replicate the error handling logic within
+ // typeRelatedToSomeType without the discriminatable type branch (as that requires a manifest union
+ // type on which to hand discriminable properties, which we are expressly trying to avoid here)
+ return isRelatedTo(source, addOptionality(getTypeOfSymbol(targetProp), targetIsOptional), reportErrors);
}
- flowDepth--;
- return type;
+ return result_11;
+ }
+ else {
+ return isRelatedTo(source, addOptionality(getTypeOfSymbol(targetProp), targetIsOptional), reportErrors, /*headMessage*/ undefined, intersectionState);
}
}
- function getInitialOrAssignedType(flow) {
- var node = flow.node;
- return getConstraintForLocation(node.kind === 242 /* VariableDeclaration */ || node.kind === 191 /* BindingElement */ ?
- getInitialType(node) :
- getAssignedType(node), reference);
- }
- function getTypeAtFlowAssignment(flow) {
- var node = flow.node;
- // Assignments only narrow the computed type if the declared type is a union type. Thus, we
- // only need to evaluate the assigned type if the declared type is a union type.
- if (isMatchingReference(reference, node)) {
- if (!isReachableFlowNode(flow)) {
- return unreachableNeverType;
- }
- if (ts.getAssignmentTargetKind(node) === 2 /* Compound */) {
- var flowType = getTypeAtFlowNode(flow.antecedent);
- return createFlowType(getBaseTypeOfLiteralType(getTypeFromFlowType(flowType)), isIncomplete(flowType));
- }
- if (declaredType === autoType || declaredType === autoArrayType) {
- if (isEmptyArrayAssignment(node)) {
- return getEvolvingArrayType(neverType);
+ function propertyRelatedTo(source, target, sourceProp, targetProp, getTypeOfSourceProperty, reportErrors, intersectionState, skipOptional) {
+ var sourcePropFlags = ts.getDeclarationModifierFlagsFromSymbol(sourceProp);
+ var targetPropFlags = ts.getDeclarationModifierFlagsFromSymbol(targetProp);
+ if (sourcePropFlags & 8 /* Private */ || targetPropFlags & 8 /* Private */) {
+ if (sourceProp.valueDeclaration !== targetProp.valueDeclaration) {
+ if (reportErrors) {
+ if (sourcePropFlags & 8 /* Private */ && targetPropFlags & 8 /* Private */) {
+ reportError(ts.Diagnostics.Types_have_separate_declarations_of_a_private_property_0, symbolToString(targetProp));
+ }
+ else {
+ reportError(ts.Diagnostics.Property_0_is_private_in_type_1_but_not_in_type_2, symbolToString(targetProp), typeToString(sourcePropFlags & 8 /* Private */ ? source : target), typeToString(sourcePropFlags & 8 /* Private */ ? target : source));
+ }
}
- var assignedType = getBaseTypeOfLiteralType(getInitialOrAssignedType(flow));
- return isTypeAssignableTo(assignedType, declaredType) ? assignedType : anyArrayType;
+ return 0 /* False */;
}
- if (declaredType.flags & 1048576 /* Union */) {
- return getAssignmentReducedType(declaredType, getInitialOrAssignedType(flow));
+ }
+ else if (targetPropFlags & 16 /* Protected */) {
+ if (!isValidOverrideOf(sourceProp, targetProp)) {
+ if (reportErrors) {
+ reportError(ts.Diagnostics.Property_0_is_protected_but_type_1_is_not_a_class_derived_from_2, symbolToString(targetProp), typeToString(getDeclaringClass(sourceProp) || source), typeToString(getDeclaringClass(targetProp) || target));
+ }
+ return 0 /* False */;
}
- return declaredType;
}
- // We didn't have a direct match. However, if the reference is a dotted name, this
- // may be an assignment to a left hand part of the reference. For example, for a
- // reference 'x.y.z', we may be at an assignment to 'x.y' or 'x'. In that case,
- // return the declared type.
- if (containsMatchingReference(reference, node)) {
- if (!isReachableFlowNode(flow)) {
- return unreachableNeverType;
+ else if (sourcePropFlags & 16 /* Protected */) {
+ if (reportErrors) {
+ reportError(ts.Diagnostics.Property_0_is_protected_in_type_1_but_public_in_type_2, symbolToString(targetProp), typeToString(source), typeToString(target));
}
- // A matching dotted name might also be an expando property on a function *expression*,
- // in which case we continue control flow analysis back to the function's declaration
- if (ts.isVariableDeclaration(node) && (ts.isInJSFile(node) || ts.isVarConst(node))) {
- var init = ts.getDeclaredExpandoInitializer(node);
- if (init && (init.kind === 201 /* FunctionExpression */ || init.kind === 202 /* ArrowFunction */)) {
- return getTypeAtFlowNode(flow.antecedent);
- }
+ return 0 /* False */;
+ }
+ // If the target comes from a partial union prop, allow `undefined` in the target type
+ var related = isPropertySymbolTypeRelated(sourceProp, targetProp, getTypeOfSourceProperty, reportErrors, intersectionState);
+ if (!related) {
+ if (reportErrors) {
+ reportIncompatibleError(ts.Diagnostics.Types_of_property_0_are_incompatible, symbolToString(targetProp));
}
- return declaredType;
+ return 0 /* False */;
}
- // for (const _ in ref) acts as a nonnull on ref
- if (ts.isVariableDeclaration(node) && node.parent.parent.kind === 231 /* ForInStatement */ && isMatchingReference(reference, node.parent.parent.expression)) {
- return getNonNullableTypeIfNeeded(getTypeFromFlowType(getTypeAtFlowNode(flow.antecedent)));
+ // When checking for comparability, be more lenient with optional properties.
+ if (!skipOptional && sourceProp.flags & 16777216 /* Optional */ && !(targetProp.flags & 16777216 /* Optional */)) {
+ // TypeScript 1.0 spec (April 2014): 3.8.3
+ // S is a subtype of a type T, and T is a supertype of S if ...
+ // S' and T are object types and, for each member M in T..
+ // M is a property and S' contains a property N where
+ // if M is a required property, N is also a required property
+ // (M - property in T)
+ // (N - property in S)
+ if (reportErrors) {
+ reportError(ts.Diagnostics.Property_0_is_optional_in_type_1_but_required_in_type_2, symbolToString(targetProp), typeToString(source), typeToString(target));
+ }
+ return 0 /* False */;
}
- // Assignment doesn't affect reference
- return undefined;
+ return related;
}
- function narrowTypeByAssertion(type, expr) {
- var node = ts.skipParentheses(expr);
- if (node.kind === 91 /* FalseKeyword */) {
- return unreachableNeverType;
+ function reportUnmatchedProperty(source, target, unmatchedProperty, requireOptionalProperties) {
+ var shouldSkipElaboration = false;
+ // give specific error in case where private names have the same description
+ if (unmatchedProperty.valueDeclaration
+ && ts.isNamedDeclaration(unmatchedProperty.valueDeclaration)
+ && ts.isPrivateIdentifier(unmatchedProperty.valueDeclaration.name)
+ && source.symbol
+ && source.symbol.flags & 32 /* Class */) {
+ var privateIdentifierDescription = unmatchedProperty.valueDeclaration.name.escapedText;
+ var symbolTableKey = ts.getSymbolNameForPrivateIdentifier(source.symbol, privateIdentifierDescription);
+ if (symbolTableKey && getPropertyOfType(source, symbolTableKey)) {
+ var sourceName = ts.factory.getDeclarationName(source.symbol.valueDeclaration);
+ var targetName = ts.factory.getDeclarationName(target.symbol.valueDeclaration);
+ reportError(ts.Diagnostics.Property_0_in_type_1_refers_to_a_different_member_that_cannot_be_accessed_from_within_type_2, diagnosticName(privateIdentifierDescription), diagnosticName(sourceName.escapedText === "" ? anon : sourceName), diagnosticName(targetName.escapedText === "" ? anon : targetName));
+ return;
+ }
}
- if (node.kind === 209 /* BinaryExpression */) {
- if (node.operatorToken.kind === 55 /* AmpersandAmpersandToken */) {
- return narrowTypeByAssertion(narrowTypeByAssertion(type, node.left), node.right);
+ var props = ts.arrayFrom(getUnmatchedProperties(source, target, requireOptionalProperties, /*matchDiscriminantProperties*/ false));
+ if (!headMessage || (headMessage.code !== ts.Diagnostics.Class_0_incorrectly_implements_interface_1.code &&
+ headMessage.code !== ts.Diagnostics.Class_0_incorrectly_implements_class_1_Did_you_mean_to_extend_1_and_inherit_its_members_as_a_subclass.code)) {
+ shouldSkipElaboration = true; // Retain top-level error for interface implementing issues, otherwise omit it
+ }
+ if (props.length === 1) {
+ var propName = symbolToString(unmatchedProperty);
+ reportError.apply(void 0, __spreadArrays([ts.Diagnostics.Property_0_is_missing_in_type_1_but_required_in_type_2, propName], getTypeNamesForErrorDisplay(source, target)));
+ if (ts.length(unmatchedProperty.declarations)) {
+ associateRelatedInfo(ts.createDiagnosticForNode(unmatchedProperty.declarations[0], ts.Diagnostics._0_is_declared_here, propName));
}
- if (node.operatorToken.kind === 56 /* BarBarToken */) {
- return getUnionType([narrowTypeByAssertion(type, node.left), narrowTypeByAssertion(type, node.right)]);
+ if (shouldSkipElaboration && errorInfo) {
+ overrideNextErrorInfo++;
}
}
- return narrowType(type, node, /*assumeTrue*/ true);
- }
- function getTypeAtFlowCall(flow) {
- var signature = getEffectsSignature(flow.node);
- if (signature) {
- var predicate = getTypePredicateOfSignature(signature);
- if (predicate && (predicate.kind === 2 /* AssertsThis */ || predicate.kind === 3 /* AssertsIdentifier */)) {
- var flowType = getTypeAtFlowNode(flow.antecedent);
- var type = finalizeEvolvingArrayType(getTypeFromFlowType(flowType));
- var narrowedType = predicate.type ? narrowTypeByTypePredicate(type, predicate, flow.node, /*assumeTrue*/ true) :
- predicate.kind === 3 /* AssertsIdentifier */ && predicate.parameterIndex >= 0 && predicate.parameterIndex < flow.node.arguments.length ? narrowTypeByAssertion(type, flow.node.arguments[predicate.parameterIndex]) :
- type;
- return narrowedType === type ? flowType : createFlowType(narrowedType, isIncomplete(flowType));
+ else if (tryElaborateArrayLikeErrors(source, target, /*reportErrors*/ false)) {
+ if (props.length > 5) { // arbitrary cutoff for too-long list form
+ reportError(ts.Diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2_and_3_more, typeToString(source), typeToString(target), ts.map(props.slice(0, 4), function (p) { return symbolToString(p); }).join(", "), props.length - 4);
}
- if (getReturnTypeOfSignature(signature).flags & 131072 /* Never */) {
- return unreachableNeverType;
+ else {
+ reportError(ts.Diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2, typeToString(source), typeToString(target), ts.map(props, function (p) { return symbolToString(p); }).join(", "));
+ }
+ if (shouldSkipElaboration && errorInfo) {
+ overrideNextErrorInfo++;
}
}
- return undefined;
+ // No array like or unmatched property error - just issue top level error (errorInfo = undefined)
}
- function getTypeAtFlowArrayMutation(flow) {
- if (declaredType === autoType || declaredType === autoArrayType) {
- var node = flow.node;
- var expr = node.kind === 196 /* CallExpression */ ?
- node.expression.expression :
- node.left.expression;
- if (isMatchingReference(reference, getReferenceCandidate(expr))) {
- var flowType = getTypeAtFlowNode(flow.antecedent);
- var type = getTypeFromFlowType(flowType);
- if (ts.getObjectFlags(type) & 256 /* EvolvingArray */) {
- var evolvedType_1 = type;
- if (node.kind === 196 /* CallExpression */) {
- for (var _i = 0, _a = node.arguments; _i < _a.length; _i++) {
- var arg = _a[_i];
- evolvedType_1 = addEvolvingArrayElementType(evolvedType_1, arg);
+ function propertiesRelatedTo(source, target, reportErrors, excludedProperties, intersectionState) {
+ if (relation === identityRelation) {
+ return propertiesIdenticalTo(source, target, excludedProperties);
+ }
+ var result = -1 /* True */;
+ if (isTupleType(target)) {
+ if (isArrayType(source) || isTupleType(source)) {
+ if (!target.target.readonly && (isReadonlyArrayType(source) || isTupleType(source) && source.target.readonly)) {
+ return 0 /* False */;
+ }
+ var sourceArity = getTypeReferenceArity(source);
+ var targetArity = getTypeReferenceArity(target);
+ var sourceRestFlag = isTupleType(source) ? source.target.combinedFlags & 4 /* Rest */ : 4 /* Rest */;
+ var targetRestFlag = target.target.combinedFlags & 4 /* Rest */;
+ var sourceMinLength = isTupleType(source) ? source.target.minLength : 0;
+ var targetMinLength = target.target.minLength;
+ if (!sourceRestFlag && sourceArity < targetMinLength) {
+ if (reportErrors) {
+ reportError(ts.Diagnostics.Source_has_0_element_s_but_target_requires_1, sourceArity, targetMinLength);
+ }
+ return 0 /* False */;
+ }
+ if (!targetRestFlag && targetArity < sourceMinLength) {
+ if (reportErrors) {
+ reportError(ts.Diagnostics.Source_has_0_element_s_but_target_allows_only_1, sourceMinLength, targetArity);
+ }
+ return 0 /* False */;
+ }
+ if (!targetRestFlag && sourceRestFlag) {
+ if (reportErrors) {
+ if (sourceMinLength < targetMinLength) {
+ reportError(ts.Diagnostics.Target_requires_0_element_s_but_source_may_have_fewer, targetMinLength);
+ }
+ else {
+ reportError(ts.Diagnostics.Target_allows_only_0_element_s_but_source_may_have_more, targetArity);
}
}
- else {
- // We must get the context free expression type so as to not recur in an uncached fashion on the LHS (which causes exponential blowup in compile time)
- var indexType = getContextFreeTypeOfExpression(node.left.argumentExpression);
- if (isTypeAssignableToKind(indexType, 296 /* NumberLike */)) {
- evolvedType_1 = addEvolvingArrayElementType(evolvedType_1, node.right);
+ return 0 /* False */;
+ }
+ var maxArity = Math.max(sourceArity, targetArity);
+ for (var i = 0; i < maxArity; i++) {
+ var targetFlags = i < targetArity ? target.target.elementFlags[i] : targetRestFlag;
+ var sourceFlags = isTupleType(source) && i < sourceArity ? source.target.elementFlags[i] : sourceRestFlag;
+ var canExcludeDiscriminants = !!excludedProperties;
+ if (sourceFlags && targetFlags) {
+ if (targetFlags & 8 /* Variadic */ && !(sourceFlags & 8 /* Variadic */) ||
+ (sourceFlags & 8 /* Variadic */ && !(targetFlags & 12 /* Variable */))) {
+ if (reportErrors) {
+ reportError(ts.Diagnostics.Element_at_index_0_is_variadic_in_one_type_but_not_in_the_other, i);
+ }
+ return 0 /* False */;
+ }
+ if (targetFlags & 1 /* Required */) {
+ if (!(sourceFlags & 1 /* Required */)) {
+ if (reportErrors) {
+ reportError(ts.Diagnostics.Property_0_is_optional_in_type_1_but_required_in_type_2, i, typeToString(source), typeToString(target));
+ }
+ return 0 /* False */;
+ }
+ }
+ // We can only exclude discriminant properties if we have not yet encountered a variable-length element.
+ if (canExcludeDiscriminants) {
+ if (sourceFlags & 12 /* Variable */ || targetFlags & 12 /* Variable */) {
+ canExcludeDiscriminants = false;
+ }
+ if (canExcludeDiscriminants && (excludedProperties === null || excludedProperties === void 0 ? void 0 : excludedProperties.has(("" + i)))) {
+ continue;
+ }
+ }
+ var sourceType = getTypeArguments(source)[Math.min(i, sourceArity - 1)];
+ var targetType = getTypeArguments(target)[Math.min(i, targetArity - 1)];
+ var targetCheckType = sourceFlags & 8 /* Variadic */ && targetFlags & 4 /* Rest */ ? createArrayType(targetType) : targetType;
+ var related = isRelatedTo(sourceType, targetCheckType, reportErrors, /*headMessage*/ undefined, intersectionState);
+ if (!related) {
+ if (reportErrors) {
+ reportIncompatibleError(ts.Diagnostics.Types_of_property_0_are_incompatible, i);
+ }
+ return 0 /* False */;
}
+ result &= related;
}
- return evolvedType_1 === type ? flowType : createFlowType(evolvedType_1, isIncomplete(flowType));
}
- return flowType;
+ return result;
+ }
+ if (target.target.combinedFlags & 12 /* Variable */) {
+ return 0 /* False */;
}
}
- return undefined;
- }
- function getTypeAtFlowCondition(flow) {
- var flowType = getTypeAtFlowNode(flow.antecedent);
- var type = getTypeFromFlowType(flowType);
- if (type.flags & 131072 /* Never */) {
- return flowType;
+ var requireOptionalProperties = (relation === subtypeRelation || relation === strictSubtypeRelation) && !isObjectLiteralType(source) && !isEmptyArrayLiteralType(source) && !isTupleType(source);
+ var unmatchedProperty = getUnmatchedProperty(source, target, requireOptionalProperties, /*matchDiscriminantProperties*/ false);
+ if (unmatchedProperty) {
+ if (reportErrors) {
+ reportUnmatchedProperty(source, target, unmatchedProperty, requireOptionalProperties);
+ }
+ return 0 /* False */;
}
- // If we have an antecedent type (meaning we're reachable in some way), we first
- // attempt to narrow the antecedent type. If that produces the never type, and if
- // the antecedent type is incomplete (i.e. a transient type in a loop), then we
- // take the type guard as an indication that control *could* reach here once we
- // have the complete type. We proceed by switching to the silent never type which
- // doesn't report errors when operators are applied to it. Note that this is the
- // *only* place a silent never type is ever generated.
- var assumeTrue = (flow.flags & 32 /* TrueCondition */) !== 0;
- var nonEvolvingType = finalizeEvolvingArrayType(type);
- var narrowedType = narrowType(nonEvolvingType, flow.node, assumeTrue);
- if (narrowedType === nonEvolvingType) {
- return flowType;
+ if (isObjectLiteralType(target)) {
+ for (var _i = 0, _a = excludeProperties(getPropertiesOfType(source), excludedProperties); _i < _a.length; _i++) {
+ var sourceProp = _a[_i];
+ if (!getPropertyOfObjectType(target, sourceProp.escapedName)) {
+ var sourceType = getTypeOfSymbol(sourceProp);
+ if (!(sourceType === undefinedType || sourceType === undefinedWideningType || sourceType === optionalType)) {
+ if (reportErrors) {
+ reportError(ts.Diagnostics.Property_0_does_not_exist_on_type_1, symbolToString(sourceProp), typeToString(target));
+ }
+ return 0 /* False */;
+ }
+ }
+ }
+ }
+ // We only call this for union target types when we're attempting to do excess property checking - in those cases, we want to get _all possible props_
+ // from the target union, across all members
+ var properties = getPropertiesOfType(target);
+ var numericNamesOnly = isTupleType(source) && isTupleType(target);
+ for (var _b = 0, _c = excludeProperties(properties, excludedProperties); _b < _c.length; _b++) {
+ var targetProp = _c[_b];
+ var name = targetProp.escapedName;
+ if (!(targetProp.flags & 4194304 /* Prototype */) && (!numericNamesOnly || isNumericLiteralName(name) || name === "length")) {
+ var sourceProp = getPropertyOfType(source, name);
+ if (sourceProp && sourceProp !== targetProp) {
+ var related = propertyRelatedTo(source, target, sourceProp, targetProp, getTypeOfSymbol, reportErrors, intersectionState, relation === comparableRelation);
+ if (!related) {
+ return 0 /* False */;
+ }
+ result &= related;
+ }
+ }
}
- var incomplete = isIncomplete(flowType);
- var resultType = incomplete && narrowedType.flags & 131072 /* Never */ ? silentNeverType : narrowedType;
- return createFlowType(resultType, incomplete);
+ return result;
}
- function getTypeAtSwitchClause(flow) {
- var expr = flow.switchStatement.expression;
- var flowType = getTypeAtFlowNode(flow.antecedent);
- var type = getTypeFromFlowType(flowType);
- if (isMatchingReference(reference, expr)) {
- type = narrowTypeBySwitchOnDiscriminant(type, flow.switchStatement, flow.clauseStart, flow.clauseEnd);
+ function propertiesIdenticalTo(source, target, excludedProperties) {
+ if (!(source.flags & 524288 /* Object */ && target.flags & 524288 /* Object */)) {
+ return 0 /* False */;
}
- else if (expr.kind === 204 /* TypeOfExpression */ && isMatchingReference(reference, expr.expression)) {
- type = narrowBySwitchOnTypeOf(type, flow.switchStatement, flow.clauseStart, flow.clauseEnd);
+ var sourceProperties = excludeProperties(getPropertiesOfObjectType(source), excludedProperties);
+ var targetProperties = excludeProperties(getPropertiesOfObjectType(target), excludedProperties);
+ if (sourceProperties.length !== targetProperties.length) {
+ return 0 /* False */;
}
- else {
- if (strictNullChecks) {
- if (optionalChainContainsReference(expr, reference)) {
- type = narrowTypeBySwitchOptionalChainContainment(type, flow.switchStatement, flow.clauseStart, flow.clauseEnd, function (t) { return !(t.flags & (32768 /* Undefined */ | 131072 /* Never */)); });
- }
- else if (expr.kind === 204 /* TypeOfExpression */ && optionalChainContainsReference(expr.expression, reference)) {
- type = narrowTypeBySwitchOptionalChainContainment(type, flow.switchStatement, flow.clauseStart, flow.clauseEnd, function (t) { return !(t.flags & 131072 /* Never */ || t.flags & 128 /* StringLiteral */ && t.value === "undefined"); });
- }
+ var result = -1 /* True */;
+ for (var _i = 0, sourceProperties_1 = sourceProperties; _i < sourceProperties_1.length; _i++) {
+ var sourceProp = sourceProperties_1[_i];
+ var targetProp = getPropertyOfObjectType(target, sourceProp.escapedName);
+ if (!targetProp) {
+ return 0 /* False */;
}
- if (isMatchingReferenceDiscriminant(expr, type)) {
- type = narrowTypeByDiscriminant(type, expr, function (t) { return narrowTypeBySwitchOnDiscriminant(t, flow.switchStatement, flow.clauseStart, flow.clauseEnd); });
+ var related = compareProperties(sourceProp, targetProp, isRelatedTo);
+ if (!related) {
+ return 0 /* False */;
}
+ result &= related;
}
- return createFlowType(type, isIncomplete(flowType));
+ return result;
}
- function getTypeAtFlowBranchLabel(flow) {
- var antecedentTypes = [];
- var subtypeReduction = false;
- var seenIncomplete = false;
- var bypassFlow;
- for (var _i = 0, _a = flow.antecedents; _i < _a.length; _i++) {
- var antecedent = _a[_i];
- if (!bypassFlow && antecedent.flags & 128 /* SwitchClause */ && antecedent.clauseStart === antecedent.clauseEnd) {
- // The antecedent is the bypass branch of a potentially exhaustive switch statement.
- bypassFlow = antecedent;
- continue;
- }
- var flowType = getTypeAtFlowNode(antecedent);
- var type = getTypeFromFlowType(flowType);
- // If the type at a particular antecedent path is the declared type and the
- // reference is known to always be assigned (i.e. when declared and initial types
- // are the same), there is no reason to process more antecedents since the only
- // possible outcome is subtypes that will be removed in the final union type anyway.
- if (type === declaredType && declaredType === initialType) {
- return type;
- }
- ts.pushIfUnique(antecedentTypes, type);
- // If an antecedent type is not a subset of the declared type, we need to perform
- // subtype reduction. This happens when a "foreign" type is injected into the control
- // flow using the instanceof operator or a user defined type predicate.
- if (!isTypeSubsetOf(type, declaredType)) {
- subtypeReduction = true;
+ function signaturesRelatedTo(source, target, kind, reportErrors) {
+ var _a, _b;
+ if (relation === identityRelation) {
+ return signaturesIdenticalTo(source, target, kind);
+ }
+ if (target === anyFunctionType || source === anyFunctionType) {
+ return -1 /* True */;
+ }
+ var sourceIsJSConstructor = source.symbol && isJSConstructor(source.symbol.valueDeclaration);
+ var targetIsJSConstructor = target.symbol && isJSConstructor(target.symbol.valueDeclaration);
+ var sourceSignatures = getSignaturesOfType(source, (sourceIsJSConstructor && kind === 1 /* Construct */) ?
+ 0 /* Call */ : kind);
+ var targetSignatures = getSignaturesOfType(target, (targetIsJSConstructor && kind === 1 /* Construct */) ?
+ 0 /* Call */ : kind);
+ if (kind === 1 /* Construct */ && sourceSignatures.length && targetSignatures.length) {
+ if (ts.isAbstractConstructorType(source) && !ts.isAbstractConstructorType(target)) {
+ // An abstract constructor type is not assignable to a non-abstract constructor type
+ // as it would otherwise be possible to new an abstract class. Note that the assignability
+ // check we perform for an extends clause excludes construct signatures from the target,
+ // so this check never proceeds.
+ if (reportErrors) {
+ reportError(ts.Diagnostics.Cannot_assign_an_abstract_constructor_type_to_a_non_abstract_constructor_type);
+ }
+ return 0 /* False */;
}
- if (isIncomplete(flowType)) {
- seenIncomplete = true;
+ if (!constructorVisibilitiesAreCompatible(sourceSignatures[0], targetSignatures[0], reportErrors)) {
+ return 0 /* False */;
}
}
- if (bypassFlow) {
- var flowType = getTypeAtFlowNode(bypassFlow);
- var type = getTypeFromFlowType(flowType);
- // If the bypass flow contributes a type we haven't seen yet and the switch statement
- // isn't exhaustive, process the bypass flow type. Since exhaustiveness checks increase
- // the risk of circularities, we only want to perform them when they make a difference.
- if (!ts.contains(antecedentTypes, type) && !isExhaustiveSwitchStatement(bypassFlow.switchStatement)) {
- if (type === declaredType && declaredType === initialType) {
- return type;
+ var result = -1 /* True */;
+ var saveErrorInfo = captureErrorCalculationState();
+ var incompatibleReporter = kind === 1 /* Construct */ ? reportIncompatibleConstructSignatureReturn : reportIncompatibleCallSignatureReturn;
+ var sourceObjectFlags = ts.getObjectFlags(source);
+ var targetObjectFlags = ts.getObjectFlags(target);
+ if (sourceObjectFlags & 64 /* Instantiated */ && targetObjectFlags & 64 /* Instantiated */ && source.symbol === target.symbol) {
+ // We have instantiations of the same anonymous type (which typically will be the type of a
+ // method). Simply do a pairwise comparison of the signatures in the two signature lists instead
+ // of the much more expensive N * M comparison matrix we explore below. We erase type parameters
+ // as they are known to always be the same.
+ for (var i = 0; i < targetSignatures.length; i++) {
+ var related = signatureRelatedTo(sourceSignatures[i], targetSignatures[i], /*erase*/ true, reportErrors, incompatibleReporter(sourceSignatures[i], targetSignatures[i]));
+ if (!related) {
+ return 0 /* False */;
}
- antecedentTypes.push(type);
- if (!isTypeSubsetOf(type, declaredType)) {
- subtypeReduction = true;
+ result &= related;
+ }
+ }
+ else if (sourceSignatures.length === 1 && targetSignatures.length === 1) {
+ // For simple functions (functions with a single signature) we only erase type parameters for
+ // the comparable relation. Otherwise, if the source signature is generic, we instantiate it
+ // in the context of the target signature before checking the relationship. Ideally we'd do
+ // this regardless of the number of signatures, but the potential costs are prohibitive due
+ // to the quadratic nature of the logic below.
+ var eraseGenerics = relation === comparableRelation || !!compilerOptions.noStrictGenericChecks;
+ var sourceSignature = ts.first(sourceSignatures);
+ var targetSignature = ts.first(targetSignatures);
+ result = signatureRelatedTo(sourceSignature, targetSignature, eraseGenerics, reportErrors, incompatibleReporter(sourceSignature, targetSignature));
+ if (!result && reportErrors && kind === 1 /* Construct */ && (sourceObjectFlags & targetObjectFlags) &&
+ (((_a = targetSignature.declaration) === null || _a === void 0 ? void 0 : _a.kind) === 166 /* Constructor */ || ((_b = sourceSignature.declaration) === null || _b === void 0 ? void 0 : _b.kind) === 166 /* Constructor */)) {
+ var constructSignatureToString = function (signature) {
+ return signatureToString(signature, /*enclosingDeclaration*/ undefined, 262144 /* WriteArrowStyleSignature */, kind);
+ };
+ reportError(ts.Diagnostics.Type_0_is_not_assignable_to_type_1, constructSignatureToString(sourceSignature), constructSignatureToString(targetSignature));
+ reportError(ts.Diagnostics.Types_of_construct_signatures_are_incompatible);
+ return result;
+ }
+ }
+ else {
+ outer: for (var _i = 0, targetSignatures_1 = targetSignatures; _i < targetSignatures_1.length; _i++) {
+ var t = targetSignatures_1[_i];
+ // Only elaborate errors from the first failure
+ var shouldElaborateErrors = reportErrors;
+ for (var _c = 0, sourceSignatures_1 = sourceSignatures; _c < sourceSignatures_1.length; _c++) {
+ var s = sourceSignatures_1[_c];
+ var related = signatureRelatedTo(s, t, /*erase*/ true, shouldElaborateErrors, incompatibleReporter(s, t));
+ if (related) {
+ result &= related;
+ resetErrorInfo(saveErrorInfo);
+ continue outer;
+ }
+ shouldElaborateErrors = false;
}
- if (isIncomplete(flowType)) {
- seenIncomplete = true;
+ if (shouldElaborateErrors) {
+ reportError(ts.Diagnostics.Type_0_provides_no_match_for_the_signature_1, typeToString(source), signatureToString(t, /*enclosingDeclaration*/ undefined, /*flags*/ undefined, kind));
}
+ return 0 /* False */;
}
}
- return createFlowType(getUnionOrEvolvingArrayType(antecedentTypes, subtypeReduction ? 2 /* Subtype */ : 1 /* Literal */), seenIncomplete);
+ return result;
}
- function getTypeAtFlowLoopLabel(flow) {
- // If we have previously computed the control flow type for the reference at
- // this flow loop junction, return the cached type.
- var id = getFlowNodeId(flow);
- var cache = flowLoopCaches[id] || (flowLoopCaches[id] = ts.createMap());
- var key = getOrSetCacheKey();
- if (!key) {
- // No cache key is generated when binding patterns are in unnarrowable situations
- return declaredType;
+ function reportIncompatibleCallSignatureReturn(siga, sigb) {
+ if (siga.parameters.length === 0 && sigb.parameters.length === 0) {
+ return function (source, target) { return reportIncompatibleError(ts.Diagnostics.Call_signatures_with_no_arguments_have_incompatible_return_types_0_and_1, typeToString(source), typeToString(target)); };
}
- var cached = cache.get(key);
- if (cached) {
- return cached;
+ return function (source, target) { return reportIncompatibleError(ts.Diagnostics.Call_signature_return_types_0_and_1_are_incompatible, typeToString(source), typeToString(target)); };
+ }
+ function reportIncompatibleConstructSignatureReturn(siga, sigb) {
+ if (siga.parameters.length === 0 && sigb.parameters.length === 0) {
+ return function (source, target) { return reportIncompatibleError(ts.Diagnostics.Construct_signatures_with_no_arguments_have_incompatible_return_types_0_and_1, typeToString(source), typeToString(target)); };
}
- // If this flow loop junction and reference are already being processed, return
- // the union of the types computed for each branch so far, marked as incomplete.
- // It is possible to see an empty array in cases where loops are nested and the
- // back edge of the outer loop reaches an inner loop that is already being analyzed.
- // In such cases we restart the analysis of the inner loop, which will then see
- // a non-empty in-process array for the outer loop and eventually terminate because
- // the first antecedent of a loop junction is always the non-looping control flow
- // path that leads to the top.
- for (var i = flowLoopStart; i < flowLoopCount; i++) {
- if (flowLoopNodes[i] === flow && flowLoopKeys[i] === key && flowLoopTypes[i].length) {
- return createFlowType(getUnionOrEvolvingArrayType(flowLoopTypes[i], 1 /* Literal */), /*incomplete*/ true);
- }
+ return function (source, target) { return reportIncompatibleError(ts.Diagnostics.Construct_signature_return_types_0_and_1_are_incompatible, typeToString(source), typeToString(target)); };
+ }
+ /**
+ * See signatureAssignableTo, compareSignaturesIdentical
+ */
+ function signatureRelatedTo(source, target, erase, reportErrors, incompatibleReporter) {
+ return compareSignaturesRelated(erase ? getErasedSignature(source) : source, erase ? getErasedSignature(target) : target, relation === strictSubtypeRelation ? 8 /* StrictArity */ : 0, reportErrors, reportError, incompatibleReporter, isRelatedTo, makeFunctionTypeMapper(reportUnreliableMarkers));
+ }
+ function signaturesIdenticalTo(source, target, kind) {
+ var sourceSignatures = getSignaturesOfType(source, kind);
+ var targetSignatures = getSignaturesOfType(target, kind);
+ if (sourceSignatures.length !== targetSignatures.length) {
+ return 0 /* False */;
}
- // Add the flow loop junction and reference to the in-process stack and analyze
- // each antecedent code path.
- var antecedentTypes = [];
- var subtypeReduction = false;
- var firstAntecedentType;
- for (var _i = 0, _a = flow.antecedents; _i < _a.length; _i++) {
- var antecedent = _a[_i];
- var flowType = void 0;
- if (!firstAntecedentType) {
- // The first antecedent of a loop junction is always the non-looping control
- // flow path that leads to the top.
- flowType = firstAntecedentType = getTypeAtFlowNode(antecedent);
+ var result = -1 /* True */;
+ for (var i = 0; i < sourceSignatures.length; i++) {
+ var related = compareSignaturesIdentical(sourceSignatures[i], targetSignatures[i], /*partialMatch*/ false, /*ignoreThisTypes*/ false, /*ignoreReturnTypes*/ false, isRelatedTo);
+ if (!related) {
+ return 0 /* False */;
}
- else {
- // All but the first antecedent are the looping control flow paths that lead
- // back to the loop junction. We track these on the flow loop stack.
- flowLoopNodes[flowLoopCount] = flow;
- flowLoopKeys[flowLoopCount] = key;
- flowLoopTypes[flowLoopCount] = antecedentTypes;
- flowLoopCount++;
- var saveFlowTypeCache = flowTypeCache;
- flowTypeCache = undefined;
- flowType = getTypeAtFlowNode(antecedent);
- flowTypeCache = saveFlowTypeCache;
- flowLoopCount--;
- // If we see a value appear in the cache it is a sign that control flow analysis
- // was restarted and completed by checkExpressionCached. We can simply pick up
- // the resulting type and bail out.
- var cached_1 = cache.get(key);
- if (cached_1) {
- return cached_1;
- }
+ result &= related;
+ }
+ return result;
+ }
+ function eachPropertyRelatedTo(source, target, kind, reportErrors) {
+ var result = -1 /* True */;
+ var props = source.flags & 2097152 /* Intersection */ ? getPropertiesOfUnionOrIntersectionType(source) : getPropertiesOfObjectType(source);
+ for (var _i = 0, props_2 = props; _i < props_2.length; _i++) {
+ var prop = props_2[_i];
+ // Skip over ignored JSX and symbol-named members
+ if (isIgnoredJsxProperty(source, prop)) {
+ continue;
}
- var type = getTypeFromFlowType(flowType);
- ts.pushIfUnique(antecedentTypes, type);
- // If an antecedent type is not a subset of the declared type, we need to perform
- // subtype reduction. This happens when a "foreign" type is injected into the control
- // flow using the instanceof operator or a user defined type predicate.
- if (!isTypeSubsetOf(type, declaredType)) {
- subtypeReduction = true;
+ var nameType = getSymbolLinks(prop).nameType;
+ if (nameType && nameType.flags & 8192 /* UniqueESSymbol */) {
+ continue;
}
- // If the type at a particular antecedent path is the declared type there is no
- // reason to process more antecedents since the only possible outcome is subtypes
- // that will be removed in the final union type anyway.
- if (type === declaredType) {
- break;
+ if (kind === 0 /* String */ || isNumericLiteralName(prop.escapedName)) {
+ var related = isRelatedTo(getTypeOfSymbol(prop), target, reportErrors);
+ if (!related) {
+ if (reportErrors) {
+ reportError(ts.Diagnostics.Property_0_is_incompatible_with_index_signature, symbolToString(prop));
+ }
+ return 0 /* False */;
+ }
+ result &= related;
}
}
- // The result is incomplete if the first antecedent (the non-looping control flow path)
- // is incomplete.
- var result = getUnionOrEvolvingArrayType(antecedentTypes, subtypeReduction ? 2 /* Subtype */ : 1 /* Literal */);
- if (isIncomplete(firstAntecedentType)) {
- return createFlowType(result, /*incomplete*/ true);
- }
- cache.set(key, result);
return result;
}
- function isMatchingReferenceDiscriminant(expr, computedType) {
- if (!(computedType.flags & 1048576 /* Union */) || !ts.isAccessExpression(expr)) {
- return false;
- }
- var name = getAccessedPropertyName(expr);
- if (name === undefined) {
- return false;
+ function indexTypeRelatedTo(sourceType, targetType, reportErrors) {
+ var related = isRelatedTo(sourceType, targetType, reportErrors);
+ if (!related && reportErrors) {
+ reportError(ts.Diagnostics.Index_signatures_are_incompatible);
}
- return isMatchingReference(reference, expr.expression) && isDiscriminantProperty(computedType, name);
+ return related;
}
- function narrowTypeByDiscriminant(type, access, narrowType) {
- var propName = getAccessedPropertyName(access);
- if (propName === undefined) {
- return type;
- }
- var propType = getTypeOfPropertyOfType(type, propName);
- if (!propType) {
- return type;
+ function indexTypesRelatedTo(source, target, kind, sourceIsPrimitive, reportErrors, intersectionState) {
+ if (relation === identityRelation) {
+ return indexTypesIdenticalTo(source, target, kind);
}
- var narrowedPropType = narrowType(propType);
- return filterType(type, function (t) {
- var discriminantType = getTypeOfPropertyOrIndexSignature(t, propName);
- return !(discriminantType.flags & 131072 /* Never */) && isTypeComparableTo(discriminantType, narrowedPropType);
- });
- }
- function narrowTypeByTruthiness(type, expr, assumeTrue) {
- if (isMatchingReference(reference, expr)) {
- return getTypeWithFacts(type, assumeTrue ? 4194304 /* Truthy */ : 8388608 /* Falsy */);
+ var targetType = getIndexTypeOfType(target, kind);
+ if (!targetType || targetType.flags & 1 /* Any */ && !sourceIsPrimitive) {
+ // Index signature of type any permits assignment from everything but primitives
+ return -1 /* True */;
}
- if (strictNullChecks && assumeTrue && optionalChainContainsReference(expr, reference)) {
- type = getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */);
+ if (isGenericMappedType(source)) {
+ // A generic mapped type { [P in K]: T } is related to a type with an index signature
+ // { [x: string]: U }, and optionally with an index signature { [x: number]: V },
+ // if T is related to U and V.
+ return getIndexTypeOfType(target, 0 /* String */) ? isRelatedTo(getTemplateTypeFromMappedType(source), targetType, reportErrors) : 0 /* False */;
}
- if (isMatchingReferenceDiscriminant(expr, declaredType)) {
- return narrowTypeByDiscriminant(type, expr, function (t) { return getTypeWithFacts(t, assumeTrue ? 4194304 /* Truthy */ : 8388608 /* Falsy */); });
+ var indexType = getIndexTypeOfType(source, kind) || kind === 1 /* Number */ && getIndexTypeOfType(source, 0 /* String */);
+ if (indexType) {
+ return indexTypeRelatedTo(indexType, targetType, reportErrors);
}
- return type;
- }
- function isTypePresencePossible(type, propName, assumeTrue) {
- if (getIndexInfoOfType(type, 0 /* String */)) {
- return true;
+ if (!(intersectionState & 1 /* Source */) && isObjectTypeWithInferableIndex(source)) {
+ // Intersection constituents are never considered to have an inferred index signature
+ var related = eachPropertyRelatedTo(source, targetType, kind, reportErrors);
+ if (related && kind === 0 /* String */) {
+ var numberIndexType = getIndexTypeOfType(source, 1 /* Number */);
+ if (numberIndexType) {
+ related &= indexTypeRelatedTo(numberIndexType, targetType, reportErrors);
+ }
+ }
+ return related;
}
- var prop = getPropertyOfType(type, propName);
- if (prop) {
- return prop.flags & 16777216 /* Optional */ ? true : assumeTrue;
+ if (reportErrors) {
+ reportError(ts.Diagnostics.Index_signature_is_missing_in_type_0, typeToString(source));
}
- return !assumeTrue;
+ return 0 /* False */;
}
- function narrowByInKeyword(type, literal, assumeTrue) {
- if (type.flags & (1048576 /* Union */ | 524288 /* Object */) || isThisTypeParameter(type)) {
- var propName_1 = ts.escapeLeadingUnderscores(literal.text);
- return filterType(type, function (t) { return isTypePresencePossible(t, propName_1, assumeTrue); });
+ function indexTypesIdenticalTo(source, target, indexKind) {
+ var targetInfo = getIndexInfoOfType(target, indexKind);
+ var sourceInfo = getIndexInfoOfType(source, indexKind);
+ if (!sourceInfo && !targetInfo) {
+ return -1 /* True */;
}
- return type;
- }
- function narrowTypeByBinaryExpression(type, expr, assumeTrue) {
- switch (expr.operatorToken.kind) {
- case 62 /* EqualsToken */:
- return narrowTypeByTruthiness(narrowType(type, expr.right, assumeTrue), expr.left, assumeTrue);
- case 34 /* EqualsEqualsToken */:
- case 35 /* ExclamationEqualsToken */:
- case 36 /* EqualsEqualsEqualsToken */:
- case 37 /* ExclamationEqualsEqualsToken */:
- var operator_1 = expr.operatorToken.kind;
- var left_1 = getReferenceCandidate(expr.left);
- var right_1 = getReferenceCandidate(expr.right);
- if (left_1.kind === 204 /* TypeOfExpression */ && ts.isStringLiteralLike(right_1)) {
- return narrowTypeByTypeof(type, left_1, operator_1, right_1, assumeTrue);
- }
- if (right_1.kind === 204 /* TypeOfExpression */ && ts.isStringLiteralLike(left_1)) {
- return narrowTypeByTypeof(type, right_1, operator_1, left_1, assumeTrue);
- }
- if (isMatchingReference(reference, left_1)) {
- return narrowTypeByEquality(type, operator_1, right_1, assumeTrue);
- }
- if (isMatchingReference(reference, right_1)) {
- return narrowTypeByEquality(type, operator_1, left_1, assumeTrue);
- }
- if (strictNullChecks) {
- if (optionalChainContainsReference(left_1, reference)) {
- type = narrowTypeByOptionalChainContainment(type, operator_1, right_1, assumeTrue);
- }
- else if (optionalChainContainsReference(right_1, reference)) {
- type = narrowTypeByOptionalChainContainment(type, operator_1, left_1, assumeTrue);
- }
- }
- if (isMatchingReferenceDiscriminant(left_1, declaredType)) {
- return narrowTypeByDiscriminant(type, left_1, function (t) { return narrowTypeByEquality(t, operator_1, right_1, assumeTrue); });
- }
- if (isMatchingReferenceDiscriminant(right_1, declaredType)) {
- return narrowTypeByDiscriminant(type, right_1, function (t) { return narrowTypeByEquality(t, operator_1, left_1, assumeTrue); });
- }
- if (isMatchingConstructorReference(left_1)) {
- return narrowTypeByConstructor(type, operator_1, right_1, assumeTrue);
- }
- if (isMatchingConstructorReference(right_1)) {
- return narrowTypeByConstructor(type, operator_1, left_1, assumeTrue);
- }
- break;
- case 98 /* InstanceOfKeyword */:
- return narrowTypeByInstanceof(type, expr, assumeTrue);
- case 97 /* InKeyword */:
- var target = getReferenceCandidate(expr.right);
- if (ts.isStringLiteralLike(expr.left) && isMatchingReference(reference, target)) {
- return narrowByInKeyword(type, expr.left, assumeTrue);
- }
- break;
- case 27 /* CommaToken */:
- return narrowType(type, expr.right, assumeTrue);
+ if (sourceInfo && targetInfo && sourceInfo.isReadonly === targetInfo.isReadonly) {
+ return isRelatedTo(sourceInfo.type, targetInfo.type);
}
- return type;
- }
- function narrowTypeByOptionalChainContainment(type, operator, value, assumeTrue) {
- // We are in a branch of obj?.foo === value (or any one of the other equality operators). We narrow obj as follows:
- // When operator is === and type of value excludes undefined, null and undefined is removed from type of obj in true branch.
- // When operator is !== and type of value excludes undefined, null and undefined is removed from type of obj in false branch.
- // When operator is == and type of value excludes null and undefined, null and undefined is removed from type of obj in true branch.
- // When operator is != and type of value excludes null and undefined, null and undefined is removed from type of obj in false branch.
- // When operator is === and type of value is undefined, null and undefined is removed from type of obj in false branch.
- // When operator is !== and type of value is undefined, null and undefined is removed from type of obj in true branch.
- // When operator is == and type of value is null or undefined, null and undefined is removed from type of obj in false branch.
- // When operator is != and type of value is null or undefined, null and undefined is removed from type of obj in true branch.
- var equalsOperator = operator === 34 /* EqualsEqualsToken */ || operator === 36 /* EqualsEqualsEqualsToken */;
- var nullableFlags = operator === 34 /* EqualsEqualsToken */ || operator === 35 /* ExclamationEqualsToken */ ? 98304 /* Nullable */ : 32768 /* Undefined */;
- var valueType = getTypeOfExpression(value);
- // Note that we include any and unknown in the exclusion test because their domain includes null and undefined.
- var removeNullable = equalsOperator !== assumeTrue && everyType(valueType, function (t) { return !!(t.flags & nullableFlags); }) ||
- equalsOperator === assumeTrue && everyType(valueType, function (t) { return !(t.flags & (3 /* AnyOrUnknown */ | nullableFlags)); });
- return removeNullable ? getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */) : type;
+ return 0 /* False */;
}
- function narrowTypeByEquality(type, operator, value, assumeTrue) {
- if (type.flags & 1 /* Any */) {
- return type;
- }
- if (operator === 35 /* ExclamationEqualsToken */ || operator === 37 /* ExclamationEqualsEqualsToken */) {
- assumeTrue = !assumeTrue;
+ function constructorVisibilitiesAreCompatible(sourceSignature, targetSignature, reportErrors) {
+ if (!sourceSignature.declaration || !targetSignature.declaration) {
+ return true;
}
- var valueType = getTypeOfExpression(value);
- if ((type.flags & 2 /* Unknown */) && assumeTrue && (operator === 36 /* EqualsEqualsEqualsToken */ || operator === 37 /* ExclamationEqualsEqualsToken */)) {
- if (valueType.flags & (131068 /* Primitive */ | 67108864 /* NonPrimitive */)) {
- return valueType;
- }
- if (valueType.flags & 524288 /* Object */) {
- return nonPrimitiveType;
- }
- return type;
+ var sourceAccessibility = ts.getSelectedEffectiveModifierFlags(sourceSignature.declaration, 24 /* NonPublicAccessibilityModifier */);
+ var targetAccessibility = ts.getSelectedEffectiveModifierFlags(targetSignature.declaration, 24 /* NonPublicAccessibilityModifier */);
+ // A public, protected and private signature is assignable to a private signature.
+ if (targetAccessibility === 8 /* Private */) {
+ return true;
}
- if (valueType.flags & 98304 /* Nullable */) {
- if (!strictNullChecks) {
- return type;
- }
- var doubleEquals = operator === 34 /* EqualsEqualsToken */ || operator === 35 /* ExclamationEqualsToken */;
- var facts = doubleEquals ?
- assumeTrue ? 262144 /* EQUndefinedOrNull */ : 2097152 /* NEUndefinedOrNull */ :
- valueType.flags & 65536 /* Null */ ?
- assumeTrue ? 131072 /* EQNull */ : 1048576 /* NENull */ :
- assumeTrue ? 65536 /* EQUndefined */ : 524288 /* NEUndefined */;
- return getTypeWithFacts(type, facts);
+ // A public and protected signature is assignable to a protected signature.
+ if (targetAccessibility === 16 /* Protected */ && sourceAccessibility !== 8 /* Private */) {
+ return true;
}
- if (type.flags & 67637251 /* NotUnionOrUnit */) {
- return type;
+ // Only a public signature is assignable to public signature.
+ if (targetAccessibility !== 16 /* Protected */ && !sourceAccessibility) {
+ return true;
}
- if (assumeTrue) {
- var filterFn = operator === 34 /* EqualsEqualsToken */ ?
- (function (t) { return areTypesComparable(t, valueType) || isCoercibleUnderDoubleEquals(t, valueType); }) :
- function (t) { return areTypesComparable(t, valueType); };
- var narrowedType = filterType(type, filterFn);
- return narrowedType.flags & 131072 /* Never */ ? type : replacePrimitivesWithLiterals(narrowedType, valueType);
+ if (reportErrors) {
+ reportError(ts.Diagnostics.Cannot_assign_a_0_constructor_type_to_a_1_constructor_type, visibilityToString(sourceAccessibility), visibilityToString(targetAccessibility));
}
- if (isUnitType(valueType)) {
- var regularType_1 = getRegularTypeOfLiteralType(valueType);
- return filterType(type, function (t) { return isUnitType(t) ? !areTypesComparable(t, valueType) : getRegularTypeOfLiteralType(t) !== regularType_1; });
+ return false;
+ }
+ }
+ function typeCouldHaveTopLevelSingletonTypes(type) {
+ // Okay, yes, 'boolean' is a union of 'true | false', but that's not useful
+ // in error reporting scenarios. If you need to use this function but that detail matters,
+ // feel free to add a flag.
+ if (type.flags & 16 /* Boolean */) {
+ return false;
+ }
+ if (type.flags & 3145728 /* UnionOrIntersection */) {
+ return !!ts.forEach(type.types, typeCouldHaveTopLevelSingletonTypes);
+ }
+ if (type.flags & 465829888 /* Instantiable */) {
+ var constraint = getConstraintOfType(type);
+ if (constraint && constraint !== type) {
+ return typeCouldHaveTopLevelSingletonTypes(constraint);
}
- return type;
}
- function narrowTypeByTypeof(type, typeOfExpr, operator, literal, assumeTrue) {
- // We have '==', '!=', '===', or !==' operator with 'typeof xxx' and string literal operands
- if (operator === 35 /* ExclamationEqualsToken */ || operator === 37 /* ExclamationEqualsEqualsToken */) {
- assumeTrue = !assumeTrue;
+ return isUnitType(type) || !!(type.flags & 134217728 /* TemplateLiteral */);
+ }
+ function getBestMatchingType(source, target, isRelatedTo) {
+ if (isRelatedTo === void 0) { isRelatedTo = compareTypesAssignable; }
+ return findMatchingDiscriminantType(source, target, isRelatedTo, /*skipPartial*/ true) ||
+ findMatchingTypeReferenceOrTypeAliasReference(source, target) ||
+ findBestTypeForObjectLiteral(source, target) ||
+ findBestTypeForInvokable(source, target) ||
+ findMostOverlappyType(source, target);
+ }
+ function discriminateTypeByDiscriminableItems(target, discriminators, related, defaultValue, skipPartial) {
+ // undefined=unknown, true=discriminated, false=not discriminated
+ // The state of each type progresses from left to right. Discriminated types stop at 'true'.
+ var discriminable = target.types.map(function (_) { return undefined; });
+ for (var _i = 0, discriminators_1 = discriminators; _i < discriminators_1.length; _i++) {
+ var _a = discriminators_1[_i], getDiscriminatingType = _a[0], propertyName = _a[1];
+ var targetProp = getUnionOrIntersectionProperty(target, propertyName);
+ if (skipPartial && targetProp && ts.getCheckFlags(targetProp) & 16 /* ReadPartial */) {
+ continue;
}
- var target = getReferenceCandidate(typeOfExpr.expression);
- if (!isMatchingReference(reference, target)) {
- if (strictNullChecks && optionalChainContainsReference(target, reference) && assumeTrue === (literal.text !== "undefined")) {
- return getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */);
+ var i = 0;
+ for (var _b = 0, _c = target.types; _b < _c.length; _b++) {
+ var type = _c[_b];
+ var targetType = getTypeOfPropertyOfType(type, propertyName);
+ if (targetType && related(getDiscriminatingType(), targetType)) {
+ discriminable[i] = discriminable[i] === undefined ? true : discriminable[i];
}
- return type;
- }
- if (type.flags & 1 /* Any */ && literal.text === "function") {
- return type;
- }
- if (assumeTrue && type.flags & 2 /* Unknown */ && literal.text === "object") {
- // The pattern x && typeof x === 'object', where x is of type unknown, narrows x to type object. We don't
- // need to check for the reverse typeof x === 'object' && x since that already narrows correctly.
- if (typeOfExpr.parent.parent.kind === 209 /* BinaryExpression */) {
- var expr = typeOfExpr.parent.parent;
- if (expr.operatorToken.kind === 55 /* AmpersandAmpersandToken */ && expr.right === typeOfExpr.parent && containsTruthyCheck(reference, expr.left)) {
- return nonPrimitiveType;
- }
+ else {
+ discriminable[i] = false;
}
- return getUnionType([nonPrimitiveType, nullType]);
+ i++;
}
- var facts = assumeTrue ?
- typeofEQFacts.get(literal.text) || 128 /* TypeofEQHostObject */ :
- typeofNEFacts.get(literal.text) || 32768 /* TypeofNEHostObject */;
- return getTypeWithFacts(assumeTrue ? mapType(type, narrowTypeForTypeof) : type, facts);
- function narrowTypeForTypeof(type) {
- // We narrow a non-union type to an exact primitive type if the non-union type
- // is a supertype of that primitive type. For example, type 'any' can be narrowed
- // to one of the primitive types.
- var targetType = literal.text === "function" ? globalFunctionType : typeofTypesByName.get(literal.text);
- if (targetType) {
- if (isTypeSubtypeOf(type, targetType)) {
- return type;
- }
- if (isTypeSubtypeOf(targetType, type)) {
- return targetType;
- }
- if (type.flags & 63176704 /* Instantiable */) {
- var constraint = getBaseConstraintOfType(type) || anyType;
- if (isTypeSubtypeOf(targetType, constraint)) {
- return getIntersectionType([type, targetType]);
- }
- }
- }
- return type;
+ }
+ var match = discriminable.indexOf(/*searchElement*/ true);
+ if (match === -1) {
+ return defaultValue;
+ }
+ // make sure exactly 1 matches before returning it
+ var nextMatch = discriminable.indexOf(/*searchElement*/ true, match + 1);
+ while (nextMatch !== -1) {
+ if (!isTypeIdenticalTo(target.types[match], target.types[nextMatch])) {
+ return defaultValue;
}
+ nextMatch = discriminable.indexOf(/*searchElement*/ true, nextMatch + 1);
}
- function narrowTypeBySwitchOptionalChainContainment(type, switchStatement, clauseStart, clauseEnd, clauseCheck) {
- var everyClauseChecks = clauseStart !== clauseEnd && ts.every(getSwitchClauseTypes(switchStatement).slice(clauseStart, clauseEnd), clauseCheck);
- return everyClauseChecks ? getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */) : type;
+ return target.types[match];
+ }
+ /**
+ * A type is 'weak' if it is an object type with at least one optional property
+ * and no required properties, call/construct signatures or index signatures
+ */
+ function isWeakType(type) {
+ if (type.flags & 524288 /* Object */) {
+ var resolved = resolveStructuredTypeMembers(type);
+ return resolved.callSignatures.length === 0 && resolved.constructSignatures.length === 0 &&
+ !resolved.stringIndexInfo && !resolved.numberIndexInfo &&
+ resolved.properties.length > 0 &&
+ ts.every(resolved.properties, function (p) { return !!(p.flags & 16777216 /* Optional */); });
}
- function narrowTypeBySwitchOnDiscriminant(type, switchStatement, clauseStart, clauseEnd) {
- // We only narrow if all case expressions specify
- // values with unit types, except for the case where
- // `type` is unknown. In this instance we map object
- // types to the nonPrimitive type and narrow with that.
- var switchTypes = getSwitchClauseTypes(switchStatement);
- if (!switchTypes.length) {
- return type;
+ if (type.flags & 2097152 /* Intersection */) {
+ return ts.every(type.types, isWeakType);
+ }
+ return false;
+ }
+ function hasCommonProperties(source, target, isComparingJsxAttributes) {
+ for (var _i = 0, _a = getPropertiesOfType(source); _i < _a.length; _i++) {
+ var prop = _a[_i];
+ if (isKnownProperty(target, prop.escapedName, isComparingJsxAttributes)) {
+ return true;
}
- var clauseTypes = switchTypes.slice(clauseStart, clauseEnd);
- var hasDefaultClause = clauseStart === clauseEnd || ts.contains(clauseTypes, neverType);
- if ((type.flags & 2 /* Unknown */) && !hasDefaultClause) {
- var groundClauseTypes = void 0;
- for (var i = 0; i < clauseTypes.length; i += 1) {
- var t = clauseTypes[i];
- if (t.flags & (131068 /* Primitive */ | 67108864 /* NonPrimitive */)) {
- if (groundClauseTypes !== undefined) {
- groundClauseTypes.push(t);
- }
- }
- else if (t.flags & 524288 /* Object */) {
- if (groundClauseTypes === undefined) {
- groundClauseTypes = clauseTypes.slice(0, i);
- }
- groundClauseTypes.push(nonPrimitiveType);
+ }
+ return false;
+ }
+ // Return a type reference where the source type parameter is replaced with the target marker
+ // type, and flag the result as a marker type reference.
+ function getMarkerTypeReference(type, source, target) {
+ var result = createTypeReference(type, ts.map(type.typeParameters, function (t) { return t === source ? target : t; }));
+ result.objectFlags |= 8192 /* MarkerType */;
+ return result;
+ }
+ function getAliasVariances(symbol) {
+ var links = getSymbolLinks(symbol);
+ return getVariancesWorker(links.typeParameters, links, function (_links, param, marker) {
+ var type = getTypeAliasInstantiation(symbol, instantiateTypes(links.typeParameters, makeUnaryTypeMapper(param, marker)));
+ type.aliasTypeArgumentsContainsMarker = true;
+ return type;
+ });
+ }
+ // Return an array containing the variance of each type parameter. The variance is effectively
+ // a digest of the type comparisons that occur for each type argument when instantiations of the
+ // generic type are structurally compared. We infer the variance information by comparing
+ // instantiations of the generic type for type arguments with known relations. The function
+ // returns the emptyArray singleton when invoked recursively for the given generic type.
+ function getVariancesWorker(typeParameters, cache, createMarkerType) {
+ var _a, _b, _c;
+ if (typeParameters === void 0) { typeParameters = ts.emptyArray; }
+ var variances = cache.variances;
+ if (!variances) {
+ ts.tracing.push("check" /* Check */, "getVariancesWorker", { arity: typeParameters.length, id: (_c = (_a = cache.id) !== null && _a !== void 0 ? _a : (_b = cache.declaredType) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : -1 });
+ // The emptyArray singleton is used to signal a recursive invocation.
+ cache.variances = ts.emptyArray;
+ variances = [];
+ var _loop_19 = function (tp) {
+ var unmeasurable = false;
+ var unreliable = false;
+ var oldHandler = outofbandVarianceMarkerHandler;
+ outofbandVarianceMarkerHandler = function (onlyUnreliable) { return onlyUnreliable ? unreliable = true : unmeasurable = true; };
+ // We first compare instantiations where the type parameter is replaced with
+ // marker types that have a known subtype relationship. From this we can infer
+ // invariance, covariance, contravariance or bivariance.
+ var typeWithSuper = createMarkerType(cache, tp, markerSuperType);
+ var typeWithSub = createMarkerType(cache, tp, markerSubType);
+ var variance = (isTypeAssignableTo(typeWithSub, typeWithSuper) ? 1 /* Covariant */ : 0) |
+ (isTypeAssignableTo(typeWithSuper, typeWithSub) ? 2 /* Contravariant */ : 0);
+ // If the instantiations appear to be related bivariantly it may be because the
+ // type parameter is independent (i.e. it isn't witnessed anywhere in the generic
+ // type). To determine this we compare instantiations where the type parameter is
+ // replaced with marker types that are known to be unrelated.
+ if (variance === 3 /* Bivariant */ && isTypeAssignableTo(createMarkerType(cache, tp, markerOtherType), typeWithSuper)) {
+ variance = 4 /* Independent */;
+ }
+ outofbandVarianceMarkerHandler = oldHandler;
+ if (unmeasurable || unreliable) {
+ if (unmeasurable) {
+ variance |= 8 /* Unmeasurable */;
}
- else {
- return type;
+ if (unreliable) {
+ variance |= 16 /* Unreliable */;
}
}
- return getUnionType(groundClauseTypes === undefined ? clauseTypes : groundClauseTypes);
- }
- var discriminantType = getUnionType(clauseTypes);
- var caseType = discriminantType.flags & 131072 /* Never */ ? neverType :
- replacePrimitivesWithLiterals(filterType(type, function (t) { return areTypesComparable(discriminantType, t); }), discriminantType);
- if (!hasDefaultClause) {
- return caseType;
+ variances.push(variance);
+ };
+ for (var _i = 0, typeParameters_1 = typeParameters; _i < typeParameters_1.length; _i++) {
+ var tp = typeParameters_1[_i];
+ _loop_19(tp);
}
- var defaultType = filterType(type, function (t) { return !(isUnitType(t) && ts.contains(switchTypes, getRegularTypeOfLiteralType(t))); });
- return caseType.flags & 131072 /* Never */ ? defaultType : getUnionType([caseType, defaultType]);
+ cache.variances = variances;
+ ts.tracing.pop();
}
- function getImpliedTypeFromTypeofCase(type, text) {
- switch (text) {
- case "function":
- return type.flags & 1 /* Any */ ? type : globalFunctionType;
- case "object":
- return type.flags & 2 /* Unknown */ ? getUnionType([nonPrimitiveType, nullType]) : type;
- default:
- return typeofTypesByName.get(text) || type;
+ return variances;
+ }
+ function getVariances(type) {
+ // Arrays and tuples are known to be covariant, no need to spend time computing this.
+ if (type === globalArrayType || type === globalReadonlyArrayType || type.objectFlags & 8 /* Tuple */) {
+ return arrayVariances;
+ }
+ return getVariancesWorker(type.typeParameters, type, getMarkerTypeReference);
+ }
+ // Return true if the given type reference has a 'void' type argument for a covariant type parameter.
+ // See comment at call in recursiveTypeRelatedTo for when this case matters.
+ function hasCovariantVoidArgument(typeArguments, variances) {
+ for (var i = 0; i < variances.length; i++) {
+ if ((variances[i] & 7 /* VarianceMask */) === 1 /* Covariant */ && typeArguments[i].flags & 16384 /* Void */) {
+ return true;
}
}
- function narrowTypeForTypeofSwitch(candidate) {
- return function (type) {
- if (isTypeSubtypeOf(candidate, type)) {
- return candidate;
- }
- if (type.flags & 63176704 /* Instantiable */) {
- var constraint = getBaseConstraintOfType(type) || anyType;
- if (isTypeSubtypeOf(candidate, constraint)) {
- return getIntersectionType([type, candidate]);
- }
+ return false;
+ }
+ function isUnconstrainedTypeParameter(type) {
+ return type.flags & 262144 /* TypeParameter */ && !getConstraintOfTypeParameter(type);
+ }
+ function isNonDeferredTypeReference(type) {
+ return !!(ts.getObjectFlags(type) & 4 /* Reference */) && !type.node;
+ }
+ function isTypeReferenceWithGenericArguments(type) {
+ return isNonDeferredTypeReference(type) && ts.some(getTypeArguments(type), function (t) { return isUnconstrainedTypeParameter(t) || isTypeReferenceWithGenericArguments(t); });
+ }
+ /**
+ * getTypeReferenceId(A) returns "111=0-12=1"
+ * where A.id=111 and number.id=12
+ */
+ function getTypeReferenceId(type, typeParameters, depth) {
+ if (depth === void 0) { depth = 0; }
+ var result = "" + type.target.id;
+ for (var _i = 0, _a = getTypeArguments(type); _i < _a.length; _i++) {
+ var t = _a[_i];
+ if (isUnconstrainedTypeParameter(t)) {
+ var index = typeParameters.indexOf(t);
+ if (index < 0) {
+ index = typeParameters.length;
+ typeParameters.push(t);
}
- return type;
- };
- }
- function narrowBySwitchOnTypeOf(type, switchStatement, clauseStart, clauseEnd) {
- var switchWitnesses = getSwitchClauseTypeOfWitnesses(switchStatement, /*retainDefault*/ true);
- if (!switchWitnesses.length) {
- return type;
+ result += "=" + index;
}
- // Equal start and end denotes implicit fallthrough; undefined marks explicit default clause
- var defaultCaseLocation = ts.findIndex(switchWitnesses, function (elem) { return elem === undefined; });
- var hasDefaultClause = clauseStart === clauseEnd || (defaultCaseLocation >= clauseStart && defaultCaseLocation < clauseEnd);
- var clauseWitnesses;
- var switchFacts;
- if (defaultCaseLocation > -1) {
- // We no longer need the undefined denoting an
- // explicit default case. Remove the undefined and
- // fix-up clauseStart and clauseEnd. This means
- // that we don't have to worry about undefined
- // in the witness array.
- var witnesses = switchWitnesses.filter(function (witness) { return witness !== undefined; });
- // The adjusted clause start and end after removing the `default` statement.
- var fixedClauseStart = defaultCaseLocation < clauseStart ? clauseStart - 1 : clauseStart;
- var fixedClauseEnd = defaultCaseLocation < clauseEnd ? clauseEnd - 1 : clauseEnd;
- clauseWitnesses = witnesses.slice(fixedClauseStart, fixedClauseEnd);
- switchFacts = getFactsFromTypeofSwitch(fixedClauseStart, fixedClauseEnd, witnesses, hasDefaultClause);
+ else if (depth < 4 && isTypeReferenceWithGenericArguments(t)) {
+ result += "<" + getTypeReferenceId(t, typeParameters, depth + 1) + ">";
}
else {
- clauseWitnesses = switchWitnesses.slice(clauseStart, clauseEnd);
- switchFacts = getFactsFromTypeofSwitch(clauseStart, clauseEnd, switchWitnesses, hasDefaultClause);
- }
- if (hasDefaultClause) {
- return filterType(type, function (t) { return (getTypeFacts(t) & switchFacts) === switchFacts; });
- }
- /*
- The implied type is the raw type suggested by a
- value being caught in this clause.
-
- When the clause contains a default case we ignore
- the implied type and try to narrow using any facts
- we can learn: see `switchFacts`.
-
- Example:
- switch (typeof x) {
- case 'number':
- case 'string': break;
- default: break;
- case 'number':
- case 'boolean': break
- }
-
- In the first clause (case `number` and `string`) the
- implied type is number | string.
-
- In the default clause we de not compute an implied type.
-
- In the third clause (case `number` and `boolean`)
- the naive implied type is number | boolean, however
- we use the type facts to narrow the implied type to
- boolean. We know that number cannot be selected
- because it is caught in the first clause.
- */
- var impliedType = getTypeWithFacts(getUnionType(clauseWitnesses.map(function (text) { return getImpliedTypeFromTypeofCase(type, text); })), switchFacts);
- if (impliedType.flags & 1048576 /* Union */) {
- impliedType = getAssignmentReducedType(impliedType, getBaseConstraintOrType(type));
+ result += "-" + t.id;
}
- return getTypeWithFacts(mapType(type, narrowTypeForTypeofSwitch(impliedType)), switchFacts);
}
- function isMatchingConstructorReference(expr) {
- return (ts.isPropertyAccessExpression(expr) && ts.idText(expr.name) === "constructor" ||
- ts.isElementAccessExpression(expr) && ts.isStringLiteralLike(expr.argumentExpression) && expr.argumentExpression.text === "constructor") &&
- isMatchingReference(reference, expr.expression);
+ return result;
+ }
+ /**
+ * To improve caching, the relation key for two generic types uses the target's id plus ids of the type parameters.
+ * For other cases, the types ids are used.
+ */
+ function getRelationKey(source, target, intersectionState, relation) {
+ if (relation === identityRelation && source.id > target.id) {
+ var temp = source;
+ source = target;
+ target = temp;
}
- function narrowTypeByConstructor(type, operator, identifier, assumeTrue) {
- // Do not narrow when checking inequality.
- if (assumeTrue ? (operator !== 34 /* EqualsEqualsToken */ && operator !== 36 /* EqualsEqualsEqualsToken */) : (operator !== 35 /* ExclamationEqualsToken */ && operator !== 37 /* ExclamationEqualsEqualsToken */)) {
- return type;
+ var postFix = intersectionState ? ":" + intersectionState : "";
+ if (isTypeReferenceWithGenericArguments(source) && isTypeReferenceWithGenericArguments(target)) {
+ var typeParameters = [];
+ return getTypeReferenceId(source, typeParameters) + "," + getTypeReferenceId(target, typeParameters) + postFix;
+ }
+ return source.id + "," + target.id + postFix;
+ }
+ // Invoke the callback for each underlying property symbol of the given symbol and return the first
+ // value that isn't undefined.
+ function forEachProperty(prop, callback) {
+ if (ts.getCheckFlags(prop) & 6 /* Synthetic */) {
+ for (var _i = 0, _a = prop.containingType.types; _i < _a.length; _i++) {
+ var t = _a[_i];
+ var p = getPropertyOfType(t, prop.escapedName);
+ var result = p && forEachProperty(p, callback);
+ if (result) {
+ return result;
+ }
}
- // Get the type of the constructor identifier expression, if it is not a function then do not narrow.
- var identifierType = getTypeOfExpression(identifier);
- if (!isFunctionType(identifierType) && !isConstructorType(identifierType)) {
- return type;
+ return undefined;
+ }
+ return callback(prop);
+ }
+ // Return the declaring class type of a property or undefined if property not declared in class
+ function getDeclaringClass(prop) {
+ return prop.parent && prop.parent.flags & 32 /* Class */ ? getDeclaredTypeOfSymbol(getParentOfSymbol(prop)) : undefined;
+ }
+ // Return the inherited type of the given property or undefined if property doesn't exist in a base class.
+ function getTypeOfPropertyInBaseClass(property) {
+ var classType = getDeclaringClass(property);
+ var baseClassType = classType && getBaseTypes(classType)[0];
+ return baseClassType && getTypeOfPropertyOfType(baseClassType, property.escapedName);
+ }
+ // Return true if some underlying source property is declared in a class that derives
+ // from the given base class.
+ function isPropertyInClassDerivedFrom(prop, baseClass) {
+ return forEachProperty(prop, function (sp) {
+ var sourceClass = getDeclaringClass(sp);
+ return sourceClass ? hasBaseType(sourceClass, baseClass) : false;
+ });
+ }
+ // Return true if source property is a valid override of protected parts of target property.
+ function isValidOverrideOf(sourceProp, targetProp) {
+ return !forEachProperty(targetProp, function (tp) { return ts.getDeclarationModifierFlagsFromSymbol(tp) & 16 /* Protected */ ?
+ !isPropertyInClassDerivedFrom(sourceProp, getDeclaringClass(tp)) : false; });
+ }
+ // Return true if the given class derives from each of the declaring classes of the protected
+ // constituents of the given property.
+ function isClassDerivedFromDeclaringClasses(checkClass, prop) {
+ return forEachProperty(prop, function (p) { return ts.getDeclarationModifierFlagsFromSymbol(p) & 16 /* Protected */ ?
+ !hasBaseType(checkClass, getDeclaringClass(p)) : false; }) ? undefined : checkClass;
+ }
+ // Return true if the given type is deeply nested. We consider this to be the case when structural type comparisons
+ // for 5 or more occurrences or instantiations of the type have been recorded on the given stack. It is possible,
+ // though highly unlikely, for this test to be true in a situation where a chain of instantiations is not infinitely
+ // expanding. Effectively, we will generate a false positive when two types are structurally equal to at least 5
+ // levels, but unequal at some level beyond that.
+ // In addition, this will also detect when an indexed access has been chained off of 5 or more times (which is essentially
+ // the dual of the structural comparison), and likewise mark the type as deeply nested, potentially adding false positives
+ // for finite but deeply expanding indexed accesses (eg, for `Q[P1][P2][P3][P4][P5]`).
+ // It also detects when a recursive type reference has expanded 5 or more times, eg, if the true branch of
+ // `type A = null extends T ? [A>] : [T]`
+ // has expanded into `[A>>>>>]`
+ // in such cases we need to terminate the expansion, and we do so here.
+ function isDeeplyNestedType(type, stack, depth) {
+ if (depth >= 5) {
+ var identity_1 = getRecursionIdentity(type);
+ if (identity_1) {
+ var count = 0;
+ for (var i = 0; i < depth; i++) {
+ if (getRecursionIdentity(stack[i]) === identity_1) {
+ count++;
+ if (count >= 5) {
+ return true;
+ }
+ }
+ }
}
- // Get the prototype property of the type identifier so we can find out its type.
- var prototypeProperty = getPropertyOfType(identifierType, "prototype");
- if (!prototypeProperty) {
- return type;
+ }
+ return false;
+ }
+ // Types with constituents that could circularly reference the type have a recursion identity. The recursion
+ // identity is some object that is common to instantiations of the type with the same origin.
+ function getRecursionIdentity(type) {
+ if (type.flags & 524288 /* Object */ && !isObjectOrArrayLiteralType(type)) {
+ if (ts.getObjectFlags(type) && 4 /* Reference */ && type.node) {
+ // Deferred type references are tracked through their associated AST node. This gives us finer
+ // granularity than using their associated target because each manifest type reference has a
+ // unique AST node.
+ return type.node;
+ }
+ if (type.symbol && !(ts.getObjectFlags(type) & 16 /* Anonymous */ && type.symbol.flags & 32 /* Class */)) {
+ // We track all object types that have an associated symbol (representing the origin of the type), but
+ // exclude the static side of classes from this check since it shares its symbol with the instance side.
+ return type.symbol;
}
- // Get the type of the prototype, if it is undefined, or the global `Object` or `Function` types then do not narrow.
- var prototypeType = getTypeOfSymbol(prototypeProperty);
- var candidate = !isTypeAny(prototypeType) ? prototypeType : undefined;
- if (!candidate || candidate === globalObjectType || candidate === globalFunctionType) {
- return type;
+ if (isTupleType(type)) {
+ // Tuple types are tracked through their target type
+ return type.target;
}
- // If the type that is being narrowed is `any` then just return the `candidate` type since every type is a subtype of `any`.
- if (isTypeAny(type)) {
- return candidate;
+ }
+ if (type.flags & 8388608 /* IndexedAccess */) {
+ // Identity is the leftmost object type in a chain of indexed accesses, eg, in A[P][Q] it is A
+ do {
+ type = type.objectType;
+ } while (type.flags & 8388608 /* IndexedAccess */);
+ return type;
+ }
+ if (type.flags & 16777216 /* Conditional */) {
+ // The root object represents the origin of the conditional type
+ return type.root;
+ }
+ return undefined;
+ }
+ function isPropertyIdenticalTo(sourceProp, targetProp) {
+ return compareProperties(sourceProp, targetProp, compareTypesIdentical) !== 0 /* False */;
+ }
+ function compareProperties(sourceProp, targetProp, compareTypes) {
+ // Two members are considered identical when
+ // - they are public properties with identical names, optionality, and types,
+ // - they are private or protected properties originating in the same declaration and having identical types
+ if (sourceProp === targetProp) {
+ return -1 /* True */;
+ }
+ var sourcePropAccessibility = ts.getDeclarationModifierFlagsFromSymbol(sourceProp) & 24 /* NonPublicAccessibilityModifier */;
+ var targetPropAccessibility = ts.getDeclarationModifierFlagsFromSymbol(targetProp) & 24 /* NonPublicAccessibilityModifier */;
+ if (sourcePropAccessibility !== targetPropAccessibility) {
+ return 0 /* False */;
+ }
+ if (sourcePropAccessibility) {
+ if (getTargetSymbol(sourceProp) !== getTargetSymbol(targetProp)) {
+ return 0 /* False */;
}
- // Filter out types that are not considered to be "constructed by" the `candidate` type.
- return filterType(type, function (t) { return isConstructedBy(t, candidate); });
- function isConstructedBy(source, target) {
- // If either the source or target type are a class type then we need to check that they are the same exact type.
- // This is because you may have a class `A` that defines some set of properties, and another class `B`
- // that defines the same set of properties as class `A`, in that case they are structurally the same
- // type, but when you do something like `instanceOfA.constructor === B` it will return false.
- if (source.flags & 524288 /* Object */ && ts.getObjectFlags(source) & 1 /* Class */ ||
- target.flags & 524288 /* Object */ && ts.getObjectFlags(target) & 1 /* Class */) {
- return source.symbol === target.symbol;
- }
- // For all other types just check that the `source` type is a subtype of the `target` type.
- return isTypeSubtypeOf(source, target);
+ }
+ else {
+ if ((sourceProp.flags & 16777216 /* Optional */) !== (targetProp.flags & 16777216 /* Optional */)) {
+ return 0 /* False */;
}
}
- function narrowTypeByInstanceof(type, expr, assumeTrue) {
- var left = getReferenceCandidate(expr.left);
- if (!isMatchingReference(reference, left)) {
- if (assumeTrue && strictNullChecks && optionalChainContainsReference(left, reference)) {
- return getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */);
+ if (isReadonlySymbol(sourceProp) !== isReadonlySymbol(targetProp)) {
+ return 0 /* False */;
+ }
+ return compareTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp));
+ }
+ function isMatchingSignature(source, target, partialMatch) {
+ var sourceParameterCount = getParameterCount(source);
+ var targetParameterCount = getParameterCount(target);
+ var sourceMinArgumentCount = getMinArgumentCount(source);
+ var targetMinArgumentCount = getMinArgumentCount(target);
+ var sourceHasRestParameter = hasEffectiveRestParameter(source);
+ var targetHasRestParameter = hasEffectiveRestParameter(target);
+ // A source signature matches a target signature if the two signatures have the same number of required,
+ // optional, and rest parameters.
+ if (sourceParameterCount === targetParameterCount &&
+ sourceMinArgumentCount === targetMinArgumentCount &&
+ sourceHasRestParameter === targetHasRestParameter) {
+ return true;
+ }
+ // A source signature partially matches a target signature if the target signature has no fewer required
+ // parameters
+ if (partialMatch && sourceMinArgumentCount <= targetMinArgumentCount) {
+ return true;
+ }
+ return false;
+ }
+ /**
+ * See signatureRelatedTo, compareSignaturesIdentical
+ */
+ function compareSignaturesIdentical(source, target, partialMatch, ignoreThisTypes, ignoreReturnTypes, compareTypes) {
+ // TODO (drosen): De-duplicate code between related functions.
+ if (source === target) {
+ return -1 /* True */;
+ }
+ if (!(isMatchingSignature(source, target, partialMatch))) {
+ return 0 /* False */;
+ }
+ // Check that the two signatures have the same number of type parameters.
+ if (ts.length(source.typeParameters) !== ts.length(target.typeParameters)) {
+ return 0 /* False */;
+ }
+ // Check that type parameter constraints and defaults match. If they do, instantiate the source
+ // signature with the type parameters of the target signature and continue the comparison.
+ if (target.typeParameters) {
+ var mapper = createTypeMapper(source.typeParameters, target.typeParameters);
+ for (var i = 0; i < target.typeParameters.length; i++) {
+ var s = source.typeParameters[i];
+ var t = target.typeParameters[i];
+ if (!(s === t || compareTypes(instantiateType(getConstraintFromTypeParameter(s), mapper) || unknownType, getConstraintFromTypeParameter(t) || unknownType) &&
+ compareTypes(instantiateType(getDefaultFromTypeParameter(s), mapper) || unknownType, getDefaultFromTypeParameter(t) || unknownType))) {
+ return 0 /* False */;
}
- return type;
}
- // Check that right operand is a function type with a prototype property
- var rightType = getTypeOfExpression(expr.right);
- if (!isTypeDerivedFrom(rightType, globalFunctionType)) {
- return type;
- }
- var targetType;
- var prototypeProperty = getPropertyOfType(rightType, "prototype");
- if (prototypeProperty) {
- // Target type is type of the prototype property
- var prototypePropertyType = getTypeOfSymbol(prototypeProperty);
- if (!isTypeAny(prototypePropertyType)) {
- targetType = prototypePropertyType;
+ source = instantiateSignature(source, mapper, /*eraseTypeParameters*/ true);
+ }
+ var result = -1 /* True */;
+ if (!ignoreThisTypes) {
+ var sourceThisType = getThisTypeOfSignature(source);
+ if (sourceThisType) {
+ var targetThisType = getThisTypeOfSignature(target);
+ if (targetThisType) {
+ var related = compareTypes(sourceThisType, targetThisType);
+ if (!related) {
+ return 0 /* False */;
+ }
+ result &= related;
}
}
- // Don't narrow from 'any' if the target type is exactly 'Object' or 'Function'
- if (isTypeAny(type) && (targetType === globalObjectType || targetType === globalFunctionType)) {
- return type;
- }
- if (!targetType) {
- var constructSignatures = getSignaturesOfType(rightType, 1 /* Construct */);
- targetType = constructSignatures.length ?
- getUnionType(ts.map(constructSignatures, function (signature) { return getReturnTypeOfSignature(getErasedSignature(signature)); })) :
- emptyObjectType;
+ }
+ var targetLen = getParameterCount(target);
+ for (var i = 0; i < targetLen; i++) {
+ var s = getTypeAtPosition(source, i);
+ var t = getTypeAtPosition(target, i);
+ var related = compareTypes(t, s);
+ if (!related) {
+ return 0 /* False */;
}
- return getNarrowedType(type, targetType, assumeTrue, isTypeDerivedFrom);
+ result &= related;
}
- function getNarrowedType(type, candidate, assumeTrue, isRelated) {
- if (!assumeTrue) {
- return filterType(type, function (t) { return !isRelated(t, candidate); });
+ if (!ignoreReturnTypes) {
+ var sourceTypePredicate = getTypePredicateOfSignature(source);
+ var targetTypePredicate = getTypePredicateOfSignature(target);
+ result &= sourceTypePredicate || targetTypePredicate ?
+ compareTypePredicatesIdentical(sourceTypePredicate, targetTypePredicate, compareTypes) :
+ compareTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target));
+ }
+ return result;
+ }
+ function compareTypePredicatesIdentical(source, target, compareTypes) {
+ return !(source && target && typePredicateKindsMatch(source, target)) ? 0 /* False */ :
+ source.type === target.type ? -1 /* True */ :
+ source.type && target.type ? compareTypes(source.type, target.type) :
+ 0 /* False */;
+ }
+ function literalTypesWithSameBaseType(types) {
+ var commonBaseType;
+ for (var _i = 0, types_13 = types; _i < types_13.length; _i++) {
+ var t = types_13[_i];
+ var baseType = getBaseTypeOfLiteralType(t);
+ if (!commonBaseType) {
+ commonBaseType = baseType;
}
- // If the current type is a union type, remove all constituents that couldn't be instances of
- // the candidate type. If one or more constituents remain, return a union of those.
- if (type.flags & 1048576 /* Union */) {
- var assignableType = filterType(type, function (t) { return isRelated(t, candidate); });
- if (!(assignableType.flags & 131072 /* Never */)) {
- return assignableType;
- }
+ if (baseType === t || baseType !== commonBaseType) {
+ return false;
}
- // If the candidate type is a subtype of the target type, narrow to the candidate type.
- // Otherwise, if the target type is assignable to the candidate type, keep the target type.
- // Otherwise, if the candidate type is assignable to the target type, narrow to the candidate
- // type. Otherwise, the types are completely unrelated, so narrow to an intersection of the
- // two types.
- return isTypeSubtypeOf(candidate, type) ? candidate :
- isTypeAssignableTo(type, candidate) ? type :
- isTypeAssignableTo(candidate, type) ? candidate :
- getIntersectionType([type, candidate]);
}
- function narrowTypeByCallExpression(type, callExpression, assumeTrue) {
- if (hasMatchingArgument(callExpression, reference)) {
- var signature = assumeTrue || !ts.isCallChain(callExpression) ? getEffectsSignature(callExpression) : undefined;
- var predicate = signature && getTypePredicateOfSignature(signature);
- if (predicate && (predicate.kind === 0 /* This */ || predicate.kind === 1 /* Identifier */)) {
- return narrowTypeByTypePredicate(type, predicate, callExpression, assumeTrue);
- }
- }
- return type;
+ return true;
+ }
+ // When the candidate types are all literal types with the same base type, return a union
+ // of those literal types. Otherwise, return the leftmost type for which no type to the
+ // right is a supertype.
+ function getSupertypeOrUnion(types) {
+ return literalTypesWithSameBaseType(types) ?
+ getUnionType(types) :
+ ts.reduceLeft(types, function (s, t) { return isTypeSubtypeOf(s, t) ? t : s; });
+ }
+ function getCommonSupertype(types) {
+ if (!strictNullChecks) {
+ return getSupertypeOrUnion(types);
+ }
+ var primaryTypes = ts.filter(types, function (t) { return !(t.flags & 98304 /* Nullable */); });
+ return primaryTypes.length ?
+ getNullableType(getSupertypeOrUnion(primaryTypes), getFalsyFlagsOfTypes(types) & 98304 /* Nullable */) :
+ getUnionType(types, 2 /* Subtype */);
+ }
+ // Return the leftmost type for which no type to the right is a subtype.
+ function getCommonSubtype(types) {
+ return ts.reduceLeft(types, function (s, t) { return isTypeSubtypeOf(t, s) ? t : s; });
+ }
+ function isArrayType(type) {
+ return !!(ts.getObjectFlags(type) & 4 /* Reference */) && (type.target === globalArrayType || type.target === globalReadonlyArrayType);
+ }
+ function isReadonlyArrayType(type) {
+ return !!(ts.getObjectFlags(type) & 4 /* Reference */) && type.target === globalReadonlyArrayType;
+ }
+ function isMutableArrayOrTuple(type) {
+ return isArrayType(type) && !isReadonlyArrayType(type) || isTupleType(type) && !type.target.readonly;
+ }
+ function getElementTypeOfArrayType(type) {
+ return isArrayType(type) ? getTypeArguments(type)[0] : undefined;
+ }
+ function isArrayLikeType(type) {
+ // A type is array-like if it is a reference to the global Array or global ReadonlyArray type,
+ // or if it is not the undefined or null type and if it is assignable to ReadonlyArray
+ return isArrayType(type) || !(type.flags & 98304 /* Nullable */) && isTypeAssignableTo(type, anyReadonlyArrayType);
+ }
+ function isEmptyArrayLiteralType(type) {
+ var elementType = isArrayType(type) ? getTypeArguments(type)[0] : undefined;
+ return elementType === undefinedWideningType || elementType === implicitNeverType;
+ }
+ function isTupleLikeType(type) {
+ return isTupleType(type) || !!getPropertyOfType(type, "0");
+ }
+ function isArrayOrTupleLikeType(type) {
+ return isArrayLikeType(type) || isTupleLikeType(type);
+ }
+ function getTupleElementType(type, index) {
+ var propType = getTypeOfPropertyOfType(type, "" + index);
+ if (propType) {
+ return propType;
+ }
+ if (everyType(type, isTupleType)) {
+ return mapType(type, function (t) { return getRestTypeOfTupleType(t) || undefinedType; });
+ }
+ return undefined;
+ }
+ function isNeitherUnitTypeNorNever(type) {
+ return !(type.flags & (109440 /* Unit */ | 131072 /* Never */));
+ }
+ function isUnitType(type) {
+ return !!(type.flags & 109440 /* Unit */);
+ }
+ function isLiteralType(type) {
+ return type.flags & 16 /* Boolean */ ? true :
+ type.flags & 1048576 /* Union */ ? type.flags & 1024 /* EnumLiteral */ ? true : ts.every(type.types, isUnitType) :
+ isUnitType(type);
+ }
+ function getBaseTypeOfLiteralType(type) {
+ return type.flags & 1024 /* EnumLiteral */ ? getBaseTypeOfEnumLiteralType(type) :
+ type.flags & 128 /* StringLiteral */ ? stringType :
+ type.flags & 256 /* NumberLiteral */ ? numberType :
+ type.flags & 2048 /* BigIntLiteral */ ? bigintType :
+ type.flags & 512 /* BooleanLiteral */ ? booleanType :
+ type.flags & 1048576 /* Union */ ? getUnionType(ts.sameMap(type.types, getBaseTypeOfLiteralType)) :
+ type;
+ }
+ function getWidenedLiteralType(type) {
+ return type.flags & 1024 /* EnumLiteral */ && isFreshLiteralType(type) ? getBaseTypeOfEnumLiteralType(type) :
+ type.flags & 128 /* StringLiteral */ && isFreshLiteralType(type) ? stringType :
+ type.flags & 256 /* NumberLiteral */ && isFreshLiteralType(type) ? numberType :
+ type.flags & 2048 /* BigIntLiteral */ && isFreshLiteralType(type) ? bigintType :
+ type.flags & 512 /* BooleanLiteral */ && isFreshLiteralType(type) ? booleanType :
+ type.flags & 1048576 /* Union */ ? getUnionType(ts.sameMap(type.types, getWidenedLiteralType)) :
+ type;
+ }
+ function getWidenedUniqueESSymbolType(type) {
+ return type.flags & 8192 /* UniqueESSymbol */ ? esSymbolType :
+ type.flags & 1048576 /* Union */ ? getUnionType(ts.sameMap(type.types, getWidenedUniqueESSymbolType)) :
+ type;
+ }
+ function getWidenedLiteralLikeTypeForContextualType(type, contextualType) {
+ if (!isLiteralOfContextualType(type, contextualType)) {
+ type = getWidenedUniqueESSymbolType(getWidenedLiteralType(type));
}
- function narrowTypeByTypePredicate(type, predicate, callExpression, assumeTrue) {
- // Don't narrow from 'any' if the predicate type is exactly 'Object' or 'Function'
- if (predicate.type && !(isTypeAny(type) && (predicate.type === globalObjectType || predicate.type === globalFunctionType))) {
- var predicateArgument = getTypePredicateArgument(predicate, callExpression);
- if (predicateArgument) {
- if (isMatchingReference(reference, predicateArgument)) {
- return getNarrowedType(type, predicate.type, assumeTrue, isTypeSubtypeOf);
- }
- if (strictNullChecks && assumeTrue && optionalChainContainsReference(predicateArgument, reference) &&
- !(getTypeFacts(predicate.type) & 65536 /* EQUndefined */)) {
- type = getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */);
- }
- if (isMatchingReferenceDiscriminant(predicateArgument, declaredType)) {
- return narrowTypeByDiscriminant(type, predicateArgument, function (t) { return getNarrowedType(t, predicate.type, assumeTrue, isTypeSubtypeOf); });
- }
- }
- }
- return type;
+ return type;
+ }
+ function getWidenedLiteralLikeTypeForContextualReturnTypeIfNeeded(type, contextualSignatureReturnType, isAsync) {
+ if (type && isUnitType(type)) {
+ var contextualType = !contextualSignatureReturnType ? undefined :
+ isAsync ? getPromisedTypeOfPromise(contextualSignatureReturnType) :
+ contextualSignatureReturnType;
+ type = getWidenedLiteralLikeTypeForContextualType(type, contextualType);
}
- // Narrow the given type based on the given expression having the assumed boolean value. The returned type
- // will be a subtype or the same type as the argument.
- function narrowType(type, expr, assumeTrue) {
- // for `a?.b`, we emulate a synthetic `a !== null && a !== undefined` condition for `a`
- if (ts.isExpressionOfOptionalChainRoot(expr) ||
- ts.isBinaryExpression(expr.parent) && expr.parent.operatorToken.kind === 60 /* QuestionQuestionToken */ && expr.parent.left === expr) {
- return narrowTypeByOptionality(type, expr, assumeTrue);
- }
- switch (expr.kind) {
- case 75 /* Identifier */:
- case 104 /* ThisKeyword */:
- case 102 /* SuperKeyword */:
- case 194 /* PropertyAccessExpression */:
- case 195 /* ElementAccessExpression */:
- return narrowTypeByTruthiness(type, expr, assumeTrue);
- case 196 /* CallExpression */:
- return narrowTypeByCallExpression(type, expr, assumeTrue);
- case 200 /* ParenthesizedExpression */:
- return narrowType(type, expr.expression, assumeTrue);
- case 209 /* BinaryExpression */:
- return narrowTypeByBinaryExpression(type, expr, assumeTrue);
- case 207 /* PrefixUnaryExpression */:
- if (expr.operator === 53 /* ExclamationToken */) {
- return narrowType(type, expr.operand, !assumeTrue);
- }
- break;
- }
- return type;
+ return type;
+ }
+ function getWidenedLiteralLikeTypeForContextualIterationTypeIfNeeded(type, contextualSignatureReturnType, kind, isAsyncGenerator) {
+ if (type && isUnitType(type)) {
+ var contextualType = !contextualSignatureReturnType ? undefined :
+ getIterationTypeOfGeneratorFunctionReturnType(kind, contextualSignatureReturnType, isAsyncGenerator);
+ type = getWidenedLiteralLikeTypeForContextualType(type, contextualType);
}
- function narrowTypeByOptionality(type, expr, assumePresent) {
- if (isMatchingReference(reference, expr)) {
- return getTypeWithFacts(type, assumePresent ? 2097152 /* NEUndefinedOrNull */ : 262144 /* EQUndefinedOrNull */);
- }
- if (isMatchingReferenceDiscriminant(expr, declaredType)) {
- return narrowTypeByDiscriminant(type, expr, function (t) { return getTypeWithFacts(t, assumePresent ? 2097152 /* NEUndefinedOrNull */ : 262144 /* EQUndefinedOrNull */); });
+ return type;
+ }
+ /**
+ * Check if a Type was written as a tuple type literal.
+ * Prefer using isTupleLikeType() unless the use of `elementTypes`/`getTypeArguments` is required.
+ */
+ function isTupleType(type) {
+ return !!(ts.getObjectFlags(type) & 4 /* Reference */ && type.target.objectFlags & 8 /* Tuple */);
+ }
+ function isGenericTupleType(type) {
+ return isTupleType(type) && !!(type.target.combinedFlags & 8 /* Variadic */);
+ }
+ function isSingleElementGenericTupleType(type) {
+ return isGenericTupleType(type) && type.target.elementFlags.length === 1;
+ }
+ function getRestTypeOfTupleType(type) {
+ return getElementTypeOfSliceOfTupleType(type, type.target.fixedLength);
+ }
+ function getRestArrayTypeOfTupleType(type) {
+ var restType = getRestTypeOfTupleType(type);
+ return restType && createArrayType(restType);
+ }
+ function getEndLengthOfType(type) {
+ return isTupleType(type) ? getTypeReferenceArity(type) - ts.findLastIndex(type.target.elementFlags, function (f) { return !(f & (1 /* Required */ | 2 /* Optional */)); }) - 1 : 0;
+ }
+ function getElementTypeOfSliceOfTupleType(type, index, endSkipCount, writing) {
+ if (endSkipCount === void 0) { endSkipCount = 0; }
+ if (writing === void 0) { writing = false; }
+ var length = getTypeReferenceArity(type) - endSkipCount;
+ if (index < length) {
+ var typeArguments = getTypeArguments(type);
+ var elementTypes = [];
+ for (var i = index; i < length; i++) {
+ var t = typeArguments[i];
+ elementTypes.push(type.target.elementFlags[i] & 8 /* Variadic */ ? getIndexedAccessType(t, numberType) : t);
}
- return type;
+ return writing ? getIntersectionType(elementTypes) : getUnionType(elementTypes);
}
+ return undefined;
}
- function getTypeOfSymbolAtLocation(symbol, location) {
- symbol = symbol.exportSymbol || symbol;
- // If we have an identifier or a property access at the given location, if the location is
- // an dotted name expression, and if the location is not an assignment target, obtain the type
- // of the expression (which will reflect control flow analysis). If the expression indeed
- // resolved to the given symbol, return the narrowed type.
- if (location.kind === 75 /* Identifier */) {
- if (ts.isRightSideOfQualifiedNameOrPropertyAccess(location)) {
- location = location.parent;
- }
- if (ts.isExpressionNode(location) && !ts.isAssignmentTarget(location)) {
- var type = getTypeOfExpression(location);
- if (getExportSymbolOfValueSymbolIfExported(getNodeLinks(location).resolvedSymbol) === symbol) {
- return type;
- }
- }
+ function isTupleTypeStructureMatching(t1, t2) {
+ return getTypeReferenceArity(t1) === getTypeReferenceArity(t2) &&
+ ts.every(t1.target.elementFlags, function (f, i) { return (f & 12 /* Variable */) === (t2.target.elementFlags[i] & 12 /* Variable */); });
+ }
+ function isZeroBigInt(_a) {
+ var value = _a.value;
+ return value.base10Value === "0";
+ }
+ function getFalsyFlagsOfTypes(types) {
+ var result = 0;
+ for (var _i = 0, types_14 = types; _i < types_14.length; _i++) {
+ var t = types_14[_i];
+ result |= getFalsyFlags(t);
}
- // The location isn't a reference to the given symbol, meaning we're being asked
- // a hypothetical question of what type the symbol would have if there was a reference
- // to it at the given location. Since we have no control flow information for the
- // hypothetical reference (control flow information is created and attached by the
- // binder), we simply return the declared type of the symbol.
- return getTypeOfSymbol(symbol);
+ return result;
}
- function getControlFlowContainer(node) {
- return ts.findAncestor(node.parent, function (node) {
- return ts.isFunctionLike(node) && !ts.getImmediatelyInvokedFunctionExpression(node) ||
- node.kind === 250 /* ModuleBlock */ ||
- node.kind === 290 /* SourceFile */ ||
- node.kind === 159 /* PropertyDeclaration */;
- });
+ // Returns the String, Number, Boolean, StringLiteral, NumberLiteral, BooleanLiteral, Void, Undefined, or Null
+ // flags for the string, number, boolean, "", 0, false, void, undefined, or null types respectively. Returns
+ // no flags for all other types (including non-falsy literal types).
+ function getFalsyFlags(type) {
+ return type.flags & 1048576 /* Union */ ? getFalsyFlagsOfTypes(type.types) :
+ type.flags & 128 /* StringLiteral */ ? type.value === "" ? 128 /* StringLiteral */ : 0 :
+ type.flags & 256 /* NumberLiteral */ ? type.value === 0 ? 256 /* NumberLiteral */ : 0 :
+ type.flags & 2048 /* BigIntLiteral */ ? isZeroBigInt(type) ? 2048 /* BigIntLiteral */ : 0 :
+ type.flags & 512 /* BooleanLiteral */ ? (type === falseType || type === regularFalseType) ? 512 /* BooleanLiteral */ : 0 :
+ type.flags & 117724 /* PossiblyFalsy */;
}
- // Check if a parameter is assigned anywhere within its declaring function.
- function isParameterAssigned(symbol) {
- var func = ts.getRootDeclaration(symbol.valueDeclaration).parent;
- var links = getNodeLinks(func);
- if (!(links.flags & 8388608 /* AssignmentsMarked */)) {
- links.flags |= 8388608 /* AssignmentsMarked */;
- if (!hasParentWithAssignmentsMarked(func)) {
- markParameterAssignments(func);
- }
+ function removeDefinitelyFalsyTypes(type) {
+ return getFalsyFlags(type) & 117632 /* DefinitelyFalsy */ ?
+ filterType(type, function (t) { return !(getFalsyFlags(t) & 117632 /* DefinitelyFalsy */); }) :
+ type;
+ }
+ function extractDefinitelyFalsyTypes(type) {
+ return mapType(type, getDefinitelyFalsyPartOfType);
+ }
+ function getDefinitelyFalsyPartOfType(type) {
+ return type.flags & 4 /* String */ ? emptyStringType :
+ type.flags & 8 /* Number */ ? zeroType :
+ type.flags & 64 /* BigInt */ ? zeroBigIntType :
+ type === regularFalseType ||
+ type === falseType ||
+ type.flags & (16384 /* Void */ | 32768 /* Undefined */ | 65536 /* Null */ | 3 /* AnyOrUnknown */) ||
+ type.flags & 128 /* StringLiteral */ && type.value === "" ||
+ type.flags & 256 /* NumberLiteral */ && type.value === 0 ||
+ type.flags & 2048 /* BigIntLiteral */ && isZeroBigInt(type) ? type :
+ neverType;
+ }
+ /**
+ * Add undefined or null or both to a type if they are missing.
+ * @param type - type to add undefined and/or null to if not present
+ * @param flags - Either TypeFlags.Undefined or TypeFlags.Null, or both
+ */
+ function getNullableType(type, flags) {
+ var missing = (flags & ~type.flags) & (32768 /* Undefined */ | 65536 /* Null */);
+ return missing === 0 ? type :
+ missing === 32768 /* Undefined */ ? getUnionType([type, undefinedType]) :
+ missing === 65536 /* Null */ ? getUnionType([type, nullType]) :
+ getUnionType([type, undefinedType, nullType]);
+ }
+ function getOptionalType(type) {
+ ts.Debug.assert(strictNullChecks);
+ return type.flags & 32768 /* Undefined */ ? type : getUnionType([type, undefinedType]);
+ }
+ function getGlobalNonNullableTypeInstantiation(type) {
+ if (!deferredGlobalNonNullableTypeAlias) {
+ deferredGlobalNonNullableTypeAlias = getGlobalSymbol("NonNullable", 524288 /* TypeAlias */, /*diagnostic*/ undefined) || unknownSymbol;
}
- return symbol.isAssigned || false;
+ // Use NonNullable global type alias if available to improve quick info/declaration emit
+ if (deferredGlobalNonNullableTypeAlias !== unknownSymbol) {
+ return getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [type]);
+ }
+ return getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */); // Type alias unavailable, fall back to non-higher-order behavior
}
- function hasParentWithAssignmentsMarked(node) {
- return !!ts.findAncestor(node.parent, function (node) { return ts.isFunctionLike(node) && !!(getNodeLinks(node).flags & 8388608 /* AssignmentsMarked */); });
+ function getNonNullableType(type) {
+ return strictNullChecks ? getGlobalNonNullableTypeInstantiation(type) : type;
}
- function markParameterAssignments(node) {
- if (node.kind === 75 /* Identifier */) {
- if (ts.isAssignmentTarget(node)) {
- var symbol = getResolvedSymbol(node);
- if (symbol.valueDeclaration && ts.getRootDeclaration(symbol.valueDeclaration).kind === 156 /* Parameter */) {
- symbol.isAssigned = true;
- }
- }
+ function addOptionalTypeMarker(type) {
+ return strictNullChecks ? getUnionType([type, optionalType]) : type;
+ }
+ function isNotOptionalTypeMarker(type) {
+ return type !== optionalType;
+ }
+ function removeOptionalTypeMarker(type) {
+ return strictNullChecks ? filterType(type, isNotOptionalTypeMarker) : type;
+ }
+ function propagateOptionalTypeMarker(type, node, wasOptional) {
+ return wasOptional ? ts.isOutermostOptionalChain(node) ? getOptionalType(type) : addOptionalTypeMarker(type) : type;
+ }
+ function getOptionalExpressionType(exprType, expression) {
+ return ts.isExpressionOfOptionalChainRoot(expression) ? getNonNullableType(exprType) :
+ ts.isOptionalChain(expression) ? removeOptionalTypeMarker(exprType) :
+ exprType;
+ }
+ /**
+ * Is source potentially coercible to target type under `==`.
+ * Assumes that `source` is a constituent of a union, hence
+ * the boolean literal flag on the LHS, but not on the RHS.
+ *
+ * This does not fully replicate the semantics of `==`. The
+ * intention is to catch cases that are clearly not right.
+ *
+ * Comparing (string | number) to number should not remove the
+ * string element.
+ *
+ * Comparing (string | number) to 1 will remove the string
+ * element, though this is not sound. This is a pragmatic
+ * choice.
+ *
+ * @see narrowTypeByEquality
+ *
+ * @param source
+ * @param target
+ */
+ function isCoercibleUnderDoubleEquals(source, target) {
+ return ((source.flags & (8 /* Number */ | 4 /* String */ | 512 /* BooleanLiteral */)) !== 0)
+ && ((target.flags & (8 /* Number */ | 4 /* String */ | 16 /* Boolean */)) !== 0);
+ }
+ /**
+ * Return true if type was inferred from an object literal, written as an object type literal, or is the shape of a module
+ * with no call or construct signatures.
+ */
+ function isObjectTypeWithInferableIndex(type) {
+ return type.flags & 2097152 /* Intersection */ ? ts.every(type.types, isObjectTypeWithInferableIndex) :
+ !!(type.symbol && (type.symbol.flags & (4096 /* ObjectLiteral */ | 2048 /* TypeLiteral */ | 384 /* Enum */ | 512 /* ValueModule */)) !== 0 &&
+ !typeHasCallOrConstructSignatures(type)) || !!(ts.getObjectFlags(type) & 2048 /* ReverseMapped */ && isObjectTypeWithInferableIndex(type.source));
+ }
+ function createSymbolWithType(source, type) {
+ var symbol = createSymbol(source.flags, source.escapedName, ts.getCheckFlags(source) & 8 /* Readonly */);
+ symbol.declarations = source.declarations;
+ symbol.parent = source.parent;
+ symbol.type = type;
+ symbol.target = source;
+ if (source.valueDeclaration) {
+ symbol.valueDeclaration = source.valueDeclaration;
}
- else {
- ts.forEachChild(node, markParameterAssignments);
+ var nameType = getSymbolLinks(source).nameType;
+ if (nameType) {
+ symbol.nameType = nameType;
}
+ return symbol;
}
- function isConstVariable(symbol) {
- return symbol.flags & 3 /* Variable */ && (getDeclarationNodeFlagsFromSymbol(symbol) & 2 /* Const */) !== 0 && getTypeOfSymbol(symbol) !== autoArrayType;
+ function transformTypeOfMembers(type, f) {
+ var members = ts.createSymbolTable();
+ for (var _i = 0, _a = getPropertiesOfObjectType(type); _i < _a.length; _i++) {
+ var property = _a[_i];
+ var original = getTypeOfSymbol(property);
+ var updated = f(original);
+ members.set(property.escapedName, updated === original ? property : createSymbolWithType(property, updated));
+ }
+ return members;
}
- /** remove undefined from the annotated type of a parameter when there is an initializer (that doesn't include undefined) */
- function removeOptionalityFromDeclaredType(declaredType, declaration) {
- if (pushTypeResolution(declaration.symbol, 2 /* DeclaredType */)) {
- var annotationIncludesUndefined = strictNullChecks &&
- declaration.kind === 156 /* Parameter */ &&
- declaration.initializer &&
- getFalsyFlags(declaredType) & 32768 /* Undefined */ &&
- !(getFalsyFlags(checkExpression(declaration.initializer)) & 32768 /* Undefined */);
- popTypeResolution();
- return annotationIncludesUndefined ? getTypeWithFacts(declaredType, 524288 /* NEUndefined */) : declaredType;
+ /**
+ * If the the provided object literal is subject to the excess properties check,
+ * create a new that is exempt. Recursively mark object literal members as exempt.
+ * Leave signatures alone since they are not subject to the check.
+ */
+ function getRegularTypeOfObjectLiteral(type) {
+ if (!(isObjectLiteralType(type) && ts.getObjectFlags(type) & 32768 /* FreshLiteral */)) {
+ return type;
}
- else {
- reportCircularityError(declaration.symbol);
- return declaredType;
+ var regularType = type.regularType;
+ if (regularType) {
+ return regularType;
}
+ var resolved = type;
+ var members = transformTypeOfMembers(type, getRegularTypeOfObjectLiteral);
+ var regularNew = createAnonymousType(resolved.symbol, members, resolved.callSignatures, resolved.constructSignatures, resolved.stringIndexInfo, resolved.numberIndexInfo);
+ regularNew.flags = resolved.flags;
+ regularNew.objectFlags |= resolved.objectFlags & ~32768 /* FreshLiteral */;
+ type.regularType = regularNew;
+ return regularNew;
}
- function isConstraintPosition(node) {
- var parent = node.parent;
- return parent.kind === 194 /* PropertyAccessExpression */ ||
- parent.kind === 196 /* CallExpression */ && parent.expression === node ||
- parent.kind === 195 /* ElementAccessExpression */ && parent.expression === node ||
- parent.kind === 191 /* BindingElement */ && parent.name === node && !!parent.initializer;
+ function createWideningContext(parent, propertyName, siblings) {
+ return { parent: parent, propertyName: propertyName, siblings: siblings, resolvedProperties: undefined };
}
- function typeHasNullableConstraint(type) {
- return type.flags & 58982400 /* InstantiableNonPrimitive */ && maybeTypeOfKind(getBaseConstraintOfType(type) || unknownType, 98304 /* Nullable */);
+ function getSiblingsOfContext(context) {
+ if (!context.siblings) {
+ var siblings_1 = [];
+ for (var _i = 0, _a = getSiblingsOfContext(context.parent); _i < _a.length; _i++) {
+ var type = _a[_i];
+ if (isObjectLiteralType(type)) {
+ var prop = getPropertyOfObjectType(type, context.propertyName);
+ if (prop) {
+ forEachType(getTypeOfSymbol(prop), function (t) {
+ siblings_1.push(t);
+ });
+ }
+ }
+ }
+ context.siblings = siblings_1;
+ }
+ return context.siblings;
}
- function getConstraintForLocation(type, node) {
- // When a node is the left hand expression of a property access, element access, or call expression,
- // and the type of the node includes type variables with constraints that are nullable, we fetch the
- // apparent type of the node *before* performing control flow analysis such that narrowings apply to
- // the constraint type.
- if (type && isConstraintPosition(node) && forEachType(type, typeHasNullableConstraint)) {
- return mapType(getWidenedType(type), getBaseConstraintOrType);
+ function getPropertiesOfContext(context) {
+ if (!context.resolvedProperties) {
+ var names = new ts.Map();
+ for (var _i = 0, _a = getSiblingsOfContext(context); _i < _a.length; _i++) {
+ var t = _a[_i];
+ if (isObjectLiteralType(t) && !(ts.getObjectFlags(t) & 1024 /* ContainsSpread */)) {
+ for (var _b = 0, _c = getPropertiesOfType(t); _b < _c.length; _b++) {
+ var prop = _c[_b];
+ names.set(prop.escapedName, prop);
+ }
+ }
+ }
+ context.resolvedProperties = ts.arrayFrom(names.values());
}
- return type;
+ return context.resolvedProperties;
}
- function isExportOrExportExpression(location) {
- return !!ts.findAncestor(location, function (e) { return e.parent && ts.isExportAssignment(e.parent) && e.parent.expression === e && ts.isEntityNameExpression(e); });
+ function getWidenedProperty(prop, context) {
+ if (!(prop.flags & 4 /* Property */)) {
+ // Since get accessors already widen their return value there is no need to
+ // widen accessor based properties here.
+ return prop;
+ }
+ var original = getTypeOfSymbol(prop);
+ var propContext = context && createWideningContext(context, prop.escapedName, /*siblings*/ undefined);
+ var widened = getWidenedTypeWithContext(original, propContext);
+ return widened === original ? prop : createSymbolWithType(prop, widened);
}
- function markAliasReferenced(symbol, location) {
- if (isNonLocalAlias(symbol, /*excludes*/ 111551 /* Value */) && !isInTypeQuery(location) && !getTypeOnlyAliasDeclaration(symbol)) {
- if (compilerOptions.preserveConstEnums && isExportOrExportExpression(location) || !isConstEnumOrConstEnumOnlyModule(resolveAlias(symbol))) {
- markAliasSymbolAsReferenced(symbol);
- }
- else {
- markConstEnumAliasAsReferenced(symbol);
+ function getUndefinedProperty(prop) {
+ var cached = undefinedProperties.get(prop.escapedName);
+ if (cached) {
+ return cached;
+ }
+ var result = createSymbolWithType(prop, undefinedType);
+ result.flags |= 16777216 /* Optional */;
+ undefinedProperties.set(prop.escapedName, result);
+ return result;
+ }
+ function getWidenedTypeOfObjectLiteral(type, context) {
+ var members = ts.createSymbolTable();
+ for (var _i = 0, _a = getPropertiesOfObjectType(type); _i < _a.length; _i++) {
+ var prop = _a[_i];
+ members.set(prop.escapedName, getWidenedProperty(prop, context));
+ }
+ if (context) {
+ for (var _b = 0, _c = getPropertiesOfContext(context); _b < _c.length; _b++) {
+ var prop = _c[_b];
+ if (!members.has(prop.escapedName)) {
+ members.set(prop.escapedName, getUndefinedProperty(prop));
+ }
}
}
+ var stringIndexInfo = getIndexInfoOfType(type, 0 /* String */);
+ var numberIndexInfo = getIndexInfoOfType(type, 1 /* Number */);
+ var result = createAnonymousType(type.symbol, members, ts.emptyArray, ts.emptyArray, stringIndexInfo && createIndexInfo(getWidenedType(stringIndexInfo.type), stringIndexInfo.isReadonly), numberIndexInfo && createIndexInfo(getWidenedType(numberIndexInfo.type), numberIndexInfo.isReadonly));
+ result.objectFlags |= (ts.getObjectFlags(type) & (16384 /* JSLiteral */ | 2097152 /* NonInferrableType */)); // Retain js literal flag through widening
+ return result;
}
- function checkIdentifier(node) {
- var symbol = getResolvedSymbol(node);
- if (symbol === unknownSymbol) {
- return errorType;
+ function getWidenedType(type) {
+ return getWidenedTypeWithContext(type, /*context*/ undefined);
+ }
+ function getWidenedTypeWithContext(type, context) {
+ if (ts.getObjectFlags(type) & 1572864 /* RequiresWidening */) {
+ if (context === undefined && type.widened) {
+ return type.widened;
+ }
+ var result = void 0;
+ if (type.flags & (1 /* Any */ | 98304 /* Nullable */)) {
+ result = anyType;
+ }
+ else if (isObjectLiteralType(type)) {
+ result = getWidenedTypeOfObjectLiteral(type, context);
+ }
+ else if (type.flags & 1048576 /* Union */) {
+ var unionContext_1 = context || createWideningContext(/*parent*/ undefined, /*propertyName*/ undefined, type.types);
+ var widenedTypes = ts.sameMap(type.types, function (t) { return t.flags & 98304 /* Nullable */ ? t : getWidenedTypeWithContext(t, unionContext_1); });
+ // Widening an empty object literal transitions from a highly restrictive type to
+ // a highly inclusive one. For that reason we perform subtype reduction here if the
+ // union includes empty object types (e.g. reducing {} | string to just {}).
+ result = getUnionType(widenedTypes, ts.some(widenedTypes, isEmptyObjectType) ? 2 /* Subtype */ : 1 /* Literal */);
+ }
+ else if (type.flags & 2097152 /* Intersection */) {
+ result = getIntersectionType(ts.sameMap(type.types, getWidenedType));
+ }
+ else if (isArrayType(type) || isTupleType(type)) {
+ result = createTypeReference(type.target, ts.sameMap(getTypeArguments(type), getWidenedType));
+ }
+ if (result && context === undefined) {
+ type.widened = result;
+ }
+ return result || type;
}
- // As noted in ECMAScript 6 language spec, arrow functions never have an arguments objects.
- // Although in down-level emit of arrow function, we emit it using function expression which means that
- // arguments objects will be bound to the inner object; emitting arrow function natively in ES6, arguments objects
- // will be bound to non-arrow function that contain this arrow function. This results in inconsistent behavior.
- // To avoid that we will give an error to users if they use arguments objects in arrow function so that they
- // can explicitly bound arguments objects
- if (symbol === argumentsSymbol) {
- var container = ts.getContainingFunction(node);
- if (languageVersion < 2 /* ES2015 */) {
- if (container.kind === 202 /* ArrowFunction */) {
- error(node, ts.Diagnostics.The_arguments_object_cannot_be_referenced_in_an_arrow_function_in_ES3_and_ES5_Consider_using_a_standard_function_expression);
+ return type;
+ }
+ /**
+ * Reports implicit any errors that occur as a result of widening 'null' and 'undefined'
+ * to 'any'. A call to reportWideningErrorsInType is normally accompanied by a call to
+ * getWidenedType. But in some cases getWidenedType is called without reporting errors
+ * (type argument inference is an example).
+ *
+ * The return value indicates whether an error was in fact reported. The particular circumstances
+ * are on a best effort basis. Currently, if the null or undefined that causes widening is inside
+ * an object literal property (arbitrarily deeply), this function reports an error. If no error is
+ * reported, reportImplicitAnyError is a suitable fallback to report a general error.
+ */
+ function reportWideningErrorsInType(type) {
+ var errorReported = false;
+ if (ts.getObjectFlags(type) & 524288 /* ContainsWideningType */) {
+ if (type.flags & 1048576 /* Union */) {
+ if (ts.some(type.types, isEmptyObjectType)) {
+ errorReported = true;
}
- else if (ts.hasModifier(container, 256 /* Async */)) {
- error(node, ts.Diagnostics.The_arguments_object_cannot_be_referenced_in_an_async_function_or_method_in_ES3_and_ES5_Consider_using_a_standard_function_or_method);
+ else {
+ for (var _i = 0, _a = type.types; _i < _a.length; _i++) {
+ var t = _a[_i];
+ if (reportWideningErrorsInType(t)) {
+ errorReported = true;
+ }
+ }
+ }
+ }
+ if (isArrayType(type) || isTupleType(type)) {
+ for (var _b = 0, _c = getTypeArguments(type); _b < _c.length; _b++) {
+ var t = _c[_b];
+ if (reportWideningErrorsInType(t)) {
+ errorReported = true;
+ }
+ }
+ }
+ if (isObjectLiteralType(type)) {
+ for (var _d = 0, _e = getPropertiesOfObjectType(type); _d < _e.length; _d++) {
+ var p = _e[_d];
+ var t = getTypeOfSymbol(p);
+ if (ts.getObjectFlags(t) & 524288 /* ContainsWideningType */) {
+ if (!reportWideningErrorsInType(t)) {
+ error(p.valueDeclaration, ts.Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type, symbolToString(p), typeToString(getWidenedType(t)));
+ }
+ errorReported = true;
+ }
}
}
- getNodeLinks(container).flags |= 8192 /* CaptureArguments */;
- return getTypeOfSymbol(symbol);
}
- // We should only mark aliases as referenced if there isn't a local value declaration
- // for the symbol. Also, don't mark any property access expression LHS - checkPropertyAccessExpression will handle that
- if (!(node.parent && ts.isPropertyAccessExpression(node.parent) && node.parent.expression === node)) {
- markAliasReferenced(symbol, node);
+ return errorReported;
+ }
+ function reportImplicitAny(declaration, type, wideningKind) {
+ var typeAsString = typeToString(getWidenedType(type));
+ if (ts.isInJSFile(declaration) && !ts.isCheckJsEnabledForFile(ts.getSourceFileOfNode(declaration), compilerOptions)) {
+ // Only report implicit any errors/suggestions in TS and ts-check JS files
+ return;
}
- var localOrExportSymbol = getExportSymbolOfValueSymbolIfExported(symbol);
- var declaration = localOrExportSymbol.valueDeclaration;
- if (localOrExportSymbol.flags & 32 /* Class */) {
- // Due to the emit for class decorators, any reference to the class from inside of the class body
- // must instead be rewritten to point to a temporary variable to avoid issues with the double-bind
- // behavior of class names in ES6.
- if (declaration.kind === 245 /* ClassDeclaration */
- && ts.nodeIsDecorated(declaration)) {
- var container = ts.getContainingClass(node);
- while (container !== undefined) {
- if (container === declaration && container.name !== node) {
- getNodeLinks(declaration).flags |= 16777216 /* ClassWithConstructorReference */;
- getNodeLinks(node).flags |= 33554432 /* ConstructorReferenceInClass */;
- break;
- }
- container = ts.getContainingClass(container);
+ var diagnostic;
+ switch (declaration.kind) {
+ case 216 /* BinaryExpression */:
+ case 163 /* PropertyDeclaration */:
+ case 162 /* PropertySignature */:
+ diagnostic = noImplicitAny ? ts.Diagnostics.Member_0_implicitly_has_an_1_type : ts.Diagnostics.Member_0_implicitly_has_an_1_type_but_a_better_type_may_be_inferred_from_usage;
+ break;
+ case 160 /* Parameter */:
+ var param = declaration;
+ if (ts.isIdentifier(param.name) &&
+ (ts.isCallSignatureDeclaration(param.parent) || ts.isMethodSignature(param.parent) || ts.isFunctionTypeNode(param.parent)) &&
+ param.parent.parameters.indexOf(param) > -1 &&
+ (resolveName(param, param.name.escapedText, 788968 /* Type */, undefined, param.name.escapedText, /*isUse*/ true) ||
+ param.name.originalKeywordKind && ts.isTypeNodeKind(param.name.originalKeywordKind))) {
+ var newName = "arg" + param.parent.parameters.indexOf(param);
+ errorOrSuggestion(noImplicitAny, declaration, ts.Diagnostics.Parameter_has_a_name_but_no_type_Did_you_mean_0_Colon_1, newName, ts.declarationNameToString(param.name));
+ return;
}
- }
- else if (declaration.kind === 214 /* ClassExpression */) {
- // When we emit a class expression with static members that contain a reference
- // to the constructor in the initializer, we will need to substitute that
- // binding with an alias as the class name is not in scope.
- var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false);
- while (container.kind !== 290 /* SourceFile */) {
- if (container.parent === declaration) {
- if (container.kind === 159 /* PropertyDeclaration */ && ts.hasModifier(container, 32 /* Static */)) {
- getNodeLinks(declaration).flags |= 16777216 /* ClassWithConstructorReference */;
- getNodeLinks(node).flags |= 33554432 /* ConstructorReferenceInClass */;
- }
- break;
- }
- container = ts.getThisContainer(container, /*includeArrowFunctions*/ false);
+ diagnostic = declaration.dotDotDotToken ?
+ noImplicitAny ? ts.Diagnostics.Rest_parameter_0_implicitly_has_an_any_type : ts.Diagnostics.Rest_parameter_0_implicitly_has_an_any_type_but_a_better_type_may_be_inferred_from_usage :
+ noImplicitAny ? ts.Diagnostics.Parameter_0_implicitly_has_an_1_type : ts.Diagnostics.Parameter_0_implicitly_has_an_1_type_but_a_better_type_may_be_inferred_from_usage;
+ break;
+ case 198 /* BindingElement */:
+ diagnostic = ts.Diagnostics.Binding_element_0_implicitly_has_an_1_type;
+ if (!noImplicitAny) {
+ // Don't issue a suggestion for binding elements since the codefix doesn't yet support them.
+ return;
}
- }
- }
- checkNestedBlockScopedBinding(node, symbol);
- var type = getConstraintForLocation(getTypeOfSymbol(localOrExportSymbol), node);
- var assignmentKind = ts.getAssignmentTargetKind(node);
- if (assignmentKind) {
- if (!(localOrExportSymbol.flags & 3 /* Variable */) &&
- !(ts.isInJSFile(node) && localOrExportSymbol.flags & 512 /* ValueModule */)) {
- error(node, ts.Diagnostics.Cannot_assign_to_0_because_it_is_not_a_variable, symbolToString(symbol));
- return errorType;
- }
- if (isReadonlySymbol(localOrExportSymbol)) {
- if (localOrExportSymbol.flags & 3 /* Variable */) {
- error(node, ts.Diagnostics.Cannot_assign_to_0_because_it_is_a_constant, symbolToString(symbol));
+ break;
+ case 308 /* JSDocFunctionType */:
+ error(declaration, ts.Diagnostics.Function_type_which_lacks_return_type_annotation_implicitly_has_an_0_return_type, typeAsString);
+ return;
+ case 251 /* FunctionDeclaration */:
+ case 165 /* MethodDeclaration */:
+ case 164 /* MethodSignature */:
+ case 167 /* GetAccessor */:
+ case 168 /* SetAccessor */:
+ case 208 /* FunctionExpression */:
+ case 209 /* ArrowFunction */:
+ if (noImplicitAny && !declaration.name) {
+ if (wideningKind === 3 /* GeneratorYield */) {
+ error(declaration, ts.Diagnostics.Generator_implicitly_has_yield_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type_annotation, typeAsString);
+ }
+ else {
+ error(declaration, ts.Diagnostics.Function_expression_which_lacks_return_type_annotation_implicitly_has_an_0_return_type, typeAsString);
+ }
+ return;
}
- else {
- error(node, ts.Diagnostics.Cannot_assign_to_0_because_it_is_a_read_only_property, symbolToString(symbol));
+ diagnostic = !noImplicitAny ? ts.Diagnostics._0_implicitly_has_an_1_return_type_but_a_better_type_may_be_inferred_from_usage :
+ wideningKind === 3 /* GeneratorYield */ ? ts.Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_yield_type :
+ ts.Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type;
+ break;
+ case 190 /* MappedType */:
+ if (noImplicitAny) {
+ error(declaration, ts.Diagnostics.Mapped_object_type_implicitly_has_an_any_template_type);
}
- return errorType;
+ return;
+ default:
+ diagnostic = noImplicitAny ? ts.Diagnostics.Variable_0_implicitly_has_an_1_type : ts.Diagnostics.Variable_0_implicitly_has_an_1_type_but_a_better_type_may_be_inferred_from_usage;
+ }
+ errorOrSuggestion(noImplicitAny, declaration, diagnostic, ts.declarationNameToString(ts.getNameOfDeclaration(declaration)), typeAsString);
+ }
+ function reportErrorsFromWidening(declaration, type, wideningKind) {
+ if (produceDiagnostics && noImplicitAny && ts.getObjectFlags(type) & 524288 /* ContainsWideningType */ && (!wideningKind || !getContextualSignatureForFunctionLikeDeclaration(declaration))) {
+ // Report implicit any error within type if possible, otherwise report error on declaration
+ if (!reportWideningErrorsInType(type)) {
+ reportImplicitAny(declaration, type, wideningKind);
}
}
- var isAlias = localOrExportSymbol.flags & 2097152 /* Alias */;
- // We only narrow variables and parameters occurring in a non-assignment position. For all other
- // entities we simply return the declared type.
- if (localOrExportSymbol.flags & 3 /* Variable */) {
- if (assignmentKind === 1 /* Definite */) {
- return type;
+ }
+ function applyToParameterTypes(source, target, callback) {
+ var sourceCount = getParameterCount(source);
+ var targetCount = getParameterCount(target);
+ var sourceRestType = getEffectiveRestType(source);
+ var targetRestType = getEffectiveRestType(target);
+ var targetNonRestCount = targetRestType ? targetCount - 1 : targetCount;
+ var paramCount = sourceRestType ? targetNonRestCount : Math.min(sourceCount, targetNonRestCount);
+ var sourceThisType = getThisTypeOfSignature(source);
+ if (sourceThisType) {
+ var targetThisType = getThisTypeOfSignature(target);
+ if (targetThisType) {
+ callback(sourceThisType, targetThisType);
}
}
- else if (isAlias) {
- declaration = ts.find(symbol.declarations, isSomeImportDeclaration);
+ for (var i = 0; i < paramCount; i++) {
+ callback(getTypeAtPosition(source, i), getTypeAtPosition(target, i));
}
- else {
- return type;
+ if (targetRestType) {
+ callback(getRestTypeAtPosition(source, paramCount), targetRestType);
}
- if (!declaration) {
- return type;
+ }
+ function applyToReturnTypes(source, target, callback) {
+ var sourceTypePredicate = getTypePredicateOfSignature(source);
+ var targetTypePredicate = getTypePredicateOfSignature(target);
+ if (sourceTypePredicate && targetTypePredicate && typePredicateKindsMatch(sourceTypePredicate, targetTypePredicate) && sourceTypePredicate.type && targetTypePredicate.type) {
+ callback(sourceTypePredicate.type, targetTypePredicate.type);
}
- // The declaration container is the innermost function that encloses the declaration of the variable
- // or parameter. The flow container is the innermost function starting with which we analyze the control
- // flow graph to determine the control flow based type.
- var isParameter = ts.getRootDeclaration(declaration).kind === 156 /* Parameter */;
- var declarationContainer = getControlFlowContainer(declaration);
- var flowContainer = getControlFlowContainer(node);
- var isOuterVariable = flowContainer !== declarationContainer;
- var isSpreadDestructuringAssignmentTarget = node.parent && node.parent.parent && ts.isSpreadAssignment(node.parent) && isDestructuringAssignmentTarget(node.parent.parent);
- var isModuleExports = symbol.flags & 134217728 /* ModuleExports */;
- // When the control flow originates in a function expression or arrow function and we are referencing
- // a const variable or parameter from an outer function, we extend the origin of the control flow
- // analysis to include the immediately enclosing function.
- while (flowContainer !== declarationContainer && (flowContainer.kind === 201 /* FunctionExpression */ ||
- flowContainer.kind === 202 /* ArrowFunction */ || ts.isObjectLiteralOrClassExpressionMethod(flowContainer)) &&
- (isConstVariable(localOrExportSymbol) || isParameter && !isParameterAssigned(localOrExportSymbol))) {
- flowContainer = getControlFlowContainer(flowContainer);
+ else {
+ callback(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target));
}
- // We only look for uninitialized variables in strict null checking mode, and only when we can analyze
- // the entire control flow graph from the variable's declaration (i.e. when the flow container and
- // declaration container are the same).
- var assumeInitialized = isParameter || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports || ts.isBindingElement(declaration) ||
- type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & (3 /* AnyOrUnknown */ | 16384 /* Void */)) !== 0 ||
- isInTypeQuery(node) || node.parent.kind === 263 /* ExportSpecifier */) ||
- node.parent.kind === 218 /* NonNullExpression */ ||
- declaration.kind === 242 /* VariableDeclaration */ && declaration.exclamationToken ||
- declaration.flags & 8388608 /* Ambient */;
- var initialType = assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, declaration) : type) :
- type === autoType || type === autoArrayType ? undefinedType :
- getOptionalType(type);
- var flowType = getFlowTypeOfReference(node, type, initialType, flowContainer, !assumeInitialized);
- // A variable is considered uninitialized when it is possible to analyze the entire control flow graph
- // from declaration to use, and when the variable's declared type doesn't include undefined but the
- // control flow based type does include undefined.
- if (!isEvolvingArrayOperationTarget(node) && (type === autoType || type === autoArrayType)) {
- if (flowType === autoType || flowType === autoArrayType) {
- if (noImplicitAny) {
- error(ts.getNameOfDeclaration(declaration), ts.Diagnostics.Variable_0_implicitly_has_type_1_in_some_locations_where_its_type_cannot_be_determined, symbolToString(symbol), typeToString(flowType));
- error(node, ts.Diagnostics.Variable_0_implicitly_has_an_1_type, symbolToString(symbol), typeToString(flowType));
+ }
+ function createInferenceContext(typeParameters, signature, flags, compareTypes) {
+ return createInferenceContextWorker(typeParameters.map(createInferenceInfo), signature, flags, compareTypes || compareTypesAssignable);
+ }
+ function cloneInferenceContext(context, extraFlags) {
+ if (extraFlags === void 0) { extraFlags = 0; }
+ return context && createInferenceContextWorker(ts.map(context.inferences, cloneInferenceInfo), context.signature, context.flags | extraFlags, context.compareTypes);
+ }
+ function createInferenceContextWorker(inferences, signature, flags, compareTypes) {
+ var context = {
+ inferences: inferences,
+ signature: signature,
+ flags: flags,
+ compareTypes: compareTypes,
+ mapper: makeFunctionTypeMapper(function (t) { return mapToInferredType(context, t, /*fix*/ true); }),
+ nonFixingMapper: makeFunctionTypeMapper(function (t) { return mapToInferredType(context, t, /*fix*/ false); }),
+ };
+ return context;
+ }
+ function mapToInferredType(context, t, fix) {
+ var inferences = context.inferences;
+ for (var i = 0; i < inferences.length; i++) {
+ var inference = inferences[i];
+ if (t === inference.typeParameter) {
+ if (fix && !inference.isFixed) {
+ clearCachedInferences(inferences);
+ inference.isFixed = true;
}
- return convertAutoToAny(flowType);
+ return getInferredType(context, i);
}
}
- else if (!assumeInitialized && !(getFalsyFlags(type) & 32768 /* Undefined */) && getFalsyFlags(flowType) & 32768 /* Undefined */) {
- error(node, ts.Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol));
- // Return the declared type to reduce follow-on errors
- return type;
+ return t;
+ }
+ function clearCachedInferences(inferences) {
+ for (var _i = 0, inferences_1 = inferences; _i < inferences_1.length; _i++) {
+ var inference = inferences_1[_i];
+ if (!inference.isFixed) {
+ inference.inferredType = undefined;
+ }
}
- return assignmentKind ? getBaseTypeOfLiteralType(flowType) : flowType;
}
- function isInsideFunction(node, threshold) {
- return !!ts.findAncestor(node, function (n) { return n === threshold ? "quit" : ts.isFunctionLike(n); });
+ function createInferenceInfo(typeParameter) {
+ return {
+ typeParameter: typeParameter,
+ candidates: undefined,
+ contraCandidates: undefined,
+ inferredType: undefined,
+ priority: undefined,
+ topLevel: true,
+ isFixed: false,
+ impliedArity: undefined
+ };
}
- function getPartOfForStatementContainingNode(node, container) {
- return ts.findAncestor(node, function (n) { return n === container ? "quit" : n === container.initializer || n === container.condition || n === container.incrementor || n === container.statement; });
+ function cloneInferenceInfo(inference) {
+ return {
+ typeParameter: inference.typeParameter,
+ candidates: inference.candidates && inference.candidates.slice(),
+ contraCandidates: inference.contraCandidates && inference.contraCandidates.slice(),
+ inferredType: inference.inferredType,
+ priority: inference.priority,
+ topLevel: inference.topLevel,
+ isFixed: inference.isFixed,
+ impliedArity: inference.impliedArity
+ };
}
- function checkNestedBlockScopedBinding(node, symbol) {
- if (languageVersion >= 2 /* ES2015 */ ||
- (symbol.flags & (2 /* BlockScopedVariable */ | 32 /* Class */)) === 0 ||
- ts.isSourceFile(symbol.valueDeclaration) ||
- symbol.valueDeclaration.parent.kind === 280 /* CatchClause */) {
- return;
+ function cloneInferredPartOfContext(context) {
+ var inferences = ts.filter(context.inferences, hasInferenceCandidates);
+ return inferences.length ?
+ createInferenceContextWorker(ts.map(inferences, cloneInferenceInfo), context.signature, context.flags, context.compareTypes) :
+ undefined;
+ }
+ function getMapperFromContext(context) {
+ return context && context.mapper;
+ }
+ // Return true if the given type could possibly reference a type parameter for which
+ // we perform type inference (i.e. a type parameter of a generic function). We cache
+ // results for union and intersection types for performance reasons.
+ function couldContainTypeVariables(type) {
+ var objectFlags = ts.getObjectFlags(type);
+ if (objectFlags & 67108864 /* CouldContainTypeVariablesComputed */) {
+ return !!(objectFlags & 134217728 /* CouldContainTypeVariables */);
}
- // 1. walk from the use site up to the declaration and check
- // if there is anything function like between declaration and use-site (is binding/class is captured in function).
- // 2. walk from the declaration up to the boundary of lexical environment and check
- // if there is an iteration statement in between declaration and boundary (is binding/class declared inside iteration statement)
- var container = ts.getEnclosingBlockScopeContainer(symbol.valueDeclaration);
- var usedInFunction = isInsideFunction(node.parent, container);
- var current = container;
- var containedInIterationStatement = false;
- while (current && !ts.nodeStartsNewLexicalEnvironment(current)) {
- if (ts.isIterationStatement(current, /*lookInLabeledStatements*/ false)) {
- containedInIterationStatement = true;
- break;
- }
- current = current.parent;
+ var result = !!(type.flags & 465829888 /* Instantiable */ ||
+ type.flags & 524288 /* Object */ && !isNonGenericTopLevelType(type) && (objectFlags & 4 /* Reference */ && (type.node || ts.forEach(getTypeArguments(type), couldContainTypeVariables)) ||
+ objectFlags & 16 /* Anonymous */ && type.symbol && type.symbol.flags & (16 /* Function */ | 8192 /* Method */ | 32 /* Class */ | 2048 /* TypeLiteral */ | 4096 /* ObjectLiteral */) && type.symbol.declarations ||
+ objectFlags & (32 /* Mapped */ | 131072 /* ObjectRestType */)) ||
+ type.flags & 3145728 /* UnionOrIntersection */ && !(type.flags & 1024 /* EnumLiteral */) && !isNonGenericTopLevelType(type) && ts.some(type.types, couldContainTypeVariables));
+ if (type.flags & 3899393 /* ObjectFlagsType */) {
+ type.objectFlags |= 67108864 /* CouldContainTypeVariablesComputed */ | (result ? 134217728 /* CouldContainTypeVariables */ : 0);
}
- if (containedInIterationStatement) {
- if (usedInFunction) {
- // mark iteration statement as containing block-scoped binding captured in some function
- var capturesBlockScopeBindingInLoopBody = true;
- if (ts.isForStatement(container)) {
- var varDeclList = ts.getAncestor(symbol.valueDeclaration, 243 /* VariableDeclarationList */);
- if (varDeclList && varDeclList.parent === container) {
- var part = getPartOfForStatementContainingNode(node.parent, container);
- if (part) {
- var links = getNodeLinks(part);
- links.flags |= 131072 /* ContainsCapturedBlockScopeBinding */;
- var capturedBindings = links.capturedBlockScopeBindings || (links.capturedBlockScopeBindings = []);
- ts.pushIfUnique(capturedBindings, symbol);
- if (part === container.initializer) {
- capturesBlockScopeBindingInLoopBody = false; // Initializer is outside of loop body
- }
- }
- }
- }
- if (capturesBlockScopeBindingInLoopBody) {
- getNodeLinks(current).flags |= 65536 /* LoopWithCapturedBlockScopedBinding */;
- }
+ return result;
+ }
+ function isNonGenericTopLevelType(type) {
+ if (type.aliasSymbol && !type.aliasTypeArguments) {
+ var declaration = ts.getDeclarationOfKind(type.aliasSymbol, 254 /* TypeAliasDeclaration */);
+ return !!(declaration && ts.findAncestor(declaration.parent, function (n) { return n.kind === 297 /* SourceFile */ ? true : n.kind === 256 /* ModuleDeclaration */ ? false : "quit"; }));
+ }
+ return false;
+ }
+ function isTypeParameterAtTopLevel(type, typeParameter) {
+ return !!(type === typeParameter ||
+ type.flags & 3145728 /* UnionOrIntersection */ && ts.some(type.types, function (t) { return isTypeParameterAtTopLevel(t, typeParameter); }) ||
+ type.flags & 16777216 /* Conditional */ && (getTrueTypeFromConditionalType(type) === typeParameter || getFalseTypeFromConditionalType(type) === typeParameter));
+ }
+ /** Create an object with properties named in the string literal type. Every property has type `any` */
+ function createEmptyObjectTypeFromStringLiteral(type) {
+ var members = ts.createSymbolTable();
+ forEachType(type, function (t) {
+ if (!(t.flags & 128 /* StringLiteral */)) {
+ return;
}
- // mark variables that are declared in loop initializer and reassigned inside the body of ForStatement.
- // if body of ForStatement will be converted to function then we'll need a extra machinery to propagate reassigned values back.
- if (ts.isForStatement(container)) {
- var varDeclList = ts.getAncestor(symbol.valueDeclaration, 243 /* VariableDeclarationList */);
- if (varDeclList && varDeclList.parent === container && isAssignedInBodyOfForStatement(node, container)) {
- getNodeLinks(symbol.valueDeclaration).flags |= 4194304 /* NeedsLoopOutParameter */;
- }
+ var name = ts.escapeLeadingUnderscores(t.value);
+ var literalProp = createSymbol(4 /* Property */, name);
+ literalProp.type = anyType;
+ if (t.symbol) {
+ literalProp.declarations = t.symbol.declarations;
+ literalProp.valueDeclaration = t.symbol.valueDeclaration;
}
- // set 'declared inside loop' bit on the block-scoped binding
- getNodeLinks(symbol.valueDeclaration).flags |= 524288 /* BlockScopedBindingInLoop */;
+ members.set(name, literalProp);
+ });
+ var indexInfo = type.flags & 4 /* String */ ? createIndexInfo(emptyObjectType, /*isReadonly*/ false) : undefined;
+ return createAnonymousType(undefined, members, ts.emptyArray, ts.emptyArray, indexInfo, undefined);
+ }
+ /**
+ * Infer a suitable input type for a homomorphic mapped type { [P in keyof T]: X }. We construct
+ * an object type with the same set of properties as the source type, where the type of each
+ * property is computed by inferring from the source property type to X for the type
+ * variable T[P] (i.e. we treat the type T[P] as the type variable we're inferring for).
+ */
+ function inferTypeForHomomorphicMappedType(source, target, constraint) {
+ if (inInferTypeForHomomorphicMappedType) {
+ return undefined;
}
- if (usedInFunction) {
- getNodeLinks(symbol.valueDeclaration).flags |= 262144 /* CapturedBlockScopedBinding */;
+ var key = source.id + "," + target.id + "," + constraint.id;
+ if (reverseMappedCache.has(key)) {
+ return reverseMappedCache.get(key);
}
+ inInferTypeForHomomorphicMappedType = true;
+ var type = createReverseMappedType(source, target, constraint);
+ inInferTypeForHomomorphicMappedType = false;
+ reverseMappedCache.set(key, type);
+ return type;
}
- function isBindingCapturedByNode(node, decl) {
- var links = getNodeLinks(node);
- return !!links && ts.contains(links.capturedBlockScopeBindings, getSymbolOfNode(decl));
+ // We consider a type to be partially inferable if it isn't marked non-inferable or if it is
+ // an object literal type with at least one property of an inferable type. For example, an object
+ // literal { a: 123, b: x => true } is marked non-inferable because it contains a context sensitive
+ // arrow function, but is considered partially inferable because property 'a' has an inferable type.
+ function isPartiallyInferableType(type) {
+ return !(ts.getObjectFlags(type) & 2097152 /* NonInferrableType */) ||
+ isObjectLiteralType(type) && ts.some(getPropertiesOfType(type), function (prop) { return isPartiallyInferableType(getTypeOfSymbol(prop)); }) ||
+ isTupleType(type) && ts.some(getTypeArguments(type), isPartiallyInferableType);
}
- function isAssignedInBodyOfForStatement(node, container) {
- // skip parenthesized nodes
- var current = node;
- while (current.parent.kind === 200 /* ParenthesizedExpression */) {
- current = current.parent;
- }
- // check if node is used as LHS in some assignment expression
- var isAssigned = false;
- if (ts.isAssignmentTarget(current)) {
- isAssigned = true;
+ function createReverseMappedType(source, target, constraint) {
+ // We consider a source type reverse mappable if it has a string index signature or if
+ // it has one or more properties and is of a partially inferable type.
+ if (!(getIndexInfoOfType(source, 0 /* String */) || getPropertiesOfType(source).length !== 0 && isPartiallyInferableType(source))) {
+ return undefined;
}
- else if ((current.parent.kind === 207 /* PrefixUnaryExpression */ || current.parent.kind === 208 /* PostfixUnaryExpression */)) {
- var expr = current.parent;
- isAssigned = expr.operator === 45 /* PlusPlusToken */ || expr.operator === 46 /* MinusMinusToken */;
+ // For arrays and tuples we infer new arrays and tuples where the reverse mapping has been
+ // applied to the element type(s).
+ if (isArrayType(source)) {
+ return createArrayType(inferReverseMappedType(getTypeArguments(source)[0], target, constraint), isReadonlyArrayType(source));
}
- if (!isAssigned) {
- return false;
+ if (isTupleType(source)) {
+ var elementTypes = ts.map(getTypeArguments(source), function (t) { return inferReverseMappedType(t, target, constraint); });
+ var elementFlags = getMappedTypeModifiers(target) & 4 /* IncludeOptional */ ?
+ ts.sameMap(source.target.elementFlags, function (f) { return f & 2 /* Optional */ ? 1 /* Required */ : f; }) :
+ source.target.elementFlags;
+ return createTupleType(elementTypes, elementFlags, source.target.readonly, source.target.labeledElementDeclarations);
}
- // at this point we know that node is the target of assignment
- // now check that modification happens inside the statement part of the ForStatement
- return !!ts.findAncestor(current, function (n) { return n === container ? "quit" : n === container.statement; });
+ // For all other object types we infer a new object type where the reverse mapping has been
+ // applied to the type of each property.
+ var reversed = createObjectType(2048 /* ReverseMapped */ | 16 /* Anonymous */, /*symbol*/ undefined);
+ reversed.source = source;
+ reversed.mappedType = target;
+ reversed.constraintType = constraint;
+ return reversed;
}
- function captureLexicalThis(node, container) {
- getNodeLinks(node).flags |= 2 /* LexicalThis */;
- if (container.kind === 159 /* PropertyDeclaration */ || container.kind === 162 /* Constructor */) {
- var classNode = container.parent;
- getNodeLinks(classNode).flags |= 4 /* CaptureThis */;
- }
- else {
- getNodeLinks(container).flags |= 4 /* CaptureThis */;
- }
+ function getTypeOfReverseMappedSymbol(symbol) {
+ return inferReverseMappedType(symbol.propertyType, symbol.mappedType, symbol.constraintType);
}
- function findFirstSuperCall(n) {
- if (ts.isSuperCall(n)) {
- return n;
- }
- else if (ts.isFunctionLike(n)) {
- return undefined;
- }
- return ts.forEachChild(n, findFirstSuperCall);
+ function inferReverseMappedType(sourceType, target, constraint) {
+ var typeParameter = getIndexedAccessType(constraint.type, getTypeParameterFromMappedType(target));
+ var templateType = getTemplateTypeFromMappedType(target);
+ var inference = createInferenceInfo(typeParameter);
+ inferTypes([inference], sourceType, templateType);
+ return getTypeFromInference(inference) || unknownType;
}
- /**
- * Return a cached result if super-statement is already found.
- * Otherwise, find a super statement in a given constructor function and cache the result in the node-links of the constructor
- *
- * @param constructor constructor-function to look for super statement
- */
- function getSuperCallInConstructor(constructor) {
- var links = getNodeLinks(constructor);
- // Only trying to find super-call if we haven't yet tried to find one. Once we try, we will record the result
- if (links.hasSuperCall === undefined) {
- links.superCall = findFirstSuperCall(constructor.body);
- links.hasSuperCall = links.superCall ? true : false;
- }
- return links.superCall;
+ function getUnmatchedProperties(source, target, requireOptionalProperties, matchDiscriminantProperties) {
+ var properties, _i, properties_2, targetProp, sourceProp, targetType, sourceType;
+ return __generator(this, function (_a) {
+ switch (_a.label) {
+ case 0:
+ properties = getPropertiesOfType(target);
+ _i = 0, properties_2 = properties;
+ _a.label = 1;
+ case 1:
+ if (!(_i < properties_2.length)) return [3 /*break*/, 6];
+ targetProp = properties_2[_i];
+ // TODO: remove this when we support static private identifier fields and find other solutions to get privateNamesAndStaticFields test to pass
+ if (isStaticPrivateIdentifierProperty(targetProp)) {
+ return [3 /*break*/, 5];
+ }
+ if (!(requireOptionalProperties || !(targetProp.flags & 16777216 /* Optional */ || ts.getCheckFlags(targetProp) & 48 /* Partial */))) return [3 /*break*/, 5];
+ sourceProp = getPropertyOfType(source, targetProp.escapedName);
+ if (!!sourceProp) return [3 /*break*/, 3];
+ return [4 /*yield*/, targetProp];
+ case 2:
+ _a.sent();
+ return [3 /*break*/, 5];
+ case 3:
+ if (!matchDiscriminantProperties) return [3 /*break*/, 5];
+ targetType = getTypeOfSymbol(targetProp);
+ if (!(targetType.flags & 109440 /* Unit */)) return [3 /*break*/, 5];
+ sourceType = getTypeOfSymbol(sourceProp);
+ if (!!(sourceType.flags & 1 /* Any */ || getRegularTypeOfLiteralType(sourceType) === getRegularTypeOfLiteralType(targetType))) return [3 /*break*/, 5];
+ return [4 /*yield*/, targetProp];
+ case 4:
+ _a.sent();
+ _a.label = 5;
+ case 5:
+ _i++;
+ return [3 /*break*/, 1];
+ case 6: return [2 /*return*/];
+ }
+ });
}
- /**
- * Check if the given class-declaration extends null then return true.
- * Otherwise, return false
- * @param classDecl a class declaration to check if it extends null
- */
- function classDeclarationExtendsNull(classDecl) {
- var classSymbol = getSymbolOfNode(classDecl);
- var classInstanceType = getDeclaredTypeOfSymbol(classSymbol);
- var baseConstructorType = getBaseConstructorTypeOfClass(classInstanceType);
- return baseConstructorType === nullWideningType;
+ function getUnmatchedProperty(source, target, requireOptionalProperties, matchDiscriminantProperties) {
+ var result = getUnmatchedProperties(source, target, requireOptionalProperties, matchDiscriminantProperties).next();
+ if (!result.done)
+ return result.value;
}
- function checkThisBeforeSuper(node, container, diagnosticMessage) {
- var containingClassDecl = container.parent;
- var baseTypeNode = ts.getClassExtendsHeritageElement(containingClassDecl);
- // If a containing class does not have extends clause or the class extends null
- // skip checking whether super statement is called before "this" accessing.
- if (baseTypeNode && !classDeclarationExtendsNull(containingClassDecl)) {
- var superCall = getSuperCallInConstructor(container);
- // We should give an error in the following cases:
- // - No super-call
- // - "this" is accessing before super-call.
- // i.e super(this)
- // this.x; super();
- // We want to make sure that super-call is done before accessing "this" so that
- // "this" is not accessed as a parameter of the super-call.
- if (!superCall || superCall.end > node.pos) {
- // In ES6, super inside constructor of class-declaration has to precede "this" accessing
- error(node, diagnosticMessage);
- }
- }
+ function tupleTypesDefinitelyUnrelated(source, target) {
+ return !(target.target.combinedFlags & 8 /* Variadic */) && target.target.minLength > source.target.minLength ||
+ !target.target.hasRestElement && (source.target.hasRestElement || target.target.fixedLength < source.target.fixedLength);
}
- function checkThisExpression(node) {
- // Stop at the first arrow function so that we can
- // tell whether 'this' needs to be captured.
- var container = ts.getThisContainer(node, /* includeArrowFunctions */ true);
- var capturedByArrowFunction = false;
- if (container.kind === 162 /* Constructor */) {
- checkThisBeforeSuper(node, container, ts.Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class);
- }
- // Now skip arrow functions to get the "real" owner of 'this'.
- if (container.kind === 202 /* ArrowFunction */) {
- container = ts.getThisContainer(container, /* includeArrowFunctions */ false);
- capturedByArrowFunction = true;
+ function typesDefinitelyUnrelated(source, target) {
+ // Two tuple types with incompatible arities are definitely unrelated.
+ // Two object types that each have a property that is unmatched in the other are definitely unrelated.
+ return isTupleType(source) && isTupleType(target) ? tupleTypesDefinitelyUnrelated(source, target) :
+ !!getUnmatchedProperty(source, target, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ true) &&
+ !!getUnmatchedProperty(target, source, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ true);
+ }
+ function getTypeFromInference(inference) {
+ return inference.candidates ? getUnionType(inference.candidates, 2 /* Subtype */) :
+ inference.contraCandidates ? getIntersectionType(inference.contraCandidates) :
+ undefined;
+ }
+ function hasSkipDirectInferenceFlag(node) {
+ return !!getNodeLinks(node).skipDirectInference;
+ }
+ function isFromInferenceBlockedSource(type) {
+ return !!(type.symbol && ts.some(type.symbol.declarations, hasSkipDirectInferenceFlag));
+ }
+ function isValidBigIntString(s) {
+ var scanner = ts.createScanner(99 /* ESNext */, /*skipTrivia*/ false);
+ var success = true;
+ scanner.setOnError(function () { return success = false; });
+ scanner.setText(s + "n");
+ var result = scanner.scan();
+ if (result === 40 /* MinusToken */) {
+ result = scanner.scan();
+ }
+ var flags = scanner.getTokenFlags();
+ // validate that
+ // * scanning proceeded without error
+ // * a bigint can be scanned, and that when it is scanned, it is
+ // * the full length of the input string (so the scanner is one character beyond the augmented input length)
+ // * it does not contain a numeric seperator (the `BigInt` constructor does not accept a numeric seperator in its input)
+ return success && result === 9 /* BigIntLiteral */ && scanner.getTextPos() === (s.length + 1) && !(flags & 512 /* ContainsSeparator */);
+ }
+ function isStringLiteralTypeValueParsableAsType(s, target) {
+ if (target.flags & 1048576 /* Union */) {
+ return !!forEachType(target, function (t) { return isStringLiteralTypeValueParsableAsType(s, t); });
+ }
+ switch (target) {
+ case stringType: return true;
+ case numberType: return s.value !== "" && isFinite(+(s.value));
+ case bigintType: return s.value !== "" && isValidBigIntString(s.value);
+ // the next 4 should be handled in `getTemplateLiteralType`, as they are all exactly one value, but are here for completeness, just in case
+ // this function is ever used on types which don't come from template literal holes
+ case trueType: return s.value === "true";
+ case falseType: return s.value === "false";
+ case undefinedType: return s.value === "undefined";
+ case nullType: return s.value === "null";
+ default: return !!(target.flags & 1 /* Any */);
+ }
+ }
+ function inferLiteralsFromTemplateLiteralType(source, target) {
+ var value = source.value;
+ var texts = target.texts;
+ var lastIndex = texts.length - 1;
+ var startText = texts[0];
+ var endText = texts[lastIndex];
+ if (!(value.startsWith(startText) && value.slice(startText.length).endsWith(endText)))
+ return undefined;
+ var matches = [];
+ var str = value.slice(startText.length, value.length - endText.length);
+ var pos = 0;
+ for (var i = 1; i < lastIndex; i++) {
+ var delim = texts[i];
+ var delimPos = delim.length > 0 ? str.indexOf(delim, pos) : pos < str.length ? pos + 1 : -1;
+ if (delimPos < 0)
+ return undefined;
+ matches.push(getLiteralType(str.slice(pos, delimPos)));
+ pos = delimPos + delim.length;
}
- switch (container.kind) {
- case 249 /* ModuleDeclaration */:
- error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_module_or_namespace_body);
- // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
- break;
- case 248 /* EnumDeclaration */:
- error(node, ts.Diagnostics.this_cannot_be_referenced_in_current_location);
- // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
- break;
- case 162 /* Constructor */:
- if (isInConstructorArgumentInitializer(node, container)) {
- error(node, ts.Diagnostics.this_cannot_be_referenced_in_constructor_arguments);
- // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
+ matches.push(getLiteralType(str.slice(pos)));
+ return matches;
+ }
+ function inferTypes(inferences, originalSource, originalTarget, priority, contravariant) {
+ if (priority === void 0) { priority = 0; }
+ if (contravariant === void 0) { contravariant = false; }
+ var bivariant = false;
+ var propagationType;
+ var inferencePriority = 1024 /* MaxValue */;
+ var allowComplexConstraintInference = true;
+ var visited;
+ var sourceStack;
+ var targetStack;
+ var expandingFlags = 0 /* None */;
+ inferFromTypes(originalSource, originalTarget);
+ function inferFromTypes(source, target) {
+ if (!couldContainTypeVariables(target)) {
+ return;
+ }
+ if (source === wildcardType) {
+ // We are inferring from an 'any' type. We want to infer this type for every type parameter
+ // referenced in the target type, so we record it as the propagation type and infer from the
+ // target to itself. Then, as we find candidates we substitute the propagation type.
+ var savePropagationType = propagationType;
+ propagationType = source;
+ inferFromTypes(target, target);
+ propagationType = savePropagationType;
+ return;
+ }
+ if (source.aliasSymbol && source.aliasTypeArguments && source.aliasSymbol === target.aliasSymbol) {
+ // Source and target are types originating in the same generic type alias declaration.
+ // Simply infer from source type arguments to target type arguments.
+ inferFromTypeArguments(source.aliasTypeArguments, target.aliasTypeArguments, getAliasVariances(source.aliasSymbol));
+ return;
+ }
+ if (source === target && source.flags & 3145728 /* UnionOrIntersection */) {
+ // When source and target are the same union or intersection type, just relate each constituent
+ // type to itself.
+ for (var _i = 0, _a = source.types; _i < _a.length; _i++) {
+ var t = _a[_i];
+ inferFromTypes(t, t);
}
- break;
- case 159 /* PropertyDeclaration */:
- case 158 /* PropertySignature */:
- if (ts.hasModifier(container, 32 /* Static */) && !(compilerOptions.target === 99 /* ESNext */ && compilerOptions.useDefineForClassFields)) {
- error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_static_property_initializer);
- // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
+ return;
+ }
+ if (target.flags & 1048576 /* Union */) {
+ // First, infer between identically matching source and target constituents and remove the
+ // matching types.
+ var _b = inferFromMatchingTypes(source.flags & 1048576 /* Union */ ? source.types : [source], target.types, isTypeOrBaseIdenticalTo), tempSources = _b[0], tempTargets = _b[1];
+ // Next, infer between closely matching source and target constituents and remove
+ // the matching types. Types closely match when they are instantiations of the same
+ // object type or instantiations of the same type alias.
+ var _c = inferFromMatchingTypes(tempSources, tempTargets, isTypeCloselyMatchedBy), sources = _c[0], targets = _c[1];
+ if (targets.length === 0) {
+ return;
}
- break;
- case 154 /* ComputedPropertyName */:
- error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_computed_property_name);
- break;
- }
- // When targeting es6, mark that we'll need to capture `this` in its lexically bound scope.
- if (capturedByArrowFunction && languageVersion < 2 /* ES2015 */) {
- captureLexicalThis(node, container);
- }
- var type = tryGetThisTypeAt(node, /*includeGlobalThis*/ true, container);
- if (noImplicitThis) {
- var globalThisType_1 = getTypeOfSymbol(globalThisSymbol);
- if (type === globalThisType_1 && capturedByArrowFunction) {
- error(node, ts.Diagnostics.The_containing_arrow_function_captures_the_global_value_of_this);
+ target = getUnionType(targets);
+ if (sources.length === 0) {
+ // All source constituents have been matched and there is nothing further to infer from.
+ // However, simply making no inferences is undesirable because it could ultimately mean
+ // inferring a type parameter constraint. Instead, make a lower priority inference from
+ // the full source to whatever remains in the target. For example, when inferring from
+ // string to 'string | T', make a lower priority inference of string for T.
+ inferWithPriority(source, target, 1 /* NakedTypeVariable */);
+ return;
+ }
+ source = getUnionType(sources);
}
- else if (!type) {
- // With noImplicitThis, functions may not reference 'this' if it has type 'any'
- var diag = error(node, ts.Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation);
- if (!ts.isSourceFile(container)) {
- var outsideThis = tryGetThisTypeAt(container);
- if (outsideThis && outsideThis !== globalThisType_1) {
- ts.addRelatedInfo(diag, ts.createDiagnosticForNode(container, ts.Diagnostics.An_outer_value_of_this_is_shadowed_by_this_container));
+ else if (target.flags & 2097152 /* Intersection */ && ts.some(target.types, function (t) { return !!getInferenceInfoForType(t) || (isGenericMappedType(t) && !!getInferenceInfoForType(getHomomorphicTypeVariable(t) || neverType)); })) {
+ // We reduce intersection types only when they contain naked type parameters. For example, when
+ // inferring from 'string[] & { extra: any }' to 'string[] & T' we want to remove string[] and
+ // infer { extra: any } for T. But when inferring to 'string[] & Iterable' we want to keep the
+ // string[] on the source side and infer string for T.
+ // Likewise, we consider a homomorphic mapped type constrainted to the target type parameter as similar to a "naked type variable"
+ // in such scenarios.
+ if (!(source.flags & 1048576 /* Union */)) {
+ // Infer between identically matching source and target constituents and remove the matching types.
+ var _d = inferFromMatchingTypes(source.flags & 2097152 /* Intersection */ ? source.types : [source], target.types, isTypeIdenticalTo), sources = _d[0], targets = _d[1];
+ if (sources.length === 0 || targets.length === 0) {
+ return;
}
+ source = getIntersectionType(sources);
+ target = getIntersectionType(targets);
}
}
- }
- return type || anyType;
- }
- function tryGetThisTypeAt(node, includeGlobalThis, container) {
- if (includeGlobalThis === void 0) { includeGlobalThis = true; }
- if (container === void 0) { container = ts.getThisContainer(node, /*includeArrowFunctions*/ false); }
- var isInJS = ts.isInJSFile(node);
- if (ts.isFunctionLike(container) &&
- (!isInParameterInitializerBeforeContainingFunction(node) || ts.getThisParameter(container))) {
- // Note: a parameter initializer should refer to class-this unless function-this is explicitly annotated.
- // If this is a function in a JS file, it might be a class method.
- var className = getClassNameFromPrototypeMethod(container);
- if (isInJS && className) {
- var classSymbol = checkExpression(className).symbol;
- if (classSymbol && classSymbol.members && (classSymbol.flags & 16 /* Function */)) {
- var classType = getDeclaredTypeOfSymbol(classSymbol).thisType;
- if (classType) {
- return getFlowTypeOfReference(node, classType);
- }
- }
- }
- // Check if it's a constructor definition, can be either a variable decl or function decl
- // i.e.
- // * /** @constructor */ function [name]() { ... }
- // * /** @constructor */ var x = function() { ... }
- else if (isInJS &&
- (container.kind === 201 /* FunctionExpression */ || container.kind === 244 /* FunctionDeclaration */) &&
- ts.getJSDocClassTag(container)) {
- var classType = getDeclaredTypeOfSymbol(getMergedSymbol(container.symbol)).thisType;
- return getFlowTypeOfReference(node, classType);
- }
- var thisType = getThisTypeOfDeclaration(container) || getContextualThisParameterType(container);
- if (thisType) {
- return getFlowTypeOfReference(node, thisType);
+ else if (target.flags & (8388608 /* IndexedAccess */ | 33554432 /* Substitution */)) {
+ target = getActualTypeVariable(target);
}
- }
- if (ts.isClassLike(container.parent)) {
- var symbol = getSymbolOfNode(container.parent);
- var type = ts.hasModifier(container, 32 /* Static */) ? getTypeOfSymbol(symbol) : getDeclaredTypeOfSymbol(symbol).thisType;
- return getFlowTypeOfReference(node, type);
- }
- if (isInJS) {
- var type = getTypeForThisExpressionFromJSDoc(container);
- if (type && type !== errorType) {
- return getFlowTypeOfReference(node, type);
+ if (target.flags & 8650752 /* TypeVariable */) {
+ // If target is a type parameter, make an inference, unless the source type contains
+ // the anyFunctionType (the wildcard type that's used to avoid contextually typing functions).
+ // Because the anyFunctionType is internal, it should not be exposed to the user by adding
+ // it as an inference candidate. Hopefully, a better candidate will come along that does
+ // not contain anyFunctionType when we come back to this argument for its second round
+ // of inference. Also, we exclude inferences for silentNeverType (which is used as a wildcard
+ // when constructing types from type parameters that had no inference candidates).
+ if (ts.getObjectFlags(source) & 2097152 /* NonInferrableType */ || source === nonInferrableAnyType || source === silentNeverType ||
+ (priority & 64 /* ReturnType */ && (source === autoType || source === autoArrayType)) || isFromInferenceBlockedSource(source)) {
+ return;
+ }
+ var inference = getInferenceInfoForType(target);
+ if (inference) {
+ if (!inference.isFixed) {
+ if (inference.priority === undefined || priority < inference.priority) {
+ inference.candidates = undefined;
+ inference.contraCandidates = undefined;
+ inference.topLevel = true;
+ inference.priority = priority;
+ }
+ if (priority === inference.priority) {
+ var candidate = propagationType || source;
+ // We make contravariant inferences only if we are in a pure contravariant position,
+ // i.e. only if we have not descended into a bivariant position.
+ if (contravariant && !bivariant) {
+ if (!ts.contains(inference.contraCandidates, candidate)) {
+ inference.contraCandidates = ts.append(inference.contraCandidates, candidate);
+ clearCachedInferences(inferences);
+ }
+ }
+ else if (!ts.contains(inference.candidates, candidate)) {
+ inference.candidates = ts.append(inference.candidates, candidate);
+ clearCachedInferences(inferences);
+ }
+ }
+ if (!(priority & 64 /* ReturnType */) && target.flags & 262144 /* TypeParameter */ && inference.topLevel && !isTypeParameterAtTopLevel(originalTarget, target)) {
+ inference.topLevel = false;
+ clearCachedInferences(inferences);
+ }
+ }
+ inferencePriority = Math.min(inferencePriority, priority);
+ return;
+ }
+ else {
+ // Infer to the simplified version of an indexed access, if possible, to (hopefully) expose more bare type parameters to the inference engine
+ var simplified = getSimplifiedType(target, /*writing*/ false);
+ if (simplified !== target) {
+ invokeOnce(source, simplified, inferFromTypes);
+ }
+ else if (target.flags & 8388608 /* IndexedAccess */) {
+ var indexType = getSimplifiedType(target.indexType, /*writing*/ false);
+ // Generally simplifications of instantiable indexes are avoided to keep relationship checking correct, however if our target is an access, we can consider
+ // that key of that access to be "instantiated", since we're looking to find the infernce goal in any way we can.
+ if (indexType.flags & 465829888 /* Instantiable */) {
+ var simplified_1 = distributeIndexOverObjectType(getSimplifiedType(target.objectType, /*writing*/ false), indexType, /*writing*/ false);
+ if (simplified_1 && simplified_1 !== target) {
+ invokeOnce(source, simplified_1, inferFromTypes);
+ }
+ }
+ }
+ }
}
- }
- if (ts.isSourceFile(container)) {
- // look up in the source file's locals or exports
- if (container.commonJsModuleIndicator) {
- var fileSymbol = getSymbolOfNode(container);
- return fileSymbol && getTypeOfSymbol(fileSymbol);
+ if (ts.getObjectFlags(source) & 4 /* Reference */ && ts.getObjectFlags(target) & 4 /* Reference */ && (source.target === target.target || isArrayType(source) && isArrayType(target)) &&
+ !(source.node && target.node)) {
+ // If source and target are references to the same generic type, infer from type arguments
+ inferFromTypeArguments(getTypeArguments(source), getTypeArguments(target), getVariances(source.target));
}
- else if (includeGlobalThis) {
- return getTypeOfSymbol(globalThisSymbol);
+ else if (source.flags & 4194304 /* Index */ && target.flags & 4194304 /* Index */) {
+ contravariant = !contravariant;
+ inferFromTypes(source.type, target.type);
+ contravariant = !contravariant;
}
- }
- }
- function getExplicitThisType(node) {
- var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false);
- if (ts.isFunctionLike(container)) {
- var signature = getSignatureFromDeclaration(container);
- if (signature.thisParameter) {
- return getExplicitTypeOfSymbol(signature.thisParameter);
+ else if ((isLiteralType(source) || source.flags & 4 /* String */) && target.flags & 4194304 /* Index */) {
+ var empty = createEmptyObjectTypeFromStringLiteral(source);
+ contravariant = !contravariant;
+ inferWithPriority(empty, target.type, 128 /* LiteralKeyof */);
+ contravariant = !contravariant;
}
- }
- if (ts.isClassLike(container.parent)) {
- var symbol = getSymbolOfNode(container.parent);
- return ts.hasModifier(container, 32 /* Static */) ? getTypeOfSymbol(symbol) : getDeclaredTypeOfSymbol(symbol).thisType;
- }
- }
- function getClassNameFromPrototypeMethod(container) {
- // Check if it's the RHS of a x.prototype.y = function [name]() { .... }
- if (container.kind === 201 /* FunctionExpression */ &&
- ts.isBinaryExpression(container.parent) &&
- ts.getAssignmentDeclarationKind(container.parent) === 3 /* PrototypeProperty */) {
- // Get the 'x' of 'x.prototype.y = container'
- return container.parent // x.prototype.y = container
- .left // x.prototype.y
- .expression // x.prototype
- .expression; // x
- }
- // x.prototype = { method() { } }
- else if (container.kind === 161 /* MethodDeclaration */ &&
- container.parent.kind === 193 /* ObjectLiteralExpression */ &&
- ts.isBinaryExpression(container.parent.parent) &&
- ts.getAssignmentDeclarationKind(container.parent.parent) === 6 /* Prototype */) {
- return container.parent.parent.left.expression;
- }
- // x.prototype = { method: function() { } }
- else if (container.kind === 201 /* FunctionExpression */ &&
- container.parent.kind === 281 /* PropertyAssignment */ &&
- container.parent.parent.kind === 193 /* ObjectLiteralExpression */ &&
- ts.isBinaryExpression(container.parent.parent.parent) &&
- ts.getAssignmentDeclarationKind(container.parent.parent.parent) === 6 /* Prototype */) {
- return container.parent.parent.parent.left.expression;
- }
- // Object.defineProperty(x, "method", { value: function() { } });
- // Object.defineProperty(x, "method", { set: (x: () => void) => void });
- // Object.defineProperty(x, "method", { get: () => function() { }) });
- else if (container.kind === 201 /* FunctionExpression */ &&
- ts.isPropertyAssignment(container.parent) &&
- ts.isIdentifier(container.parent.name) &&
- (container.parent.name.escapedText === "value" || container.parent.name.escapedText === "get" || container.parent.name.escapedText === "set") &&
- ts.isObjectLiteralExpression(container.parent.parent) &&
- ts.isCallExpression(container.parent.parent.parent) &&
- container.parent.parent.parent.arguments[2] === container.parent.parent &&
- ts.getAssignmentDeclarationKind(container.parent.parent.parent) === 9 /* ObjectDefinePrototypeProperty */) {
- return container.parent.parent.parent.arguments[0].expression;
- }
- // Object.defineProperty(x, "method", { value() { } });
- // Object.defineProperty(x, "method", { set(x: () => void) {} });
- // Object.defineProperty(x, "method", { get() { return () => {} } });
- else if (ts.isMethodDeclaration(container) &&
- ts.isIdentifier(container.name) &&
- (container.name.escapedText === "value" || container.name.escapedText === "get" || container.name.escapedText === "set") &&
- ts.isObjectLiteralExpression(container.parent) &&
- ts.isCallExpression(container.parent.parent) &&
- container.parent.parent.arguments[2] === container.parent &&
- ts.getAssignmentDeclarationKind(container.parent.parent) === 9 /* ObjectDefinePrototypeProperty */) {
- return container.parent.parent.arguments[0].expression;
- }
- }
- function getTypeForThisExpressionFromJSDoc(node) {
- var jsdocType = ts.getJSDocType(node);
- if (jsdocType && jsdocType.kind === 300 /* JSDocFunctionType */) {
- var jsDocFunctionType = jsdocType;
- if (jsDocFunctionType.parameters.length > 0 &&
- jsDocFunctionType.parameters[0].name &&
- jsDocFunctionType.parameters[0].name.escapedText === "this" /* This */) {
- return getTypeFromTypeNode(jsDocFunctionType.parameters[0].type);
+ else if (source.flags & 8388608 /* IndexedAccess */ && target.flags & 8388608 /* IndexedAccess */) {
+ inferFromTypes(source.objectType, target.objectType);
+ inferFromTypes(source.indexType, target.indexType);
}
- }
- var thisTag = ts.getJSDocThisTag(node);
- if (thisTag && thisTag.typeExpression) {
- return getTypeFromTypeNode(thisTag.typeExpression);
- }
- }
- function isInConstructorArgumentInitializer(node, constructorDecl) {
- return !!ts.findAncestor(node, function (n) { return ts.isFunctionLikeDeclaration(n) ? "quit" : n.kind === 156 /* Parameter */ && n.parent === constructorDecl; });
- }
- function checkSuperExpression(node) {
- var isCallExpression = node.parent.kind === 196 /* CallExpression */ && node.parent.expression === node;
- var container = ts.getSuperContainer(node, /*stopOnFunctions*/ true);
- var needToCaptureLexicalThis = false;
- // adjust the container reference in case if super is used inside arrow functions with arbitrarily deep nesting
- if (!isCallExpression) {
- while (container && container.kind === 202 /* ArrowFunction */) {
- container = ts.getSuperContainer(container, /*stopOnFunctions*/ true);
- needToCaptureLexicalThis = languageVersion < 2 /* ES2015 */;
+ else if (source.flags & 268435456 /* StringMapping */ && target.flags & 268435456 /* StringMapping */) {
+ if (source.symbol === target.symbol) {
+ inferFromTypes(source.type, target.type);
+ }
}
- }
- var canUseSuperExpression = isLegalUsageOfSuperExpression(container);
- var nodeCheckFlag = 0;
- if (!canUseSuperExpression) {
- // issue more specific error if super is used in computed property name
- // class A { foo() { return "1" }}
- // class B {
- // [super.foo()]() {}
- // }
- var current = ts.findAncestor(node, function (n) { return n === container ? "quit" : n.kind === 154 /* ComputedPropertyName */; });
- if (current && current.kind === 154 /* ComputedPropertyName */) {
- error(node, ts.Diagnostics.super_cannot_be_referenced_in_a_computed_property_name);
+ else if (target.flags & 16777216 /* Conditional */) {
+ invokeOnce(source, target, inferToConditionalType);
}
- else if (isCallExpression) {
- error(node, ts.Diagnostics.Super_calls_are_not_permitted_outside_constructors_or_in_nested_functions_inside_constructors);
+ else if (target.flags & 3145728 /* UnionOrIntersection */) {
+ inferToMultipleTypes(source, target.types, target.flags);
}
- else if (!container || !container.parent || !(ts.isClassLike(container.parent) || container.parent.kind === 193 /* ObjectLiteralExpression */)) {
- error(node, ts.Diagnostics.super_can_only_be_referenced_in_members_of_derived_classes_or_object_literal_expressions);
+ else if (source.flags & 1048576 /* Union */) {
+ // Source is a union or intersection type, infer from each constituent type
+ var sourceTypes = source.types;
+ for (var _e = 0, sourceTypes_2 = sourceTypes; _e < sourceTypes_2.length; _e++) {
+ var sourceType = sourceTypes_2[_e];
+ inferFromTypes(sourceType, target);
+ }
+ }
+ else if (target.flags & 134217728 /* TemplateLiteral */) {
+ inferToTemplateLiteralType(source, target);
}
else {
- error(node, ts.Diagnostics.super_property_access_is_permitted_only_in_a_constructor_member_function_or_member_accessor_of_a_derived_class);
+ source = getReducedType(source);
+ if (!(priority & 256 /* NoConstraints */ && source.flags & (2097152 /* Intersection */ | 465829888 /* Instantiable */))) {
+ var apparentSource = getApparentType(source);
+ // getApparentType can return _any_ type, since an indexed access or conditional may simplify to any other type.
+ // If that occurs and it doesn't simplify to an object or intersection, we'll need to restart `inferFromTypes`
+ // with the simplified source.
+ if (apparentSource !== source && allowComplexConstraintInference && !(apparentSource.flags & (524288 /* Object */ | 2097152 /* Intersection */))) {
+ // TODO: The `allowComplexConstraintInference` flag is a hack! This forbids inference from complex constraints within constraints!
+ // This isn't required algorithmically, but rather is used to lower the memory burden caused by performing inference
+ // that is _too good_ in projects with complicated constraints (eg, fp-ts). In such cases, if we did not limit ourselves
+ // here, we might produce more valid inferences for types, causing us to do more checks and perform more instantiations
+ // (in addition to the extra stack depth here) which, in turn, can push the already close process over its limit.
+ // TL;DR: If we ever become generally more memory efficient (or our resource budget ever increases), we should just
+ // remove this `allowComplexConstraintInference` flag.
+ allowComplexConstraintInference = false;
+ return inferFromTypes(apparentSource, target);
+ }
+ source = apparentSource;
+ }
+ if (source.flags & (524288 /* Object */ | 2097152 /* Intersection */)) {
+ invokeOnce(source, target, inferFromObjectTypes);
+ }
+ }
+ if (source.flags & 25165824 /* Simplifiable */) {
+ var simplified = getSimplifiedType(source, contravariant);
+ if (simplified !== source) {
+ inferFromTypes(simplified, target);
+ }
}
- return errorType;
- }
- if (!isCallExpression && container.kind === 162 /* Constructor */) {
- checkThisBeforeSuper(node, container, ts.Diagnostics.super_must_be_called_before_accessing_a_property_of_super_in_the_constructor_of_a_derived_class);
- }
- if (ts.hasModifier(container, 32 /* Static */) || isCallExpression) {
- nodeCheckFlag = 512 /* SuperStatic */;
}
- else {
- nodeCheckFlag = 256 /* SuperInstance */;
+ function inferWithPriority(source, target, newPriority) {
+ var savePriority = priority;
+ priority |= newPriority;
+ inferFromTypes(source, target);
+ priority = savePriority;
}
- getNodeLinks(node).flags |= nodeCheckFlag;
- // Due to how we emit async functions, we need to specialize the emit for an async method that contains a `super` reference.
- // This is due to the fact that we emit the body of an async function inside of a generator function. As generator
- // functions cannot reference `super`, we emit a helper inside of the method body, but outside of the generator. This helper
- // uses an arrow function, which is permitted to reference `super`.
- //
- // There are two primary ways we can access `super` from within an async method. The first is getting the value of a property
- // or indexed access on super, either as part of a right-hand-side expression or call expression. The second is when setting the value
- // of a property or indexed access, either as part of an assignment expression or destructuring assignment.
- //
- // The simplest case is reading a value, in which case we will emit something like the following:
- //
- // // ts
- // ...
- // async asyncMethod() {
- // let x = await super.asyncMethod();
- // return x;
- // }
- // ...
- //
- // // js
- // ...
- // asyncMethod() {
- // const _super = Object.create(null, {
- // asyncMethod: { get: () => super.asyncMethod },
- // });
- // return __awaiter(this, arguments, Promise, function *() {
- // let x = yield _super.asyncMethod.call(this);
- // return x;
- // });
- // }
- // ...
- //
- // The more complex case is when we wish to assign a value, especially as part of a destructuring assignment. As both cases
- // are legal in ES6, but also likely less frequent, we only emit setters if there is an assignment:
- //
- // // ts
- // ...
- // async asyncMethod(ar: Promise) {
- // [super.a, super.b] = await ar;
- // }
- // ...
- //
- // // js
- // ...
- // asyncMethod(ar) {
- // const _super = Object.create(null, {
- // a: { get: () => super.a, set: (v) => super.a = v },
- // b: { get: () => super.b, set: (v) => super.b = v }
- // };
- // return __awaiter(this, arguments, Promise, function *() {
- // [_super.a, _super.b] = yield ar;
- // });
- // }
- // ...
- //
- // Creating an object that has getter and setters instead of just an accessor function is required for destructuring assignments
- // as a call expression cannot be used as the target of a destructuring assignment while a property access can.
- //
- // For element access expressions (`super[x]`), we emit a generic helper that forwards the element access in both situations.
- if (container.kind === 161 /* MethodDeclaration */ && ts.hasModifier(container, 256 /* Async */)) {
- if (ts.isSuperProperty(node.parent) && ts.isAssignmentTarget(node.parent)) {
- getNodeLinks(container).flags |= 4096 /* AsyncMethodWithSuperBinding */;
+ function invokeOnce(source, target, action) {
+ var key = source.id + "," + target.id;
+ var status = visited && visited.get(key);
+ if (status !== undefined) {
+ inferencePriority = Math.min(inferencePriority, status);
+ return;
+ }
+ (visited || (visited = new ts.Map())).set(key, -1 /* Circularity */);
+ var saveInferencePriority = inferencePriority;
+ inferencePriority = 1024 /* MaxValue */;
+ // We stop inferring and report a circularity if we encounter duplicate recursion identities on both
+ // the source side and the target side.
+ var saveExpandingFlags = expandingFlags;
+ var sourceIdentity = getRecursionIdentity(source) || source;
+ var targetIdentity = getRecursionIdentity(target) || target;
+ if (sourceIdentity && ts.contains(sourceStack, sourceIdentity))
+ expandingFlags |= 1 /* Source */;
+ if (targetIdentity && ts.contains(targetStack, targetIdentity))
+ expandingFlags |= 2 /* Target */;
+ if (expandingFlags !== 3 /* Both */) {
+ if (sourceIdentity)
+ (sourceStack || (sourceStack = [])).push(sourceIdentity);
+ if (targetIdentity)
+ (targetStack || (targetStack = [])).push(targetIdentity);
+ action(source, target);
+ if (targetIdentity)
+ targetStack.pop();
+ if (sourceIdentity)
+ sourceStack.pop();
}
else {
- getNodeLinks(container).flags |= 2048 /* AsyncMethodWithSuper */;
+ inferencePriority = -1 /* Circularity */;
+ }
+ expandingFlags = saveExpandingFlags;
+ visited.set(key, inferencePriority);
+ inferencePriority = Math.min(inferencePriority, saveInferencePriority);
+ }
+ function inferFromMatchingTypes(sources, targets, matches) {
+ var matchedSources;
+ var matchedTargets;
+ for (var _i = 0, targets_1 = targets; _i < targets_1.length; _i++) {
+ var t = targets_1[_i];
+ for (var _a = 0, sources_1 = sources; _a < sources_1.length; _a++) {
+ var s = sources_1[_a];
+ if (matches(s, t)) {
+ inferFromTypes(s, t);
+ matchedSources = ts.appendIfUnique(matchedSources, s);
+ matchedTargets = ts.appendIfUnique(matchedTargets, t);
+ }
+ }
}
+ return [
+ matchedSources ? ts.filter(sources, function (t) { return !ts.contains(matchedSources, t); }) : sources,
+ matchedTargets ? ts.filter(targets, function (t) { return !ts.contains(matchedTargets, t); }) : targets,
+ ];
}
- if (needToCaptureLexicalThis) {
- // call expressions are allowed only in constructors so they should always capture correct 'this'
- // super property access expressions can also appear in arrow functions -
- // in this case they should also use correct lexical this
- captureLexicalThis(node.parent, container);
+ function inferFromTypeArguments(sourceTypes, targetTypes, variances) {
+ var count = sourceTypes.length < targetTypes.length ? sourceTypes.length : targetTypes.length;
+ for (var i = 0; i < count; i++) {
+ if (i < variances.length && (variances[i] & 7 /* VarianceMask */) === 2 /* Contravariant */) {
+ inferFromContravariantTypes(sourceTypes[i], targetTypes[i]);
+ }
+ else {
+ inferFromTypes(sourceTypes[i], targetTypes[i]);
+ }
+ }
}
- if (container.parent.kind === 193 /* ObjectLiteralExpression */) {
- if (languageVersion < 2 /* ES2015 */) {
- error(node, ts.Diagnostics.super_is_only_allowed_in_members_of_object_literal_expressions_when_option_target_is_ES2015_or_higher);
- return errorType;
+ function inferFromContravariantTypes(source, target) {
+ if (strictFunctionTypes || priority & 512 /* AlwaysStrict */) {
+ contravariant = !contravariant;
+ inferFromTypes(source, target);
+ contravariant = !contravariant;
}
else {
- // for object literal assume that type of 'super' is 'any'
- return anyType;
+ inferFromTypes(source, target);
}
}
- // at this point the only legal case for parent is ClassLikeDeclaration
- var classLikeDeclaration = container.parent;
- if (!ts.getClassExtendsHeritageElement(classLikeDeclaration)) {
- error(node, ts.Diagnostics.super_can_only_be_referenced_in_a_derived_class);
- return errorType;
- }
- var classType = getDeclaredTypeOfSymbol(getSymbolOfNode(classLikeDeclaration));
- var baseClassType = classType && getBaseTypes(classType)[0];
- if (!baseClassType) {
- return errorType;
- }
- if (container.kind === 162 /* Constructor */ && isInConstructorArgumentInitializer(node, container)) {
- // issue custom error message for super property access in constructor arguments (to be aligned with old compiler)
- error(node, ts.Diagnostics.super_cannot_be_referenced_in_constructor_arguments);
- return errorType;
+ function getInferenceInfoForType(type) {
+ if (type.flags & 8650752 /* TypeVariable */) {
+ for (var _i = 0, inferences_2 = inferences; _i < inferences_2.length; _i++) {
+ var inference = inferences_2[_i];
+ if (type === inference.typeParameter) {
+ return inference;
+ }
+ }
+ }
+ return undefined;
}
- return nodeCheckFlag === 512 /* SuperStatic */
- ? getBaseConstructorTypeOfClass(classType)
- : getTypeWithThisArgument(baseClassType, classType.thisType);
- function isLegalUsageOfSuperExpression(container) {
- if (!container) {
- return false;
+ function getSingleTypeVariableFromIntersectionTypes(types) {
+ var typeVariable;
+ for (var _i = 0, types_15 = types; _i < types_15.length; _i++) {
+ var type = types_15[_i];
+ var t = type.flags & 2097152 /* Intersection */ && ts.find(type.types, function (t) { return !!getInferenceInfoForType(t); });
+ if (!t || typeVariable && t !== typeVariable) {
+ return undefined;
+ }
+ typeVariable = t;
}
- if (isCallExpression) {
- // TS 1.0 SPEC (April 2014): 4.8.1
- // Super calls are only permitted in constructors of derived classes
- return container.kind === 162 /* Constructor */;
+ return typeVariable;
+ }
+ function inferToMultipleTypes(source, targets, targetFlags) {
+ var typeVariableCount = 0;
+ if (targetFlags & 1048576 /* Union */) {
+ var nakedTypeVariable = void 0;
+ var sources = source.flags & 1048576 /* Union */ ? source.types : [source];
+ var matched_1 = new Array(sources.length);
+ var inferenceCircularity = false;
+ // First infer to types that are not naked type variables. For each source type we
+ // track whether inferences were made from that particular type to some target with
+ // equal priority (i.e. of equal quality) to what we would infer for a naked type
+ // parameter.
+ for (var _i = 0, targets_2 = targets; _i < targets_2.length; _i++) {
+ var t = targets_2[_i];
+ if (getInferenceInfoForType(t)) {
+ nakedTypeVariable = t;
+ typeVariableCount++;
+ }
+ else {
+ for (var i = 0; i < sources.length; i++) {
+ var saveInferencePriority = inferencePriority;
+ inferencePriority = 1024 /* MaxValue */;
+ inferFromTypes(sources[i], t);
+ if (inferencePriority === priority)
+ matched_1[i] = true;
+ inferenceCircularity = inferenceCircularity || inferencePriority === -1 /* Circularity */;
+ inferencePriority = Math.min(inferencePriority, saveInferencePriority);
+ }
+ }
+ }
+ if (typeVariableCount === 0) {
+ // If every target is an intersection of types containing a single naked type variable,
+ // make a lower priority inference to that type variable. This handles inferring from
+ // 'A | B' to 'T & (X | Y)' where we want to infer 'A | B' for T.
+ var intersectionTypeVariable = getSingleTypeVariableFromIntersectionTypes(targets);
+ if (intersectionTypeVariable) {
+ inferWithPriority(source, intersectionTypeVariable, 1 /* NakedTypeVariable */);
+ }
+ return;
+ }
+ // If the target has a single naked type variable and no inference circularities were
+ // encountered above (meaning we explored the types fully), create a union of the source
+ // types from which no inferences have been made so far and infer from that union to the
+ // naked type variable.
+ if (typeVariableCount === 1 && !inferenceCircularity) {
+ var unmatched = ts.flatMap(sources, function (s, i) { return matched_1[i] ? undefined : s; });
+ if (unmatched.length) {
+ inferFromTypes(getUnionType(unmatched), nakedTypeVariable);
+ return;
+ }
+ }
}
else {
- // TS 1.0 SPEC (April 2014)
- // 'super' property access is allowed
- // - In a constructor, instance member function, instance member accessor, or instance member variable initializer where this references a derived class instance
- // - In a static member function or static member accessor
- // topmost container must be something that is directly nested in the class declaration\object literal expression
- if (ts.isClassLike(container.parent) || container.parent.kind === 193 /* ObjectLiteralExpression */) {
- if (ts.hasModifier(container, 32 /* Static */)) {
- return container.kind === 161 /* MethodDeclaration */ ||
- container.kind === 160 /* MethodSignature */ ||
- container.kind === 163 /* GetAccessor */ ||
- container.kind === 164 /* SetAccessor */;
+ // We infer from types that are not naked type variables first so that inferences we
+ // make from nested naked type variables and given slightly higher priority by virtue
+ // of being first in the candidates array.
+ for (var _a = 0, targets_3 = targets; _a < targets_3.length; _a++) {
+ var t = targets_3[_a];
+ if (getInferenceInfoForType(t)) {
+ typeVariableCount++;
}
else {
- return container.kind === 161 /* MethodDeclaration */ ||
- container.kind === 160 /* MethodSignature */ ||
- container.kind === 163 /* GetAccessor */ ||
- container.kind === 164 /* SetAccessor */ ||
- container.kind === 159 /* PropertyDeclaration */ ||
- container.kind === 158 /* PropertySignature */ ||
- container.kind === 162 /* Constructor */;
+ inferFromTypes(source, t);
}
}
}
- return false;
- }
- }
- function getContainingObjectLiteral(func) {
- return (func.kind === 161 /* MethodDeclaration */ ||
- func.kind === 163 /* GetAccessor */ ||
- func.kind === 164 /* SetAccessor */) && func.parent.kind === 193 /* ObjectLiteralExpression */ ? func.parent :
- func.kind === 201 /* FunctionExpression */ && func.parent.kind === 281 /* PropertyAssignment */ ? func.parent.parent :
- undefined;
- }
- function getThisTypeArgument(type) {
- return ts.getObjectFlags(type) & 4 /* Reference */ && type.target === globalThisType ? getTypeArguments(type)[0] : undefined;
- }
- function getThisTypeFromContextualType(type) {
- return mapType(type, function (t) {
- return t.flags & 2097152 /* Intersection */ ? ts.forEach(t.types, getThisTypeArgument) : getThisTypeArgument(t);
- });
- }
- function getContextualThisParameterType(func) {
- if (func.kind === 202 /* ArrowFunction */) {
- return undefined;
- }
- if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) {
- var contextualSignature = getContextualSignature(func);
- if (contextualSignature) {
- var thisParameter = contextualSignature.thisParameter;
- if (thisParameter) {
- return getTypeOfSymbol(thisParameter);
+ // Inferences directly to naked type variables are given lower priority as they are
+ // less specific. For example, when inferring from Promise to T | Promise,
+ // we want to infer string for T, not Promise | string. For intersection types
+ // we only infer to single naked type variables.
+ if (targetFlags & 2097152 /* Intersection */ ? typeVariableCount === 1 : typeVariableCount > 0) {
+ for (var _b = 0, targets_4 = targets; _b < targets_4.length; _b++) {
+ var t = targets_4[_b];
+ if (getInferenceInfoForType(t)) {
+ inferWithPriority(source, t, 1 /* NakedTypeVariable */);
+ }
}
}
}
- var inJs = ts.isInJSFile(func);
- if (noImplicitThis || inJs) {
- var containingLiteral = getContainingObjectLiteral(func);
- if (containingLiteral) {
- // We have an object literal method. Check if the containing object literal has a contextual type
- // that includes a ThisType. If so, T is the contextual type for 'this'. We continue looking in
- // any directly enclosing object literals.
- var contextualType = getApparentTypeOfContextualType(containingLiteral);
- var literal = containingLiteral;
- var type = contextualType;
- while (type) {
- var thisType = getThisTypeFromContextualType(type);
- if (thisType) {
- return instantiateType(thisType, getMapperFromContext(getInferenceContext(containingLiteral)));
- }
- if (literal.parent.kind !== 281 /* PropertyAssignment */) {
- break;
- }
- literal = literal.parent.parent;
- type = getApparentTypeOfContextualType(literal);
+ function inferToMappedType(source, target, constraintType) {
+ if (constraintType.flags & 1048576 /* Union */) {
+ var result = false;
+ for (var _i = 0, _a = constraintType.types; _i < _a.length; _i++) {
+ var type = _a[_i];
+ result = inferToMappedType(source, target, type) || result;
}
- // There was no contextual ThisType for the containing object literal, so the contextual type
- // for 'this' is the non-null form of the contextual type for the containing object literal or
- // the type of the object literal itself.
- return getWidenedType(contextualType ? getNonNullableType(contextualType) : checkExpressionCached(containingLiteral));
+ return result;
}
- // In an assignment of the form 'obj.xxx = function(...)' or 'obj[xxx] = function(...)', the
- // contextual type for 'this' is 'obj'.
- var parent = ts.walkUpParenthesizedExpressions(func.parent);
- if (parent.kind === 209 /* BinaryExpression */ && parent.operatorToken.kind === 62 /* EqualsToken */) {
- var target = parent.left;
- if (ts.isAccessExpression(target)) {
- var expression = target.expression;
- // Don't contextually type `this` as `exports` in `exports.Point = function(x, y) { this.x = x; this.y = y; }`
- if (inJs && ts.isIdentifier(expression)) {
- var sourceFile = ts.getSourceFileOfNode(parent);
- if (sourceFile.commonJsModuleIndicator && getResolvedSymbol(expression) === sourceFile.symbol) {
- return undefined;
- }
+ if (constraintType.flags & 4194304 /* Index */) {
+ // We're inferring from some source type S to a homomorphic mapped type { [P in keyof T]: X },
+ // where T is a type variable. Use inferTypeForHomomorphicMappedType to infer a suitable source
+ // type and then make a secondary inference from that type to T. We make a secondary inference
+ // such that direct inferences to T get priority over inferences to Partial, for example.
+ var inference = getInferenceInfoForType(constraintType.type);
+ if (inference && !inference.isFixed && !isFromInferenceBlockedSource(source)) {
+ var inferredType = inferTypeForHomomorphicMappedType(source, target, constraintType);
+ if (inferredType) {
+ // We assign a lower priority to inferences made from types containing non-inferrable
+ // types because we may only have a partial result (i.e. we may have failed to make
+ // reverse inferences for some properties).
+ inferWithPriority(inferredType, inference.typeParameter, ts.getObjectFlags(source) & 2097152 /* NonInferrableType */ ?
+ 8 /* PartialHomomorphicMappedType */ :
+ 4 /* HomomorphicMappedType */);
}
- return getWidenedType(checkExpressionCached(expression));
}
+ return true;
}
- }
- return undefined;
- }
- // Return contextual type of parameter or undefined if no contextual type is available
- function getContextuallyTypedParameterType(parameter) {
- var func = parameter.parent;
- if (!isContextSensitiveFunctionOrObjectLiteralMethod(func)) {
- return undefined;
- }
- var iife = ts.getImmediatelyInvokedFunctionExpression(func);
- if (iife && iife.arguments) {
- var args = getEffectiveCallArguments(iife);
- var indexOfParameter = func.parameters.indexOf(parameter);
- if (parameter.dotDotDotToken) {
- return getSpreadArgumentType(args, indexOfParameter, args.length, anyType, /*context*/ undefined);
+ if (constraintType.flags & 262144 /* TypeParameter */) {
+ // We're inferring from some source type S to a mapped type { [P in K]: X }, where K is a type
+ // parameter. First infer from 'keyof S' to K.
+ inferWithPriority(getIndexType(source), constraintType, 16 /* MappedTypeConstraint */);
+ // If K is constrained to a type C, also infer to C. Thus, for a mapped type { [P in K]: X },
+ // where K extends keyof T, we make the same inferences as for a homomorphic mapped type
+ // { [P in keyof T]: X }. This enables us to make meaningful inferences when the target is a
+ // Pick.
+ var extendedConstraint = getConstraintOfType(constraintType);
+ if (extendedConstraint && inferToMappedType(source, target, extendedConstraint)) {
+ return true;
+ }
+ // If no inferences can be made to K's constraint, infer from a union of the property types
+ // in the source to the template type X.
+ var propTypes = ts.map(getPropertiesOfType(source), getTypeOfSymbol);
+ var stringIndexType = getIndexTypeOfType(source, 0 /* String */);
+ var numberIndexInfo = getNonEnumNumberIndexInfo(source);
+ var numberIndexType = numberIndexInfo && numberIndexInfo.type;
+ inferFromTypes(getUnionType(ts.append(ts.append(propTypes, stringIndexType), numberIndexType)), getTemplateTypeFromMappedType(target));
+ return true;
}
- var links = getNodeLinks(iife);
- var cached = links.resolvedSignature;
- links.resolvedSignature = anySignature;
- var type = indexOfParameter < args.length ?
- getWidenedLiteralType(checkExpression(args[indexOfParameter])) :
- parameter.initializer ? undefined : undefinedWideningType;
- links.resolvedSignature = cached;
- return type;
- }
- var contextualSignature = getContextualSignature(func);
- if (contextualSignature) {
- var index = func.parameters.indexOf(parameter) - (ts.getThisParameter(func) ? 1 : 0);
- return parameter.dotDotDotToken && ts.lastOrUndefined(func.parameters) === parameter ?
- getRestTypeAtPosition(contextualSignature, index) :
- tryGetTypeAtPosition(contextualSignature, index);
- }
- }
- function getContextualTypeForVariableLikeDeclaration(declaration) {
- var typeNode = ts.getEffectiveTypeAnnotationNode(declaration);
- if (typeNode) {
- return getTypeFromTypeNode(typeNode);
+ return false;
}
- switch (declaration.kind) {
- case 156 /* Parameter */:
- return getContextuallyTypedParameterType(declaration);
- case 191 /* BindingElement */:
- return getContextualTypeForBindingElement(declaration);
- // By default, do nothing and return undefined - only parameters and binding elements have context implied by a parent
+ function inferToConditionalType(source, target) {
+ if (source.flags & 16777216 /* Conditional */) {
+ inferFromTypes(source.checkType, target.checkType);
+ inferFromTypes(source.extendsType, target.extendsType);
+ inferFromTypes(getTrueTypeFromConditionalType(source), getTrueTypeFromConditionalType(target));
+ inferFromTypes(getFalseTypeFromConditionalType(source), getFalseTypeFromConditionalType(target));
+ }
+ else {
+ var savePriority = priority;
+ priority |= contravariant ? 32 /* ContravariantConditional */ : 0;
+ var targetTypes = [getTrueTypeFromConditionalType(target), getFalseTypeFromConditionalType(target)];
+ inferToMultipleTypes(source, targetTypes, target.flags);
+ priority = savePriority;
+ }
}
- }
- function getContextualTypeForBindingElement(declaration) {
- var parent = declaration.parent.parent;
- var name = declaration.propertyName || declaration.name;
- var parentType = getContextualTypeForVariableLikeDeclaration(parent) ||
- parent.kind !== 191 /* BindingElement */ && parent.initializer && checkDeclarationInitializer(parent);
- if (parentType && !ts.isBindingPattern(name) && !ts.isComputedNonLiteralName(name)) {
- var nameType = getLiteralTypeFromPropertyName(name);
- if (isTypeUsableAsPropertyName(nameType)) {
- var text = getPropertyNameFromType(nameType);
- return getTypeOfPropertyOfType(parentType, text);
+ function inferToTemplateLiteralType(source, target) {
+ var matches = source.flags & 128 /* StringLiteral */ ? inferLiteralsFromTemplateLiteralType(source, target) :
+ source.flags & 134217728 /* TemplateLiteral */ && ts.arraysEqual(source.texts, target.texts) ? source.types :
+ undefined;
+ var types = target.types;
+ for (var i = 0; i < types.length; i++) {
+ inferFromTypes(matches ? matches[i] : neverType, types[i]);
}
}
- }
- // In a variable, parameter or property declaration with a type annotation,
- // the contextual type of an initializer expression is the type of the variable, parameter or property.
- // Otherwise, in a parameter declaration of a contextually typed function expression,
- // the contextual type of an initializer expression is the contextual type of the parameter.
- // Otherwise, in a variable or parameter declaration with a binding pattern name,
- // the contextual type of an initializer expression is the type implied by the binding pattern.
- // Otherwise, in a binding pattern inside a variable or parameter declaration,
- // the contextual type of an initializer expression is the type annotation of the containing declaration, if present.
- function getContextualTypeForInitializerExpression(node) {
- var declaration = node.parent;
- if (ts.hasInitializer(declaration) && node === declaration.initializer) {
- var result = getContextualTypeForVariableLikeDeclaration(declaration);
- if (result) {
- return result;
+ function inferFromObjectTypes(source, target) {
+ if (ts.getObjectFlags(source) & 4 /* Reference */ && ts.getObjectFlags(target) & 4 /* Reference */ && (source.target === target.target || isArrayType(source) && isArrayType(target))) {
+ // If source and target are references to the same generic type, infer from type arguments
+ inferFromTypeArguments(getTypeArguments(source), getTypeArguments(target), getVariances(source.target));
+ return;
}
- if (ts.isBindingPattern(declaration.name)) { // This is less a contextual type and more an implied shape - in some cases, this may be undesirable
- return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false);
+ if (isGenericMappedType(source) && isGenericMappedType(target)) {
+ // The source and target types are generic types { [P in S]: X } and { [P in T]: Y }, so we infer
+ // from S to T and from X to Y.
+ inferFromTypes(getConstraintTypeFromMappedType(source), getConstraintTypeFromMappedType(target));
+ inferFromTypes(getTemplateTypeFromMappedType(source), getTemplateTypeFromMappedType(target));
+ var sourceNameType = getNameTypeFromMappedType(source);
+ var targetNameType = getNameTypeFromMappedType(target);
+ if (sourceNameType && targetNameType)
+ inferFromTypes(sourceNameType, targetNameType);
}
- }
- return undefined;
- }
- function getContextualTypeForReturnExpression(node) {
- var func = ts.getContainingFunction(node);
- if (func) {
- var functionFlags = ts.getFunctionFlags(func);
- if (functionFlags & 1 /* Generator */) { // AsyncGenerator function or Generator function
- return undefined;
+ if (ts.getObjectFlags(target) & 32 /* Mapped */ && !target.declaration.nameType) {
+ var constraintType = getConstraintTypeFromMappedType(target);
+ if (inferToMappedType(source, target, constraintType)) {
+ return;
+ }
}
- var contextualReturnType = getContextualReturnType(func);
- if (contextualReturnType) {
- if (functionFlags & 2 /* Async */) { // Async function
- var contextualAwaitedType = mapType(contextualReturnType, getAwaitedTypeOfPromise);
- return contextualAwaitedType && getUnionType([contextualAwaitedType, createPromiseLikeType(contextualAwaitedType)]);
+ // Infer from the members of source and target only if the two types are possibly related
+ if (!typesDefinitelyUnrelated(source, target)) {
+ if (isArrayType(source) || isTupleType(source)) {
+ if (isTupleType(target)) {
+ var sourceArity = getTypeReferenceArity(source);
+ var targetArity = getTypeReferenceArity(target);
+ var elementTypes = getTypeArguments(target);
+ var elementFlags = target.target.elementFlags;
+ // When source and target are tuple types with the same structure (fixed, variadic, and rest are matched
+ // to the same kind in each position), simply infer between the element types.
+ if (isTupleType(source) && isTupleTypeStructureMatching(source, target)) {
+ for (var i = 0; i < targetArity; i++) {
+ inferFromTypes(getTypeArguments(source)[i], elementTypes[i]);
+ }
+ return;
+ }
+ var startLength = isTupleType(source) ? Math.min(source.target.fixedLength, target.target.fixedLength) : 0;
+ var sourceRestType = !isTupleType(source) || sourceArity > 0 && source.target.elementFlags[sourceArity - 1] & 4 /* Rest */ ?
+ getTypeArguments(source)[sourceArity - 1] : undefined;
+ var endLength = !(target.target.combinedFlags & 12 /* Variable */) ? 0 :
+ sourceRestType ? getEndLengthOfType(target) :
+ Math.min(getEndLengthOfType(source), getEndLengthOfType(target));
+ var sourceEndLength = sourceRestType ? 0 : endLength;
+ // Infer between starting fixed elements.
+ for (var i = 0; i < startLength; i++) {
+ inferFromTypes(getTypeArguments(source)[i], elementTypes[i]);
+ }
+ if (sourceRestType && sourceArity - startLength === 1) {
+ // Single rest element remains in source, infer from that to every element in target
+ for (var i = startLength; i < targetArity - endLength; i++) {
+ inferFromTypes(elementFlags[i] & 8 /* Variadic */ ? createArrayType(sourceRestType) : sourceRestType, elementTypes[i]);
+ }
+ }
+ else {
+ var middleLength = targetArity - startLength - endLength;
+ if (middleLength === 2 && elementFlags[startLength] & elementFlags[startLength + 1] & 8 /* Variadic */ && isTupleType(source)) {
+ // Middle of target is [...T, ...U] and source is tuple type
+ var targetInfo = getInferenceInfoForType(elementTypes[startLength]);
+ if (targetInfo && targetInfo.impliedArity !== undefined) {
+ // Infer slices from source based on implied arity of T.
+ inferFromTypes(sliceTupleType(source, startLength, sourceEndLength + sourceArity - targetInfo.impliedArity), elementTypes[startLength]);
+ inferFromTypes(sliceTupleType(source, startLength + targetInfo.impliedArity, sourceEndLength), elementTypes[startLength + 1]);
+ }
+ }
+ else if (middleLength === 1 && elementFlags[startLength] & 8 /* Variadic */) {
+ // Middle of target is exactly one variadic element. Infer the slice between the fixed parts in the source.
+ // If target ends in optional element(s), make a lower priority a speculative inference.
+ var endsInOptional = target.target.elementFlags[targetArity - 1] & 2 /* Optional */;
+ var sourceSlice = isTupleType(source) ? sliceTupleType(source, startLength, sourceEndLength) : createArrayType(sourceRestType);
+ inferWithPriority(sourceSlice, elementTypes[startLength], endsInOptional ? 2 /* SpeculativeTuple */ : 0);
+ }
+ else if (middleLength === 1 && elementFlags[startLength] & 4 /* Rest */) {
+ // Middle of target is exactly one rest element. If middle of source is not empty, infer union of middle element types.
+ var restType = isTupleType(source) ? getElementTypeOfSliceOfTupleType(source, startLength, sourceEndLength) : sourceRestType;
+ if (restType) {
+ inferFromTypes(restType, elementTypes[startLength]);
+ }
+ }
+ }
+ // Infer between ending fixed elements
+ for (var i = 0; i < endLength; i++) {
+ inferFromTypes(sourceRestType || getTypeArguments(source)[sourceArity - i - 1], elementTypes[targetArity - i - 1]);
+ }
+ return;
+ }
+ if (isArrayType(target)) {
+ inferFromIndexTypes(source, target);
+ return;
+ }
+ }
+ inferFromProperties(source, target);
+ inferFromSignatures(source, target, 0 /* Call */);
+ inferFromSignatures(source, target, 1 /* Construct */);
+ inferFromIndexTypes(source, target);
+ }
+ }
+ function inferFromProperties(source, target) {
+ var properties = getPropertiesOfObjectType(target);
+ for (var _i = 0, properties_3 = properties; _i < properties_3.length; _i++) {
+ var targetProp = properties_3[_i];
+ var sourceProp = getPropertyOfType(source, targetProp.escapedName);
+ if (sourceProp) {
+ inferFromTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp));
}
- return contextualReturnType; // Regular function
}
}
- return undefined;
- }
- function getContextualTypeForAwaitOperand(node) {
- var contextualType = getContextualType(node);
- if (contextualType) {
- var contextualAwaitedType = getAwaitedType(contextualType);
- return contextualAwaitedType && getUnionType([contextualAwaitedType, createPromiseLikeType(contextualAwaitedType)]);
+ function inferFromSignatures(source, target, kind) {
+ var sourceSignatures = getSignaturesOfType(source, kind);
+ var targetSignatures = getSignaturesOfType(target, kind);
+ var sourceLen = sourceSignatures.length;
+ var targetLen = targetSignatures.length;
+ var len = sourceLen < targetLen ? sourceLen : targetLen;
+ var skipParameters = !!(ts.getObjectFlags(source) & 2097152 /* NonInferrableType */);
+ for (var i = 0; i < len; i++) {
+ inferFromSignature(getBaseSignature(sourceSignatures[sourceLen - len + i]), getErasedSignature(targetSignatures[targetLen - len + i]), skipParameters);
+ }
}
- return undefined;
- }
- function getContextualTypeForYieldOperand(node) {
- var func = ts.getContainingFunction(node);
- if (func) {
- var functionFlags = ts.getFunctionFlags(func);
- var contextualReturnType = getContextualReturnType(func);
- if (contextualReturnType) {
- return node.asteriskToken
- ? contextualReturnType
- : getIterationTypeOfGeneratorFunctionReturnType(0 /* Yield */, contextualReturnType, (functionFlags & 2 /* Async */) !== 0);
+ function inferFromSignature(source, target, skipParameters) {
+ if (!skipParameters) {
+ var saveBivariant = bivariant;
+ var kind = target.declaration ? target.declaration.kind : 0 /* Unknown */;
+ // Once we descend into a bivariant signature we remain bivariant for all nested inferences
+ bivariant = bivariant || kind === 165 /* MethodDeclaration */ || kind === 164 /* MethodSignature */ || kind === 166 /* Constructor */;
+ applyToParameterTypes(source, target, inferFromContravariantTypes);
+ bivariant = saveBivariant;
}
+ applyToReturnTypes(source, target, inferFromTypes);
}
- return undefined;
- }
- function isInParameterInitializerBeforeContainingFunction(node) {
- var inBindingInitializer = false;
- while (node.parent && !ts.isFunctionLike(node.parent)) {
- if (ts.isParameter(node.parent) && (inBindingInitializer || node.parent.initializer === node)) {
- return true;
+ function inferFromIndexTypes(source, target) {
+ var targetStringIndexType = getIndexTypeOfType(target, 0 /* String */);
+ if (targetStringIndexType) {
+ var sourceIndexType = getIndexTypeOfType(source, 0 /* String */) ||
+ getImplicitIndexTypeOfType(source, 0 /* String */);
+ if (sourceIndexType) {
+ inferFromTypes(sourceIndexType, targetStringIndexType);
+ }
}
- if (ts.isBindingElement(node.parent) && node.parent.initializer === node) {
- inBindingInitializer = true;
+ var targetNumberIndexType = getIndexTypeOfType(target, 1 /* Number */);
+ if (targetNumberIndexType) {
+ var sourceIndexType = getIndexTypeOfType(source, 1 /* Number */) ||
+ getIndexTypeOfType(source, 0 /* String */) ||
+ getImplicitIndexTypeOfType(source, 1 /* Number */);
+ if (sourceIndexType) {
+ inferFromTypes(sourceIndexType, targetNumberIndexType);
+ }
}
- node = node.parent;
}
- return false;
}
- function getContextualIterationType(kind, functionDecl) {
- var isAsync = !!(ts.getFunctionFlags(functionDecl) & 2 /* Async */);
- var contextualReturnType = getContextualReturnType(functionDecl);
- if (contextualReturnType) {
- return getIterationTypeOfGeneratorFunctionReturnType(kind, contextualReturnType, isAsync)
- || undefined;
- }
- return undefined;
+ function isTypeOrBaseIdenticalTo(s, t) {
+ return isTypeIdenticalTo(s, t) || !!(t.flags & 4 /* String */ && s.flags & 128 /* StringLiteral */ || t.flags & 8 /* Number */ && s.flags & 256 /* NumberLiteral */);
}
- function getContextualReturnType(functionDecl) {
- // If the containing function has a return type annotation, is a constructor, or is a get accessor whose
- // corresponding set accessor has a type annotation, return statements in the function are contextually typed
- var returnType = getReturnTypeFromAnnotation(functionDecl);
- if (returnType) {
- return returnType;
- }
- // Otherwise, if the containing function is contextually typed by a function type with exactly one call signature
- // and that call signature is non-generic, return statements are contextually typed by the return type of the signature
- var signature = getContextualSignatureForFunctionLikeDeclaration(functionDecl);
- if (signature && !isResolvingReturnTypeOfSignature(signature)) {
- return getReturnTypeOfSignature(signature);
- }
- return undefined;
+ function isTypeCloselyMatchedBy(s, t) {
+ return !!(s.flags & 524288 /* Object */ && t.flags & 524288 /* Object */ && s.symbol && s.symbol === t.symbol ||
+ s.aliasSymbol && s.aliasTypeArguments && s.aliasSymbol === t.aliasSymbol);
}
- // In a typed function call, an argument or substitution expression is contextually typed by the type of the corresponding parameter.
- function getContextualTypeForArgument(callTarget, arg) {
- var args = getEffectiveCallArguments(callTarget);
- var argIndex = args.indexOf(arg); // -1 for e.g. the expression of a CallExpression, or the tag of a TaggedTemplateExpression
- return argIndex === -1 ? undefined : getContextualTypeForArgumentAtIndex(callTarget, argIndex);
+ function hasPrimitiveConstraint(type) {
+ var constraint = getConstraintOfTypeParameter(type);
+ return !!constraint && maybeTypeOfKind(constraint.flags & 16777216 /* Conditional */ ? getDefaultConstraintOfConditionalType(constraint) : constraint, 131068 /* Primitive */ | 4194304 /* Index */ | 134217728 /* TemplateLiteral */ | 268435456 /* StringMapping */);
}
- function getContextualTypeForArgumentAtIndex(callTarget, argIndex) {
- // If we're already in the process of resolving the given signature, don't resolve again as
- // that could cause infinite recursion. Instead, return anySignature.
- var signature = getNodeLinks(callTarget).resolvedSignature === resolvingSignature ? resolvingSignature : getResolvedSignature(callTarget);
- if (ts.isJsxOpeningLikeElement(callTarget) && argIndex === 0) {
- return getEffectiveFirstArgumentForJsxSignature(signature, callTarget);
- }
- return getTypeAtPosition(signature, argIndex);
+ function isObjectLiteralType(type) {
+ return !!(ts.getObjectFlags(type) & 128 /* ObjectLiteral */);
}
- function getContextualTypeForSubstitutionExpression(template, substitutionExpression) {
- if (template.parent.kind === 198 /* TaggedTemplateExpression */) {
- return getContextualTypeForArgument(template.parent, substitutionExpression);
- }
- return undefined;
+ function isObjectOrArrayLiteralType(type) {
+ return !!(ts.getObjectFlags(type) & (128 /* ObjectLiteral */ | 65536 /* ArrayLiteral */));
}
- function getContextualTypeForBinaryOperand(node, contextFlags) {
- var binaryExpression = node.parent;
- var left = binaryExpression.left, operatorToken = binaryExpression.operatorToken, right = binaryExpression.right;
- switch (operatorToken.kind) {
- case 62 /* EqualsToken */:
- if (node !== right) {
- return undefined;
- }
- var contextSensitive = getIsContextSensitiveAssignmentOrContextType(binaryExpression);
- if (!contextSensitive) {
- return undefined;
- }
- return contextSensitive === true ? getTypeOfExpression(left) : contextSensitive;
- case 56 /* BarBarToken */:
- case 60 /* QuestionQuestionToken */:
- // When an || expression has a contextual type, the operands are contextually typed by that type, except
- // when that type originates in a binding pattern, the right operand is contextually typed by the type of
- // the left operand. When an || expression has no contextual type, the right operand is contextually typed
- // by the type of the left operand, except for the special case of Javascript declarations of the form
- // `namespace.prop = namespace.prop || {}`.
- var type = getContextualType(binaryExpression, contextFlags);
- return node === right && (type && type.pattern || !type && !ts.isDefaultedExpandoInitializer(binaryExpression)) ?
- getTypeOfExpression(left) : type;
- case 55 /* AmpersandAmpersandToken */:
- case 27 /* CommaToken */:
- return node === right ? getContextualType(binaryExpression, contextFlags) : undefined;
- default:
- return undefined;
+ function unionObjectAndArrayLiteralCandidates(candidates) {
+ if (candidates.length > 1) {
+ var objectLiterals = ts.filter(candidates, isObjectOrArrayLiteralType);
+ if (objectLiterals.length) {
+ var literalsType = getUnionType(objectLiterals, 2 /* Subtype */);
+ return ts.concatenate(ts.filter(candidates, function (t) { return !isObjectOrArrayLiteralType(t); }), [literalsType]);
+ }
}
+ return candidates;
}
- // In an assignment expression, the right operand is contextually typed by the type of the left operand.
- // Don't do this for assignment declarations unless there is a type tag on the assignment, to avoid circularity from checking the right operand.
- function getIsContextSensitiveAssignmentOrContextType(binaryExpression) {
- var kind = ts.getAssignmentDeclarationKind(binaryExpression);
- switch (kind) {
- case 0 /* None */:
- return true;
- case 5 /* Property */:
- case 1 /* ExportsProperty */:
- case 6 /* Prototype */:
- case 3 /* PrototypeProperty */:
- // If `binaryExpression.left` was assigned a symbol, then this is a new declaration; otherwise it is an assignment to an existing declaration.
- // See `bindStaticPropertyAssignment` in `binder.ts`.
- if (!binaryExpression.left.symbol) {
- return true;
- }
- else {
- var decl = binaryExpression.left.symbol.valueDeclaration;
- if (!decl) {
- return false;
- }
- var lhs = ts.cast(binaryExpression.left, ts.isAccessExpression);
- var overallAnnotation = ts.getEffectiveTypeAnnotationNode(decl);
- if (overallAnnotation) {
- return getTypeFromTypeNode(overallAnnotation);
- }
- else if (ts.isIdentifier(lhs.expression)) {
- var id = lhs.expression;
- var parentSymbol = resolveName(id, id.escapedText, 111551 /* Value */, undefined, id.escapedText, /*isUse*/ true);
- if (parentSymbol) {
- var annotated = ts.getEffectiveTypeAnnotationNode(parentSymbol.valueDeclaration);
- if (annotated) {
- var nameStr_1 = ts.getElementOrPropertyAccessName(lhs);
- if (nameStr_1 !== undefined) {
- var type = getTypeOfPropertyOfContextualType(getTypeFromTypeNode(annotated), nameStr_1);
- return type || false;
- }
- }
- return false;
- }
- }
- return !ts.isInJSFile(decl);
- }
- case 2 /* ModuleExports */:
- case 4 /* ThisProperty */:
- if (!binaryExpression.symbol)
- return true;
- if (binaryExpression.symbol.valueDeclaration) {
- var annotated = ts.getEffectiveTypeAnnotationNode(binaryExpression.symbol.valueDeclaration);
- if (annotated) {
- var type = getTypeFromTypeNode(annotated);
- if (type) {
- return type;
- }
- }
- }
- if (kind === 2 /* ModuleExports */)
- return false;
- var thisAccess = ts.cast(binaryExpression.left, ts.isAccessExpression);
- if (!ts.isObjectLiteralMethod(ts.getThisContainer(thisAccess.expression, /*includeArrowFunctions*/ false))) {
- return false;
- }
- var thisType = checkThisExpression(thisAccess.expression);
- var nameStr = ts.getElementOrPropertyAccessName(thisAccess);
- return nameStr !== undefined && thisType && getTypeOfPropertyOfContextualType(thisType, nameStr) || false;
- case 7 /* ObjectDefinePropertyValue */:
- case 8 /* ObjectDefinePropertyExports */:
- case 9 /* ObjectDefinePrototypeProperty */:
- return ts.Debug.fail("Does not apply");
- default:
- return ts.Debug.assertNever(kind);
- }
+ function getContravariantInference(inference) {
+ return inference.priority & 208 /* PriorityImpliesCombination */ ? getIntersectionType(inference.contraCandidates) : getCommonSubtype(inference.contraCandidates);
}
- function getTypeOfPropertyOfContextualType(type, name) {
- return mapType(type, function (t) {
- if (isGenericMappedType(t)) {
- var constraint = getConstraintTypeFromMappedType(t);
- var constraintOfConstraint = getBaseConstraintOfType(constraint) || constraint;
- var propertyNameType = getLiteralType(ts.unescapeLeadingUnderscores(name));
- if (isTypeAssignableTo(propertyNameType, constraintOfConstraint)) {
- return substituteIndexedMappedType(t, propertyNameType);
+ function getCovariantInference(inference, signature) {
+ // Extract all object and array literal types and replace them with a single widened and normalized type.
+ var candidates = unionObjectAndArrayLiteralCandidates(inference.candidates);
+ // We widen inferred literal types if
+ // all inferences were made to top-level occurrences of the type parameter, and
+ // the type parameter has no constraint or its constraint includes no primitive or literal types, and
+ // the type parameter was fixed during inference or does not occur at top-level in the return type.
+ var primitiveConstraint = hasPrimitiveConstraint(inference.typeParameter);
+ var widenLiteralTypes = !primitiveConstraint && inference.topLevel &&
+ (inference.isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), inference.typeParameter));
+ var baseCandidates = primitiveConstraint ? ts.sameMap(candidates, getRegularTypeOfLiteralType) :
+ widenLiteralTypes ? ts.sameMap(candidates, getWidenedLiteralType) :
+ candidates;
+ // If all inferences were made from a position that implies a combined result, infer a union type.
+ // Otherwise, infer a common supertype.
+ var unwidenedType = inference.priority & 208 /* PriorityImpliesCombination */ ?
+ getUnionType(baseCandidates, 2 /* Subtype */) :
+ getCommonSupertype(baseCandidates);
+ return getWidenedType(unwidenedType);
+ }
+ function getInferredType(context, index) {
+ var inference = context.inferences[index];
+ if (!inference.inferredType) {
+ var inferredType = void 0;
+ var signature = context.signature;
+ if (signature) {
+ var inferredCovariantType = inference.candidates ? getCovariantInference(inference, signature) : undefined;
+ if (inference.contraCandidates) {
+ var inferredContravariantType = getContravariantInference(inference);
+ // If we have both co- and contra-variant inferences, we prefer the contra-variant inference
+ // unless the co-variant inference is a subtype and not 'never'.
+ inferredType = inferredCovariantType && !(inferredCovariantType.flags & 131072 /* Never */) &&
+ isTypeSubtypeOf(inferredCovariantType, inferredContravariantType) ?
+ inferredCovariantType : inferredContravariantType;
}
- }
- else if (t.flags & 3670016 /* StructuredType */) {
- var prop = getPropertyOfType(t, name);
- if (prop) {
- return getTypeOfSymbol(prop);
+ else if (inferredCovariantType) {
+ inferredType = inferredCovariantType;
+ }
+ else if (context.flags & 1 /* NoDefault */) {
+ // We use silentNeverType as the wildcard that signals no inferences.
+ inferredType = silentNeverType;
}
- if (isTupleType(t)) {
- var restType = getRestTypeOfTupleType(t);
- if (restType && isNumericLiteralName(name) && +name >= 0) {
- return restType;
+ else {
+ // Infer either the default or the empty object type when no inferences were
+ // made. It is important to remember that in this case, inference still
+ // succeeds, meaning there is no error for not having inference candidates. An
+ // inference error only occurs when there are *conflicting* candidates, i.e.
+ // candidates with no common supertype.
+ var defaultType = getDefaultFromTypeParameter(inference.typeParameter);
+ if (defaultType) {
+ // Instantiate the default type. Any forward reference to a type
+ // parameter should be instantiated to the empty object type.
+ inferredType = instantiateType(defaultType, mergeTypeMappers(createBackreferenceMapper(context, index), context.nonFixingMapper));
}
}
- return isNumericLiteralName(name) && getIndexTypeOfContextualType(t, 1 /* Number */) ||
- getIndexTypeOfContextualType(t, 0 /* String */);
}
- return undefined;
- }, /*noReductions*/ true);
+ else {
+ inferredType = getTypeFromInference(inference);
+ }
+ inference.inferredType = inferredType || getDefaultTypeArgumentType(!!(context.flags & 2 /* AnyDefault */));
+ var constraint = getConstraintOfTypeParameter(inference.typeParameter);
+ if (constraint) {
+ var instantiatedConstraint = instantiateType(constraint, context.nonFixingMapper);
+ if (!inferredType || !context.compareTypes(inferredType, getTypeWithThisArgument(instantiatedConstraint, inferredType))) {
+ inference.inferredType = inferredType = instantiatedConstraint;
+ }
+ }
+ }
+ return inference.inferredType;
}
- function getIndexTypeOfContextualType(type, kind) {
- return mapType(type, function (t) { return getIndexTypeOfStructuredType(t, kind); }, /*noReductions*/ true);
+ function getDefaultTypeArgumentType(isInJavaScriptFile) {
+ return isInJavaScriptFile ? anyType : unknownType;
}
- // In an object literal contextually typed by a type T, the contextual type of a property assignment is the type of
- // the matching property in T, if one exists. Otherwise, it is the type of the numeric index signature in T, if one
- // exists. Otherwise, it is the type of the string index signature in T, if one exists.
- function getContextualTypeForObjectLiteralMethod(node, contextFlags) {
- ts.Debug.assert(ts.isObjectLiteralMethod(node));
- if (node.flags & 16777216 /* InWithStatement */) {
- // We cannot answer semantic questions within a with block, do not proceed any further
- return undefined;
+ function getInferredTypes(context) {
+ var result = [];
+ for (var i = 0; i < context.inferences.length; i++) {
+ result.push(getInferredType(context, i));
}
- return getContextualTypeForObjectLiteralElement(node, contextFlags);
+ return result;
}
- function getContextualTypeForObjectLiteralElement(element, contextFlags) {
- var objectLiteral = element.parent;
- var type = getApparentTypeOfContextualType(objectLiteral, contextFlags);
- if (type) {
- if (!hasNonBindableDynamicName(element)) {
- // For a (non-symbol) computed property, there is no reason to look up the name
- // in the type. It will just be "__computed", which does not appear in any
- // SymbolTable.
- var symbolName_3 = getSymbolOfNode(element).escapedName;
- var propertyType = getTypeOfPropertyOfContextualType(type, symbolName_3);
- if (propertyType) {
- return propertyType;
+ // EXPRESSION TYPE CHECKING
+ function getCannotFindNameDiagnosticForName(node) {
+ switch (node.escapedText) {
+ case "document":
+ case "console":
+ return ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_include_dom;
+ case "$":
+ return compilerOptions.types
+ ? ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_save_dev_types_Slashjquery_and_then_add_jquery_to_the_types_field_in_your_tsconfig
+ : ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_save_dev_types_Slashjquery;
+ case "describe":
+ case "suite":
+ case "it":
+ case "test":
+ return compilerOptions.types
+ ? ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_a_test_runner_Try_npm_i_save_dev_types_Slashjest_or_npm_i_save_dev_types_Slashmocha_and_then_add_jest_or_mocha_to_the_types_field_in_your_tsconfig
+ : ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_a_test_runner_Try_npm_i_save_dev_types_Slashjest_or_npm_i_save_dev_types_Slashmocha;
+ case "process":
+ case "require":
+ case "Buffer":
+ case "module":
+ return compilerOptions.types
+ ? ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode_and_then_add_node_to_the_types_field_in_your_tsconfig
+ : ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode;
+ case "Map":
+ case "Set":
+ case "Promise":
+ case "Symbol":
+ case "WeakMap":
+ case "WeakSet":
+ case "Iterator":
+ case "AsyncIterator":
+ case "SharedArrayBuffer":
+ case "Atomics":
+ case "AsyncIterable":
+ case "AsyncIterableIterator":
+ case "AsyncGenerator":
+ case "AsyncGeneratorFunction":
+ case "BigInt":
+ case "Reflect":
+ case "BigInt64Array":
+ case "BigUint64Array":
+ return ts.Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_1_or_later;
+ default:
+ if (node.parent.kind === 289 /* ShorthandPropertyAssignment */) {
+ return ts.Diagnostics.No_value_exists_in_scope_for_the_shorthand_property_0_Either_declare_one_or_provide_an_initializer;
+ }
+ else {
+ return ts.Diagnostics.Cannot_find_name_0;
}
- }
- return isNumericName(element.name) && getIndexTypeOfContextualType(type, 1 /* Number */) ||
- getIndexTypeOfContextualType(type, 0 /* String */);
}
- return undefined;
}
- // In an array literal contextually typed by a type T, the contextual type of an element expression at index N is
- // the type of the property with the numeric name N in T, if one exists. Otherwise, if T has a numeric index signature,
- // it is the type of the numeric index signature in T. Otherwise, in ES6 and higher, the contextual type is the iterated
- // type of T.
- function getContextualTypeForElementExpression(arrayContextualType, index) {
- return arrayContextualType && (getTypeOfPropertyOfContextualType(arrayContextualType, "" + index)
- || getIteratedTypeOrElementType(1 /* Element */, arrayContextualType, undefinedType, /*errorNode*/ undefined, /*checkAssignability*/ false));
+ function getResolvedSymbol(node) {
+ var links = getNodeLinks(node);
+ if (!links.resolvedSymbol) {
+ links.resolvedSymbol = !ts.nodeIsMissing(node) &&
+ resolveName(node, node.escapedText, 111551 /* Value */ | 1048576 /* ExportValue */, getCannotFindNameDiagnosticForName(node), node, !ts.isWriteOnlyAccess(node),
+ /*excludeGlobals*/ false, ts.Diagnostics.Cannot_find_name_0_Did_you_mean_1) || unknownSymbol;
+ }
+ return links.resolvedSymbol;
}
- // In a contextually typed conditional expression, the true/false expressions are contextually typed by the same type.
- function getContextualTypeForConditionalOperand(node, contextFlags) {
- var conditional = node.parent;
- return node === conditional.whenTrue || node === conditional.whenFalse ? getContextualType(conditional, contextFlags) : undefined;
+ function isInTypeQuery(node) {
+ // TypeScript 1.0 spec (April 2014): 3.6.3
+ // A type query consists of the keyword typeof followed by an expression.
+ // The expression is restricted to a single identifier or a sequence of identifiers separated by periods
+ return !!ts.findAncestor(node, function (n) { return n.kind === 176 /* TypeQuery */ ? true : n.kind === 78 /* Identifier */ || n.kind === 157 /* QualifiedName */ ? false : "quit"; });
}
- function getContextualTypeForChildJsxExpression(node, child) {
- var attributesType = getApparentTypeOfContextualType(node.openingElement.tagName);
- // JSX expression is in children of JSX Element, we will look for an "children" atttribute (we get the name from JSX.ElementAttributesProperty)
- var jsxChildrenPropertyName = getJsxElementChildrenPropertyName(getJsxNamespaceAt(node));
- if (!(attributesType && !isTypeAny(attributesType) && jsxChildrenPropertyName && jsxChildrenPropertyName !== "")) {
- return undefined;
+ // Return the flow cache key for a "dotted name" (i.e. a sequence of identifiers
+ // separated by dots). The key consists of the id of the symbol referenced by the
+ // leftmost identifier followed by zero or more property names separated by dots.
+ // The result is undefined if the reference isn't a dotted name. We prefix nodes
+ // occurring in an apparent type position with '@' because the control flow type
+ // of such nodes may be based on the apparent type instead of the declared type.
+ function getFlowCacheKey(node, declaredType, initialType, flowContainer) {
+ switch (node.kind) {
+ case 78 /* Identifier */:
+ var symbol = getResolvedSymbol(node);
+ return symbol !== unknownSymbol ? (flowContainer ? getNodeId(flowContainer) : "-1") + "|" + getTypeId(declaredType) + "|" + getTypeId(initialType) + "|" + (isConstraintPosition(node) ? "@" : "") + getSymbolId(symbol) : undefined;
+ case 107 /* ThisKeyword */:
+ return "0|" + (flowContainer ? getNodeId(flowContainer) : "-1") + "|" + getTypeId(declaredType) + "|" + getTypeId(initialType);
+ case 225 /* NonNullExpression */:
+ case 207 /* ParenthesizedExpression */:
+ return getFlowCacheKey(node.expression, declaredType, initialType, flowContainer);
+ case 201 /* PropertyAccessExpression */:
+ case 202 /* ElementAccessExpression */:
+ var propName = getAccessedPropertyName(node);
+ if (propName !== undefined) {
+ var key = getFlowCacheKey(node.expression, declaredType, initialType, flowContainer);
+ return key && key + "." + propName;
+ }
}
- var realChildren = getSemanticJsxChildren(node.children);
- var childIndex = realChildren.indexOf(child);
- var childFieldType = getTypeOfPropertyOfContextualType(attributesType, jsxChildrenPropertyName);
- return childFieldType && (realChildren.length === 1 ? childFieldType : mapType(childFieldType, function (t) {
- if (isArrayLikeType(t)) {
- return getIndexedAccessType(t, getLiteralType(childIndex));
- }
- else {
- return t;
- }
- }, /*noReductions*/ true));
- }
- function getContextualTypeForJsxExpression(node) {
- var exprParent = node.parent;
- return ts.isJsxAttributeLike(exprParent)
- ? getContextualType(node)
- : ts.isJsxElement(exprParent)
- ? getContextualTypeForChildJsxExpression(exprParent, node)
- : undefined;
+ return undefined;
}
- function getContextualTypeForJsxAttribute(attribute) {
- // When we trying to resolve JsxOpeningLikeElement as a stateless function element, we will already give its attributes a contextual type
- // which is a type of the parameter of the signature we are trying out.
- // If there is no contextual type (e.g. we are trying to resolve stateful component), get attributes type from resolving element's tagName
- if (ts.isJsxAttribute(attribute)) {
- var attributesType = getApparentTypeOfContextualType(attribute.parent);
- if (!attributesType || isTypeAny(attributesType)) {
- return undefined;
- }
- return getTypeOfPropertyOfContextualType(attributesType, attribute.name.escapedText);
+ function isMatchingReference(source, target) {
+ switch (target.kind) {
+ case 207 /* ParenthesizedExpression */:
+ case 225 /* NonNullExpression */:
+ return isMatchingReference(source, target.expression);
+ case 216 /* BinaryExpression */:
+ return (ts.isAssignmentExpression(target) && isMatchingReference(source, target.left)) ||
+ (ts.isBinaryExpression(target) && target.operatorToken.kind === 27 /* CommaToken */ && isMatchingReference(source, target.right));
}
- else {
- return getContextualType(attribute.parent);
+ switch (source.kind) {
+ case 78 /* Identifier */:
+ case 79 /* PrivateIdentifier */:
+ return target.kind === 78 /* Identifier */ && getResolvedSymbol(source) === getResolvedSymbol(target) ||
+ (target.kind === 249 /* VariableDeclaration */ || target.kind === 198 /* BindingElement */) &&
+ getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(source)) === getSymbolOfNode(target);
+ case 107 /* ThisKeyword */:
+ return target.kind === 107 /* ThisKeyword */;
+ case 105 /* SuperKeyword */:
+ return target.kind === 105 /* SuperKeyword */;
+ case 225 /* NonNullExpression */:
+ case 207 /* ParenthesizedExpression */:
+ return isMatchingReference(source.expression, target);
+ case 201 /* PropertyAccessExpression */:
+ case 202 /* ElementAccessExpression */:
+ return ts.isAccessExpression(target) &&
+ getAccessedPropertyName(source) === getAccessedPropertyName(target) &&
+ isMatchingReference(source.expression, target.expression);
}
+ return false;
}
- // Return true if the given expression is possibly a discriminant value. We limit the kinds of
- // expressions we check to those that don't depend on their contextual type in order not to cause
- // recursive (and possibly infinite) invocations of getContextualType.
- function isPossiblyDiscriminantValue(node) {
- switch (node.kind) {
- case 10 /* StringLiteral */:
- case 8 /* NumericLiteral */:
- case 9 /* BigIntLiteral */:
- case 14 /* NoSubstitutionTemplateLiteral */:
- case 106 /* TrueKeyword */:
- case 91 /* FalseKeyword */:
- case 100 /* NullKeyword */:
- case 75 /* Identifier */:
- case 146 /* UndefinedKeyword */:
+ // Given a source x, check if target matches x or is an && operation with an operand that matches x.
+ function containsTruthyCheck(source, target) {
+ return isMatchingReference(source, target) ||
+ (target.kind === 216 /* BinaryExpression */ && target.operatorToken.kind === 55 /* AmpersandAmpersandToken */ &&
+ (containsTruthyCheck(source, target.left) || containsTruthyCheck(source, target.right)));
+ }
+ function getAccessedPropertyName(access) {
+ return access.kind === 201 /* PropertyAccessExpression */ ? access.name.escapedText :
+ ts.isStringOrNumericLiteralLike(access.argumentExpression) ? ts.escapeLeadingUnderscores(access.argumentExpression.text) :
+ undefined;
+ }
+ function containsMatchingReference(source, target) {
+ while (ts.isAccessExpression(source)) {
+ source = source.expression;
+ if (isMatchingReference(source, target)) {
return true;
- case 194 /* PropertyAccessExpression */:
- case 200 /* ParenthesizedExpression */:
- return isPossiblyDiscriminantValue(node.expression);
- case 276 /* JsxExpression */:
- return !node.expression || isPossiblyDiscriminantValue(node.expression);
+ }
}
return false;
}
- function discriminateContextualTypeByObjectMembers(node, contextualType) {
- return discriminateTypeByDiscriminableItems(contextualType, ts.map(ts.filter(node.properties, function (p) { return !!p.symbol && p.kind === 281 /* PropertyAssignment */ && isPossiblyDiscriminantValue(p.initializer) && isDiscriminantProperty(contextualType, p.symbol.escapedName); }), function (prop) { return [function () { return checkExpression(prop.initializer); }, prop.symbol.escapedName]; }), isTypeAssignableTo, contextualType);
- }
- function discriminateContextualTypeByJSXAttributes(node, contextualType) {
- return discriminateTypeByDiscriminableItems(contextualType, ts.map(ts.filter(node.properties, function (p) { return !!p.symbol && p.kind === 273 /* JsxAttribute */ && isDiscriminantProperty(contextualType, p.symbol.escapedName) && (!p.initializer || isPossiblyDiscriminantValue(p.initializer)); }), function (prop) { return [!prop.initializer ? (function () { return trueType; }) : (function () { return checkExpression(prop.initializer); }), prop.symbol.escapedName]; }), isTypeAssignableTo, contextualType);
+ function optionalChainContainsReference(source, target) {
+ while (ts.isOptionalChain(source)) {
+ source = source.expression;
+ if (isMatchingReference(source, target)) {
+ return true;
+ }
+ }
+ return false;
}
- // Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily
- // be "pushed" onto a node using the contextualType property.
- function getApparentTypeOfContextualType(node, contextFlags) {
- var contextualType = ts.isObjectLiteralMethod(node) ?
- getContextualTypeForObjectLiteralMethod(node, contextFlags) :
- getContextualType(node, contextFlags);
- var instantiatedType = instantiateContextualType(contextualType, node, contextFlags);
- if (instantiatedType && !(contextFlags && contextFlags & 2 /* NoConstraints */ && instantiatedType.flags & 8650752 /* TypeVariable */)) {
- var apparentType = mapType(instantiatedType, getApparentType, /*noReductions*/ true);
- if (apparentType.flags & 1048576 /* Union */) {
- if (ts.isObjectLiteralExpression(node)) {
- return discriminateContextualTypeByObjectMembers(node, apparentType);
- }
- else if (ts.isJsxAttributes(node)) {
- return discriminateContextualTypeByJSXAttributes(node, apparentType);
+ function isDiscriminantProperty(type, name) {
+ if (type && type.flags & 1048576 /* Union */) {
+ var prop = getUnionOrIntersectionProperty(type, name);
+ if (prop && ts.getCheckFlags(prop) & 2 /* SyntheticProperty */) {
+ if (prop.isDiscriminantProperty === undefined) {
+ prop.isDiscriminantProperty =
+ (prop.checkFlags & 192 /* Discriminant */) === 192 /* Discriminant */ &&
+ !maybeTypeOfKind(getTypeOfSymbol(prop), 465829888 /* Instantiable */);
}
+ return !!prop.isDiscriminantProperty;
}
- return apparentType;
}
+ return false;
}
- // If the given contextual type contains instantiable types and if a mapper representing
- // return type inferences is available, instantiate those types using that mapper.
- function instantiateContextualType(contextualType, node, contextFlags) {
- if (contextualType && maybeTypeOfKind(contextualType, 63176704 /* Instantiable */)) {
- var inferenceContext = getInferenceContext(node);
- // If no inferences have been made, nothing is gained from instantiating as type parameters
- // would just be replaced with their defaults similar to the apparent type.
- if (inferenceContext && ts.some(inferenceContext.inferences, hasInferenceCandidates)) {
- // For contextual signatures we incorporate all inferences made so far, e.g. from return
- // types as well as arguments to the left in a function call.
- if (contextFlags && contextFlags & 1 /* Signature */) {
- return instantiateInstantiableTypes(contextualType, inferenceContext.nonFixingMapper);
+ function findDiscriminantProperties(sourceProperties, target) {
+ var result;
+ for (var _i = 0, sourceProperties_2 = sourceProperties; _i < sourceProperties_2.length; _i++) {
+ var sourceProperty = sourceProperties_2[_i];
+ if (isDiscriminantProperty(target, sourceProperty.escapedName)) {
+ if (result) {
+ result.push(sourceProperty);
+ continue;
}
- // For other purposes (e.g. determining whether to produce literal types) we only
- // incorporate inferences made from the return type in a function call.
- if (inferenceContext.returnMapper) {
- return instantiateInstantiableTypes(contextualType, inferenceContext.returnMapper);
+ result = [sourceProperty];
+ }
+ }
+ return result;
+ }
+ function isOrContainsMatchingReference(source, target) {
+ return isMatchingReference(source, target) || containsMatchingReference(source, target);
+ }
+ function hasMatchingArgument(callExpression, reference) {
+ if (callExpression.arguments) {
+ for (var _i = 0, _a = callExpression.arguments; _i < _a.length; _i++) {
+ var argument = _a[_i];
+ if (isOrContainsMatchingReference(reference, argument)) {
+ return true;
}
}
}
- return contextualType;
+ if (callExpression.expression.kind === 201 /* PropertyAccessExpression */ &&
+ isOrContainsMatchingReference(reference, callExpression.expression.expression)) {
+ return true;
+ }
+ return false;
}
- // This function is similar to instantiateType, except that (a) it only instantiates types that
- // are classified as instantiable (i.e. it doesn't instantiate object types), and (b) it performs
- // no reductions on instantiated union types.
- function instantiateInstantiableTypes(type, mapper) {
- if (type.flags & 63176704 /* Instantiable */) {
- return instantiateType(type, mapper);
+ function getFlowNodeId(flow) {
+ if (!flow.id || flow.id < 0) {
+ flow.id = nextFlowId;
+ nextFlowId++;
}
- if (type.flags & 1048576 /* Union */) {
- return getUnionType(ts.map(type.types, function (t) { return instantiateInstantiableTypes(t, mapper); }), 0 /* None */);
+ return flow.id;
+ }
+ function typeMaybeAssignableTo(source, target) {
+ if (!(source.flags & 1048576 /* Union */)) {
+ return isTypeAssignableTo(source, target);
}
- if (type.flags & 2097152 /* Intersection */) {
- return getIntersectionType(ts.map(type.types, function (t) { return instantiateInstantiableTypes(t, mapper); }));
+ for (var _i = 0, _a = source.types; _i < _a.length; _i++) {
+ var t = _a[_i];
+ if (isTypeAssignableTo(t, target)) {
+ return true;
+ }
}
- return type;
+ return false;
}
- /**
- * Whoa! Do you really want to use this function?
- *
- * Unless you're trying to get the *non-apparent* type for a
- * value-literal type or you're authoring relevant portions of this algorithm,
- * you probably meant to use 'getApparentTypeOfContextualType'.
- * Otherwise this may not be very useful.
- *
- * In cases where you *are* working on this function, you should understand
- * when it is appropriate to use 'getContextualType' and 'getApparentTypeOfContextualType'.
- *
- * - Use 'getContextualType' when you are simply going to propagate the result to the expression.
- * - Use 'getApparentTypeOfContextualType' when you're going to need the members of the type.
- *
- * @param node the expression whose contextual type will be returned.
- * @returns the contextual type of an expression.
- */
- function getContextualType(node, contextFlags) {
- if (node.flags & 16777216 /* InWithStatement */) {
- // We cannot answer semantic questions within a with block, do not proceed any further
- return undefined;
+ // Remove those constituent types of declaredType to which no constituent type of assignedType is assignable.
+ // For example, when a variable of type number | string | boolean is assigned a value of type number | boolean,
+ // we remove type string.
+ function getAssignmentReducedType(declaredType, assignedType) {
+ if (declaredType !== assignedType) {
+ if (assignedType.flags & 131072 /* Never */) {
+ return assignedType;
+ }
+ var reducedType = filterType(declaredType, function (t) { return typeMaybeAssignableTo(assignedType, t); });
+ if (assignedType.flags & 512 /* BooleanLiteral */ && isFreshLiteralType(assignedType)) {
+ reducedType = mapType(reducedType, getFreshTypeOfLiteralType); // Ensure that if the assignment is a fresh type, that we narrow to fresh types
+ }
+ // Our crude heuristic produces an invalid result in some cases: see GH#26130.
+ // For now, when that happens, we give up and don't narrow at all. (This also
+ // means we'll never narrow for erroneous assignments where the assigned type
+ // is not assignable to the declared type.)
+ if (isTypeAssignableTo(assignedType, reducedType)) {
+ return reducedType;
+ }
}
- if (node.contextualType) {
- return node.contextualType;
+ return declaredType;
+ }
+ function getTypeFactsOfTypes(types) {
+ var result = 0 /* None */;
+ for (var _i = 0, types_16 = types; _i < types_16.length; _i++) {
+ var t = types_16[_i];
+ result |= getTypeFacts(t);
+ }
+ return result;
+ }
+ function isFunctionObjectType(type) {
+ // We do a quick check for a "bind" property before performing the more expensive subtype
+ // check. This gives us a quicker out in the common case where an object type is not a function.
+ var resolved = resolveStructuredTypeMembers(type);
+ return !!(resolved.callSignatures.length || resolved.constructSignatures.length ||
+ resolved.members.get("bind") && isTypeSubtypeOf(type, globalFunctionType));
+ }
+ function getTypeFacts(type) {
+ var flags = type.flags;
+ if (flags & 4 /* String */) {
+ return strictNullChecks ? 16317953 /* StringStrictFacts */ : 16776705 /* StringFacts */;
+ }
+ if (flags & 128 /* StringLiteral */) {
+ var isEmpty = type.value === "";
+ return strictNullChecks ?
+ isEmpty ? 12123649 /* EmptyStringStrictFacts */ : 7929345 /* NonEmptyStringStrictFacts */ :
+ isEmpty ? 12582401 /* EmptyStringFacts */ : 16776705 /* NonEmptyStringFacts */;
+ }
+ if (flags & (8 /* Number */ | 32 /* Enum */)) {
+ return strictNullChecks ? 16317698 /* NumberStrictFacts */ : 16776450 /* NumberFacts */;
+ }
+ if (flags & 256 /* NumberLiteral */) {
+ var isZero = type.value === 0;
+ return strictNullChecks ?
+ isZero ? 12123394 /* ZeroNumberStrictFacts */ : 7929090 /* NonZeroNumberStrictFacts */ :
+ isZero ? 12582146 /* ZeroNumberFacts */ : 16776450 /* NonZeroNumberFacts */;
+ }
+ if (flags & 64 /* BigInt */) {
+ return strictNullChecks ? 16317188 /* BigIntStrictFacts */ : 16775940 /* BigIntFacts */;
+ }
+ if (flags & 2048 /* BigIntLiteral */) {
+ var isZero = isZeroBigInt(type);
+ return strictNullChecks ?
+ isZero ? 12122884 /* ZeroBigIntStrictFacts */ : 7928580 /* NonZeroBigIntStrictFacts */ :
+ isZero ? 12581636 /* ZeroBigIntFacts */ : 16775940 /* NonZeroBigIntFacts */;
+ }
+ if (flags & 16 /* Boolean */) {
+ return strictNullChecks ? 16316168 /* BooleanStrictFacts */ : 16774920 /* BooleanFacts */;
+ }
+ if (flags & 528 /* BooleanLike */) {
+ return strictNullChecks ?
+ (type === falseType || type === regularFalseType) ? 12121864 /* FalseStrictFacts */ : 7927560 /* TrueStrictFacts */ :
+ (type === falseType || type === regularFalseType) ? 12580616 /* FalseFacts */ : 16774920 /* TrueFacts */;
+ }
+ if (flags & 524288 /* Object */) {
+ return ts.getObjectFlags(type) & 16 /* Anonymous */ && isEmptyObjectType(type) ?
+ strictNullChecks ? 16318463 /* EmptyObjectStrictFacts */ : 16777215 /* EmptyObjectFacts */ :
+ isFunctionObjectType(type) ?
+ strictNullChecks ? 7880640 /* FunctionStrictFacts */ : 16728000 /* FunctionFacts */ :
+ strictNullChecks ? 7888800 /* ObjectStrictFacts */ : 16736160 /* ObjectFacts */;
+ }
+ if (flags & (16384 /* Void */ | 32768 /* Undefined */)) {
+ return 9830144 /* UndefinedFacts */;
+ }
+ if (flags & 65536 /* Null */) {
+ return 9363232 /* NullFacts */;
+ }
+ if (flags & 12288 /* ESSymbolLike */) {
+ return strictNullChecks ? 7925520 /* SymbolStrictFacts */ : 16772880 /* SymbolFacts */;
+ }
+ if (flags & 67108864 /* NonPrimitive */) {
+ return strictNullChecks ? 7888800 /* ObjectStrictFacts */ : 16736160 /* ObjectFacts */;
+ }
+ if (flags & 131072 /* Never */) {
+ return 0 /* None */;
+ }
+ if (flags & 465829888 /* Instantiable */) {
+ return getTypeFacts(getBaseConstraintOfType(type) || unknownType);
+ }
+ if (flags & 3145728 /* UnionOrIntersection */) {
+ return getTypeFactsOfTypes(type.types);
}
+ return 16777215 /* All */;
+ }
+ function getTypeWithFacts(type, include) {
+ return filterType(type, function (t) { return (getTypeFacts(t) & include) !== 0; });
+ }
+ function getTypeWithDefault(type, defaultExpression) {
+ if (defaultExpression) {
+ var defaultType = getTypeOfExpression(defaultExpression);
+ return getUnionType([getTypeWithFacts(type, 524288 /* NEUndefined */), defaultType]);
+ }
+ return type;
+ }
+ function getTypeOfDestructuredProperty(type, name) {
+ var nameType = getLiteralTypeFromPropertyName(name);
+ if (!isTypeUsableAsPropertyName(nameType))
+ return errorType;
+ var text = getPropertyNameFromType(nameType);
+ return getConstraintForLocation(getTypeOfPropertyOfType(type, text), name) ||
+ isNumericLiteralName(text) && includeUndefinedInIndexSignature(getIndexTypeOfType(type, 1 /* Number */)) ||
+ includeUndefinedInIndexSignature(getIndexTypeOfType(type, 0 /* String */)) ||
+ errorType;
+ }
+ function getTypeOfDestructuredArrayElement(type, index) {
+ return everyType(type, isTupleLikeType) && getTupleElementType(type, index) ||
+ includeUndefinedInIndexSignature(checkIteratedTypeOrElementType(65 /* Destructuring */, type, undefinedType, /*errorNode*/ undefined)) ||
+ errorType;
+ }
+ function includeUndefinedInIndexSignature(type) {
+ if (!type)
+ return type;
+ return compilerOptions.noUncheckedIndexedAccess ?
+ getUnionType([type, undefinedType]) :
+ type;
+ }
+ function getTypeOfDestructuredSpreadExpression(type) {
+ return createArrayType(checkIteratedTypeOrElementType(65 /* Destructuring */, type, undefinedType, /*errorNode*/ undefined) || errorType);
+ }
+ function getAssignedTypeOfBinaryExpression(node) {
+ var isDestructuringDefaultAssignment = node.parent.kind === 199 /* ArrayLiteralExpression */ && isDestructuringAssignmentTarget(node.parent) ||
+ node.parent.kind === 288 /* PropertyAssignment */ && isDestructuringAssignmentTarget(node.parent.parent);
+ return isDestructuringDefaultAssignment ?
+ getTypeWithDefault(getAssignedType(node), node.right) :
+ getTypeOfExpression(node.right);
+ }
+ function isDestructuringAssignmentTarget(parent) {
+ return parent.parent.kind === 216 /* BinaryExpression */ && parent.parent.left === parent ||
+ parent.parent.kind === 239 /* ForOfStatement */ && parent.parent.initializer === parent;
+ }
+ function getAssignedTypeOfArrayLiteralElement(node, element) {
+ return getTypeOfDestructuredArrayElement(getAssignedType(node), node.elements.indexOf(element));
+ }
+ function getAssignedTypeOfSpreadExpression(node) {
+ return getTypeOfDestructuredSpreadExpression(getAssignedType(node.parent));
+ }
+ function getAssignedTypeOfPropertyAssignment(node) {
+ return getTypeOfDestructuredProperty(getAssignedType(node.parent), node.name);
+ }
+ function getAssignedTypeOfShorthandPropertyAssignment(node) {
+ return getTypeWithDefault(getAssignedTypeOfPropertyAssignment(node), node.objectAssignmentInitializer);
+ }
+ function getAssignedType(node) {
var parent = node.parent;
switch (parent.kind) {
- case 242 /* VariableDeclaration */:
- case 156 /* Parameter */:
- case 159 /* PropertyDeclaration */:
- case 158 /* PropertySignature */:
- case 191 /* BindingElement */:
- return getContextualTypeForInitializerExpression(node);
- case 202 /* ArrowFunction */:
- case 235 /* ReturnStatement */:
- return getContextualTypeForReturnExpression(node);
- case 212 /* YieldExpression */:
- return getContextualTypeForYieldOperand(parent);
- case 206 /* AwaitExpression */:
- return getContextualTypeForAwaitOperand(parent);
- case 196 /* CallExpression */:
- if (parent.expression.kind === 96 /* ImportKeyword */) {
- return stringType;
- }
- /* falls through */
- case 197 /* NewExpression */:
- return getContextualTypeForArgument(parent, node);
- case 199 /* TypeAssertionExpression */:
- case 217 /* AsExpression */:
- return ts.isConstTypeReference(parent.type) ? undefined : getTypeFromTypeNode(parent.type);
- case 209 /* BinaryExpression */:
- return getContextualTypeForBinaryOperand(node, contextFlags);
- case 281 /* PropertyAssignment */:
- case 282 /* ShorthandPropertyAssignment */:
- return getContextualTypeForObjectLiteralElement(parent, contextFlags);
- case 283 /* SpreadAssignment */:
- return getApparentTypeOfContextualType(parent.parent, contextFlags);
- case 192 /* ArrayLiteralExpression */: {
- var arrayLiteral = parent;
- var type = getApparentTypeOfContextualType(arrayLiteral, contextFlags);
- return getContextualTypeForElementExpression(type, ts.indexOfNode(arrayLiteral.elements, node));
- }
- case 210 /* ConditionalExpression */:
- return getContextualTypeForConditionalOperand(node, contextFlags);
- case 221 /* TemplateSpan */:
- ts.Debug.assert(parent.parent.kind === 211 /* TemplateExpression */);
- return getContextualTypeForSubstitutionExpression(parent.parent, node);
- case 200 /* ParenthesizedExpression */: {
- // Like in `checkParenthesizedExpression`, an `/** @type {xyz} */` comment before a parenthesized expression acts as a type cast.
- var tag = ts.isInJSFile(parent) ? ts.getJSDocTypeTag(parent) : undefined;
- return tag ? getTypeFromTypeNode(tag.typeExpression.type) : getContextualType(parent, contextFlags);
- }
- case 276 /* JsxExpression */:
- return getContextualTypeForJsxExpression(parent);
- case 273 /* JsxAttribute */:
- case 275 /* JsxSpreadAttribute */:
- return getContextualTypeForJsxAttribute(parent);
- case 268 /* JsxOpeningElement */:
- case 267 /* JsxSelfClosingElement */:
- return getContextualJsxElementAttributesType(parent, contextFlags);
+ case 238 /* ForInStatement */:
+ return stringType;
+ case 239 /* ForOfStatement */:
+ return checkRightHandSideOfForOf(parent) || errorType;
+ case 216 /* BinaryExpression */:
+ return getAssignedTypeOfBinaryExpression(parent);
+ case 210 /* DeleteExpression */:
+ return undefinedType;
+ case 199 /* ArrayLiteralExpression */:
+ return getAssignedTypeOfArrayLiteralElement(parent, node);
+ case 220 /* SpreadElement */:
+ return getAssignedTypeOfSpreadExpression(parent);
+ case 288 /* PropertyAssignment */:
+ return getAssignedTypeOfPropertyAssignment(parent);
+ case 289 /* ShorthandPropertyAssignment */:
+ return getAssignedTypeOfShorthandPropertyAssignment(parent);
}
- return undefined;
+ return errorType;
}
- function getInferenceContext(node) {
- var ancestor = ts.findAncestor(node, function (n) { return !!n.inferenceContext; });
- return ancestor && ancestor.inferenceContext;
+ function getInitialTypeOfBindingElement(node) {
+ var pattern = node.parent;
+ var parentType = getInitialType(pattern.parent);
+ var type = pattern.kind === 196 /* ObjectBindingPattern */ ?
+ getTypeOfDestructuredProperty(parentType, node.propertyName || node.name) :
+ !node.dotDotDotToken ?
+ getTypeOfDestructuredArrayElement(parentType, pattern.elements.indexOf(node)) :
+ getTypeOfDestructuredSpreadExpression(parentType);
+ return getTypeWithDefault(type, node.initializer);
}
- function getContextualJsxElementAttributesType(node, contextFlags) {
- if (ts.isJsxOpeningElement(node) && node.parent.contextualType && contextFlags !== 4 /* Completions */) {
- // Contextually applied type is moved from attributes up to the outer jsx attributes so when walking up from the children they get hit
- // _However_ to hit them from the _attributes_ we must look for them here; otherwise we'll used the declared type
- // (as below) instead!
- return node.parent.contextualType;
+ function getTypeOfInitializer(node) {
+ // Return the cached type if one is available. If the type of the variable was inferred
+ // from its initializer, we'll already have cached the type. Otherwise we compute it now
+ // without caching such that transient types are reflected.
+ var links = getNodeLinks(node);
+ return links.resolvedType || getTypeOfExpression(node);
+ }
+ function getInitialTypeOfVariableDeclaration(node) {
+ if (node.initializer) {
+ return getTypeOfInitializer(node.initializer);
}
- return getContextualTypeForArgumentAtIndex(node, 0);
+ if (node.parent.parent.kind === 238 /* ForInStatement */) {
+ return stringType;
+ }
+ if (node.parent.parent.kind === 239 /* ForOfStatement */) {
+ return checkRightHandSideOfForOf(node.parent.parent) || errorType;
+ }
+ return errorType;
}
- function getEffectiveFirstArgumentForJsxSignature(signature, node) {
- return getJsxReferenceKind(node) !== 0 /* Component */
- ? getJsxPropsTypeFromCallSignature(signature, node)
- : getJsxPropsTypeFromClassType(signature, node);
+ function getInitialType(node) {
+ return node.kind === 249 /* VariableDeclaration */ ?
+ getInitialTypeOfVariableDeclaration(node) :
+ getInitialTypeOfBindingElement(node);
}
- function getJsxPropsTypeFromCallSignature(sig, context) {
- var propsType = getTypeOfFirstParameterOfSignatureWithFallback(sig, unknownType);
- propsType = getJsxManagedAttributesFromLocatedAttributes(context, getJsxNamespaceAt(context), propsType);
- var intrinsicAttribs = getJsxType(JsxNames.IntrinsicAttributes, context);
- if (intrinsicAttribs !== errorType) {
- propsType = intersectTypes(intrinsicAttribs, propsType);
- }
- return propsType;
+ function isEmptyArrayAssignment(node) {
+ return node.kind === 249 /* VariableDeclaration */ && node.initializer &&
+ isEmptyArrayLiteral(node.initializer) ||
+ node.kind !== 198 /* BindingElement */ && node.parent.kind === 216 /* BinaryExpression */ &&
+ isEmptyArrayLiteral(node.parent.right);
}
- function getJsxPropsTypeForSignatureFromMember(sig, forcedLookupLocation) {
- if (sig.unionSignatures) {
- // JSX Elements using the legacy `props`-field based lookup (eg, react class components) need to treat the `props` member as an input
- // instead of an output position when resolving the signature. We need to go back to the input signatures of the composite signature,
- // get the type of `props` on each return type individually, and then _intersect them_, rather than union them (as would normally occur
- // for a union signature). It's an unfortunate quirk of looking in the output of the signature for the type we want to use for the input.
- // The default behavior of `getTypeOfFirstParameterOfSignatureWithFallback` when no `props` member name is defined is much more sane.
- var results = [];
- for (var _i = 0, _a = sig.unionSignatures; _i < _a.length; _i++) {
- var signature = _a[_i];
- var instance = getReturnTypeOfSignature(signature);
- if (isTypeAny(instance)) {
- return instance;
- }
- var propType = getTypeOfPropertyOfType(instance, forcedLookupLocation);
- if (!propType) {
- return;
+ function getReferenceCandidate(node) {
+ switch (node.kind) {
+ case 207 /* ParenthesizedExpression */:
+ return getReferenceCandidate(node.expression);
+ case 216 /* BinaryExpression */:
+ switch (node.operatorToken.kind) {
+ case 62 /* EqualsToken */:
+ case 74 /* BarBarEqualsToken */:
+ case 75 /* AmpersandAmpersandEqualsToken */:
+ case 76 /* QuestionQuestionEqualsToken */:
+ return getReferenceCandidate(node.left);
+ case 27 /* CommaToken */:
+ return getReferenceCandidate(node.right);
}
- results.push(propType);
- }
- return getIntersectionType(results);
}
- var instanceType = getReturnTypeOfSignature(sig);
- return isTypeAny(instanceType) ? instanceType : getTypeOfPropertyOfType(instanceType, forcedLookupLocation);
+ return node;
}
- function getStaticTypeOfReferencedJsxConstructor(context) {
- if (isJsxIntrinsicIdentifier(context.tagName)) {
- var result = getIntrinsicAttributesTypeFromJsxOpeningLikeElement(context);
- var fakeSignature = createSignatureForJSXIntrinsic(context, result);
- return getOrCreateTypeFromSignature(fakeSignature);
- }
- var tagType = checkExpressionCached(context.tagName);
- if (tagType.flags & 128 /* StringLiteral */) {
- var result = getIntrinsicAttributesTypeFromStringLiteralType(tagType, context);
- if (!result) {
- return errorType;
- }
- var fakeSignature = createSignatureForJSXIntrinsic(context, result);
- return getOrCreateTypeFromSignature(fakeSignature);
- }
- return tagType;
+ function getReferenceRoot(node) {
+ var parent = node.parent;
+ return parent.kind === 207 /* ParenthesizedExpression */ ||
+ parent.kind === 216 /* BinaryExpression */ && parent.operatorToken.kind === 62 /* EqualsToken */ && parent.left === node ||
+ parent.kind === 216 /* BinaryExpression */ && parent.operatorToken.kind === 27 /* CommaToken */ && parent.right === node ?
+ getReferenceRoot(parent) : node;
}
- function getJsxManagedAttributesFromLocatedAttributes(context, ns, attributesType) {
- var managedSym = getJsxLibraryManagedAttributes(ns);
- if (managedSym) {
- var declaredManagedType = getDeclaredTypeOfSymbol(managedSym);
- var ctorType = getStaticTypeOfReferencedJsxConstructor(context);
- if (ts.length(declaredManagedType.typeParameters) >= 2) {
- var args = fillMissingTypeArguments([ctorType, attributesType], declaredManagedType.typeParameters, 2, ts.isInJSFile(context));
- return createTypeReference(declaredManagedType, args);
- }
- else if (ts.length(declaredManagedType.aliasTypeArguments) >= 2) {
- var args = fillMissingTypeArguments([ctorType, attributesType], declaredManagedType.aliasTypeArguments, 2, ts.isInJSFile(context));
- return getTypeAliasInstantiation(declaredManagedType.aliasSymbol, args);
- }
+ function getTypeOfSwitchClause(clause) {
+ if (clause.kind === 284 /* CaseClause */) {
+ return getRegularTypeOfLiteralType(getTypeOfExpression(clause.expression));
}
- return attributesType;
+ return neverType;
}
- function getJsxPropsTypeFromClassType(sig, context) {
- var ns = getJsxNamespaceAt(context);
- var forcedLookupLocation = getJsxElementPropertiesName(ns);
- var attributesType = forcedLookupLocation === undefined
- // If there is no type ElementAttributesProperty, return the type of the first parameter of the signature, which should be the props type
- ? getTypeOfFirstParameterOfSignatureWithFallback(sig, unknownType)
- : forcedLookupLocation === ""
- // If there is no e.g. 'props' member in ElementAttributesProperty, use the element class type instead
- ? getReturnTypeOfSignature(sig)
- // Otherwise get the type of the property on the signature return type
- : getJsxPropsTypeForSignatureFromMember(sig, forcedLookupLocation);
- if (!attributesType) {
- // There is no property named 'props' on this instance type
- if (!!forcedLookupLocation && !!ts.length(context.attributes.properties)) {
- error(context, ts.Diagnostics.JSX_element_class_does_not_support_attributes_because_it_does_not_have_a_0_property, ts.unescapeLeadingUnderscores(forcedLookupLocation));
- }
- return unknownType;
- }
- attributesType = getJsxManagedAttributesFromLocatedAttributes(context, ns, attributesType);
- if (isTypeAny(attributesType)) {
- // Props is of type 'any' or unknown
- return attributesType;
- }
- else {
- // Normal case -- add in IntrinsicClassElements and IntrinsicElements
- var apparentAttributesType = attributesType;
- var intrinsicClassAttribs = getJsxType(JsxNames.IntrinsicClassAttributes, context);
- if (intrinsicClassAttribs !== errorType) {
- var typeParams = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(intrinsicClassAttribs.symbol);
- var hostClassType = getReturnTypeOfSignature(sig);
- apparentAttributesType = intersectTypes(typeParams
- ? createTypeReference(intrinsicClassAttribs, fillMissingTypeArguments([hostClassType], typeParams, getMinTypeArgumentCount(typeParams), ts.isInJSFile(context)))
- : intrinsicClassAttribs, apparentAttributesType);
- }
- var intrinsicAttribs = getJsxType(JsxNames.IntrinsicAttributes, context);
- if (intrinsicAttribs !== errorType) {
- apparentAttributesType = intersectTypes(intrinsicAttribs, apparentAttributesType);
+ function getSwitchClauseTypes(switchStatement) {
+ var links = getNodeLinks(switchStatement);
+ if (!links.switchTypes) {
+ links.switchTypes = [];
+ for (var _i = 0, _a = switchStatement.caseBlock.clauses; _i < _a.length; _i++) {
+ var clause = _a[_i];
+ links.switchTypes.push(getTypeOfSwitchClause(clause));
}
- return apparentAttributesType;
}
+ return links.switchTypes;
}
- // If the given type is an object or union type with a single signature, and if that signature has at
- // least as many parameters as the given function, return the signature. Otherwise return undefined.
- function getContextualCallSignature(type, node) {
- var signatures = getSignaturesOfType(type, 0 /* Call */);
- if (signatures.length === 1) {
- var signature = signatures[0];
- if (!isAritySmaller(signature, node)) {
- return signature;
+ function getSwitchClauseTypeOfWitnesses(switchStatement, retainDefault) {
+ var witnesses = [];
+ for (var _i = 0, _a = switchStatement.caseBlock.clauses; _i < _a.length; _i++) {
+ var clause = _a[_i];
+ if (clause.kind === 284 /* CaseClause */) {
+ if (ts.isStringLiteralLike(clause.expression)) {
+ witnesses.push(clause.expression.text);
+ continue;
+ }
+ return ts.emptyArray;
}
+ if (retainDefault)
+ witnesses.push(/*explicitDefaultStatement*/ undefined);
}
+ return witnesses;
}
- /** If the contextual signature has fewer parameters than the function expression, do not use it */
- function isAritySmaller(signature, target) {
- var targetParameterCount = 0;
- for (; targetParameterCount < target.parameters.length; targetParameterCount++) {
- var param = target.parameters[targetParameterCount];
- if (param.initializer || param.questionToken || param.dotDotDotToken || isJSDocOptionalParameter(param)) {
- break;
+ function eachTypeContainedIn(source, types) {
+ return source.flags & 1048576 /* Union */ ? !ts.forEach(source.types, function (t) { return !ts.contains(types, t); }) : ts.contains(types, source);
+ }
+ function isTypeSubsetOf(source, target) {
+ return source === target || target.flags & 1048576 /* Union */ && isTypeSubsetOfUnion(source, target);
+ }
+ function isTypeSubsetOfUnion(source, target) {
+ if (source.flags & 1048576 /* Union */) {
+ for (var _i = 0, _a = source.types; _i < _a.length; _i++) {
+ var t = _a[_i];
+ if (!containsType(target.types, t)) {
+ return false;
+ }
}
+ return true;
}
- if (target.parameters.length && ts.parameterIsThisKeyword(target.parameters[0])) {
- targetParameterCount--;
+ if (source.flags & 1024 /* EnumLiteral */ && getBaseTypeOfEnumLiteralType(source) === target) {
+ return true;
}
- return !hasEffectiveRestParameter(signature) && getParameterCount(signature) < targetParameterCount;
+ return containsType(target.types, source);
}
- function isFunctionExpressionOrArrowFunction(node) {
- return node.kind === 201 /* FunctionExpression */ || node.kind === 202 /* ArrowFunction */;
+ function forEachType(type, f) {
+ return type.flags & 1048576 /* Union */ ? ts.forEach(type.types, f) : f(type);
}
- function getContextualSignatureForFunctionLikeDeclaration(node) {
- // Only function expressions, arrow functions, and object literal methods are contextually typed.
- return isFunctionExpressionOrArrowFunction(node) || ts.isObjectLiteralMethod(node)
- ? getContextualSignature(node)
- : undefined;
+ function everyType(type, f) {
+ return type.flags & 1048576 /* Union */ ? ts.every(type.types, f) : f(type);
}
- // Return the contextual signature for a given expression node. A contextual type provides a
- // contextual signature if it has a single call signature and if that call signature is non-generic.
- // If the contextual type is a union type, get the signature from each type possible and if they are
- // all identical ignoring their return type, the result is same signature but with return type as
- // union type of return types from these signatures
- function getContextualSignature(node) {
- ts.Debug.assert(node.kind !== 161 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node));
- var typeTagSignature = getSignatureOfTypeTag(node);
- if (typeTagSignature) {
- return typeTagSignature;
+ function filterType(type, f) {
+ if (type.flags & 1048576 /* Union */) {
+ var types = type.types;
+ var filtered = ts.filter(types, f);
+ return filtered === types ? type : getUnionTypeFromSortedList(filtered, type.objectFlags);
}
- var type = getApparentTypeOfContextualType(node, 1 /* Signature */);
- if (!type) {
- return undefined;
+ return type.flags & 131072 /* Never */ || f(type) ? type : neverType;
+ }
+ function countTypes(type) {
+ return type.flags & 1048576 /* Union */ ? type.types.length : 1;
+ }
+ function mapType(type, mapper, noReductions) {
+ if (type.flags & 131072 /* Never */) {
+ return type;
}
if (!(type.flags & 1048576 /* Union */)) {
- return getContextualCallSignature(type, node);
+ return mapper(type);
}
- var signatureList;
- var types = type.types;
- for (var _i = 0, types_17 = types; _i < types_17.length; _i++) {
- var current = types_17[_i];
- var signature = getContextualCallSignature(current, node);
- if (signature) {
- if (!signatureList) {
- // This signature will contribute to contextual union signature
- signatureList = [signature];
- }
- else if (!compareSignaturesIdentical(signatureList[0], signature, /*partialMatch*/ false, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesIdentical)) {
- // Signatures aren't identical, do not use
- return undefined;
+ var mappedTypes;
+ for (var _i = 0, _a = type.types; _i < _a.length; _i++) {
+ var t = _a[_i];
+ var mapped = mapper(t);
+ if (mapped) {
+ if (!mappedTypes) {
+ mappedTypes = [mapped];
}
else {
- // Use this signature for contextual union signature
- signatureList.push(signature);
+ mappedTypes.push(mapped);
}
}
}
- // Result is union of signatures collected (return type is union of return types of this signature set)
- if (signatureList) {
- return signatureList.length === 1 ? signatureList[0] : createUnionSignature(signatureList[0], signatureList);
+ return mappedTypes && getUnionType(mappedTypes, noReductions ? 0 /* None */ : 1 /* Literal */);
+ }
+ function extractTypesOfKind(type, kind) {
+ return filterType(type, function (t) { return (t.flags & kind) !== 0; });
+ }
+ // Return a new type in which occurrences of the string and number primitive types in
+ // typeWithPrimitives have been replaced with occurrences of string literals and numeric
+ // literals in typeWithLiterals, respectively.
+ function replacePrimitivesWithLiterals(typeWithPrimitives, typeWithLiterals) {
+ if (isTypeSubsetOf(stringType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 128 /* StringLiteral */) ||
+ isTypeSubsetOf(numberType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 256 /* NumberLiteral */) ||
+ isTypeSubsetOf(bigintType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 2048 /* BigIntLiteral */)) {
+ return mapType(typeWithPrimitives, function (t) {
+ return t.flags & 4 /* String */ ? extractTypesOfKind(typeWithLiterals, 4 /* String */ | 128 /* StringLiteral */) :
+ t.flags & 8 /* Number */ ? extractTypesOfKind(typeWithLiterals, 8 /* Number */ | 256 /* NumberLiteral */) :
+ t.flags & 64 /* BigInt */ ? extractTypesOfKind(typeWithLiterals, 64 /* BigInt */ | 2048 /* BigIntLiteral */) : t;
+ });
}
+ return typeWithPrimitives;
}
- function checkSpreadExpression(node, checkMode) {
- if (languageVersion < 2 /* ES2015 */) {
- checkExternalEmitHelpers(node, compilerOptions.downlevelIteration ? 1536 /* SpreadIncludes */ : 2048 /* SpreadArrays */);
+ function isIncomplete(flowType) {
+ return flowType.flags === 0;
+ }
+ function getTypeFromFlowType(flowType) {
+ return flowType.flags === 0 ? flowType.type : flowType;
+ }
+ function createFlowType(type, incomplete) {
+ return incomplete ? { flags: 0, type: type.flags & 131072 /* Never */ ? silentNeverType : type } : type;
+ }
+ // An evolving array type tracks the element types that have so far been seen in an
+ // 'x.push(value)' or 'x[n] = value' operation along the control flow graph. Evolving
+ // array types are ultimately converted into manifest array types (using getFinalArrayType)
+ // and never escape the getFlowTypeOfReference function.
+ function createEvolvingArrayType(elementType) {
+ var result = createObjectType(256 /* EvolvingArray */);
+ result.elementType = elementType;
+ return result;
+ }
+ function getEvolvingArrayType(elementType) {
+ return evolvingArrayTypes[elementType.id] || (evolvingArrayTypes[elementType.id] = createEvolvingArrayType(elementType));
+ }
+ // When adding evolving array element types we do not perform subtype reduction. Instead,
+ // we defer subtype reduction until the evolving array type is finalized into a manifest
+ // array type.
+ function addEvolvingArrayElementType(evolvingArrayType, node) {
+ var elementType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(getContextFreeTypeOfExpression(node)));
+ return isTypeSubsetOf(elementType, evolvingArrayType.elementType) ? evolvingArrayType : getEvolvingArrayType(getUnionType([evolvingArrayType.elementType, elementType]));
+ }
+ function createFinalArrayType(elementType) {
+ return elementType.flags & 131072 /* Never */ ?
+ autoArrayType :
+ createArrayType(elementType.flags & 1048576 /* Union */ ?
+ getUnionType(elementType.types, 2 /* Subtype */) :
+ elementType);
+ }
+ // We perform subtype reduction upon obtaining the final array type from an evolving array type.
+ function getFinalArrayType(evolvingArrayType) {
+ return evolvingArrayType.finalArrayType || (evolvingArrayType.finalArrayType = createFinalArrayType(evolvingArrayType.elementType));
+ }
+ function finalizeEvolvingArrayType(type) {
+ return ts.getObjectFlags(type) & 256 /* EvolvingArray */ ? getFinalArrayType(type) : type;
+ }
+ function getElementTypeOfEvolvingArrayType(type) {
+ return ts.getObjectFlags(type) & 256 /* EvolvingArray */ ? type.elementType : neverType;
+ }
+ function isEvolvingArrayTypeList(types) {
+ var hasEvolvingArrayType = false;
+ for (var _i = 0, types_17 = types; _i < types_17.length; _i++) {
+ var t = types_17[_i];
+ if (!(t.flags & 131072 /* Never */)) {
+ if (!(ts.getObjectFlags(t) & 256 /* EvolvingArray */)) {
+ return false;
+ }
+ hasEvolvingArrayType = true;
+ }
}
- var arrayOrIterableType = checkExpression(node.expression, checkMode);
- return checkIteratedTypeOrElementType(33 /* Spread */, arrayOrIterableType, undefinedType, node.expression);
+ return hasEvolvingArrayType;
}
- function hasDefaultValue(node) {
- return (node.kind === 191 /* BindingElement */ && !!node.initializer) ||
- (node.kind === 209 /* BinaryExpression */ && node.operatorToken.kind === 62 /* EqualsToken */);
+ // At flow control branch or loop junctions, if the type along every antecedent code path
+ // is an evolving array type, we construct a combined evolving array type. Otherwise we
+ // finalize all evolving array types.
+ function getUnionOrEvolvingArrayType(types, subtypeReduction) {
+ return isEvolvingArrayTypeList(types) ?
+ getEvolvingArrayType(getUnionType(ts.map(types, getElementTypeOfEvolvingArrayType))) :
+ getUnionType(ts.sameMap(types, finalizeEvolvingArrayType), subtypeReduction);
}
- function checkArrayLiteral(node, checkMode, forceTuple) {
- var elements = node.elements;
- var elementCount = elements.length;
- var elementTypes = [];
- var hasEndingSpreadElement = false;
- var hasNonEndingSpreadElement = false;
- var contextualType = getApparentTypeOfContextualType(node);
- var inDestructuringPattern = ts.isAssignmentTarget(node);
- var inConstContext = isConstContext(node);
- for (var i = 0; i < elementCount; i++) {
- var e = elements[i];
- var spread = e.kind === 213 /* SpreadElement */ && e.expression;
- var spreadType = spread && checkExpression(spread, checkMode, forceTuple);
- if (spreadType && isTupleType(spreadType)) {
- elementTypes.push.apply(elementTypes, getTypeArguments(spreadType));
- if (spreadType.target.hasRestElement) {
- if (i === elementCount - 1)
- hasEndingSpreadElement = true;
- else
- hasNonEndingSpreadElement = true;
+ // Return true if the given node is 'x' in an 'x.length', x.push(value)', 'x.unshift(value)' or
+ // 'x[n] = value' operation, where 'n' is an expression of type any, undefined, or a number-like type.
+ function isEvolvingArrayOperationTarget(node) {
+ var root = getReferenceRoot(node);
+ var parent = root.parent;
+ var isLengthPushOrUnshift = ts.isPropertyAccessExpression(parent) && (parent.name.escapedText === "length" ||
+ parent.parent.kind === 203 /* CallExpression */
+ && ts.isIdentifier(parent.name)
+ && ts.isPushOrUnshiftIdentifier(parent.name));
+ var isElementAssignment = parent.kind === 202 /* ElementAccessExpression */ &&
+ parent.expression === root &&
+ parent.parent.kind === 216 /* BinaryExpression */ &&
+ parent.parent.operatorToken.kind === 62 /* EqualsToken */ &&
+ parent.parent.left === parent &&
+ !ts.isAssignmentTarget(parent.parent) &&
+ isTypeAssignableToKind(getTypeOfExpression(parent.argumentExpression), 296 /* NumberLike */);
+ return isLengthPushOrUnshift || isElementAssignment;
+ }
+ function isDeclarationWithExplicitTypeAnnotation(declaration) {
+ return (declaration.kind === 249 /* VariableDeclaration */ || declaration.kind === 160 /* Parameter */ ||
+ declaration.kind === 163 /* PropertyDeclaration */ || declaration.kind === 162 /* PropertySignature */) &&
+ !!ts.getEffectiveTypeAnnotationNode(declaration);
+ }
+ function getExplicitTypeOfSymbol(symbol, diagnostic) {
+ if (symbol.flags & (16 /* Function */ | 8192 /* Method */ | 32 /* Class */ | 512 /* ValueModule */)) {
+ return getTypeOfSymbol(symbol);
+ }
+ if (symbol.flags & (3 /* Variable */ | 4 /* Property */)) {
+ if (ts.getCheckFlags(symbol) & 262144 /* Mapped */) {
+ var origin = symbol.syntheticOrigin;
+ if (origin && getExplicitTypeOfSymbol(origin)) {
+ return getTypeOfSymbol(symbol);
}
}
- else {
- if (inDestructuringPattern && spreadType) {
- // Given the following situation:
- // var c: {};
- // [...c] = ["", 0];
- //
- // c is represented in the tree as a spread element in an array literal.
- // But c really functions as a rest element, and its purpose is to provide
- // a contextual type for the right hand side of the assignment. Therefore,
- // instead of calling checkExpression on "...c", which will give an error
- // if c is not iterable/array-like, we need to act as if we are trying to
- // get the contextual element type from it. So we do something similar to
- // getContextualTypeForElementExpression, which will crucially not error
- // if there is no index type / iterated type.
- var restElementType = getIndexTypeOfType(spreadType, 1 /* Number */) ||
- getIteratedTypeOrElementType(65 /* Destructuring */, spreadType, undefinedType, /*errorNode*/ undefined, /*checkAssignability*/ false);
- if (restElementType) {
- elementTypes.push(restElementType);
- }
+ var declaration = symbol.valueDeclaration;
+ if (declaration) {
+ if (isDeclarationWithExplicitTypeAnnotation(declaration)) {
+ return getTypeOfSymbol(symbol);
}
- else {
- var elementContextualType = getContextualTypeForElementExpression(contextualType, elementTypes.length);
- var type = checkExpressionForMutableLocation(e, checkMode, elementContextualType, forceTuple);
- elementTypes.push(type);
+ if (ts.isVariableDeclaration(declaration) && declaration.parent.parent.kind === 239 /* ForOfStatement */) {
+ var statement = declaration.parent.parent;
+ var expressionType = getTypeOfDottedName(statement.expression, /*diagnostic*/ undefined);
+ if (expressionType) {
+ var use = statement.awaitModifier ? 15 /* ForAwaitOf */ : 13 /* ForOf */;
+ return checkIteratedTypeOrElementType(use, expressionType, undefinedType, /*errorNode*/ undefined);
+ }
}
- if (spread) { // tuples are done above, so these are only arrays
- if (i === elementCount - 1)
- hasEndingSpreadElement = true;
- else
- hasNonEndingSpreadElement = true;
+ if (diagnostic) {
+ ts.addRelatedInfo(diagnostic, ts.createDiagnosticForNode(declaration, ts.Diagnostics._0_needs_an_explicit_type_annotation, symbolToString(symbol)));
}
}
}
- if (!hasNonEndingSpreadElement) {
- var minLength = elementTypes.length - (hasEndingSpreadElement ? 1 : 0);
- // If array literal is actually a destructuring pattern, mark it as an implied type. We do this such
- // that we get the same behavior for "var [x, y] = []" and "[x, y] = []".
- var tupleResult = void 0;
- if (inDestructuringPattern && minLength > 0) {
- var type = cloneTypeReference(createTupleType(elementTypes, minLength, hasEndingSpreadElement));
- type.pattern = node;
- return type;
- }
- else if (tupleResult = getArrayLiteralTupleTypeIfApplicable(elementTypes, contextualType, hasEndingSpreadElement, elementTypes.length, inConstContext)) {
- return createArrayLiteralType(tupleResult);
- }
- else if (forceTuple) {
- return createArrayLiteralType(createTupleType(elementTypes, minLength, hasEndingSpreadElement));
- }
- }
- return createArrayLiteralType(createArrayType(elementTypes.length ?
- getUnionType(elementTypes, 2 /* Subtype */) :
- strictNullChecks ? implicitNeverType : undefinedWideningType, inConstContext));
- }
- function createArrayLiteralType(type) {
- if (!(ts.getObjectFlags(type) & 4 /* Reference */)) {
- return type;
- }
- var literalType = type.literalType;
- if (!literalType) {
- literalType = type.literalType = cloneTypeReference(type);
- literalType.objectFlags |= 65536 /* ArrayLiteral */ | 1048576 /* ContainsObjectOrArrayLiteral */;
- }
- return literalType;
- }
- function getArrayLiteralTupleTypeIfApplicable(elementTypes, contextualType, hasRestElement, elementCount, readonly) {
- if (elementCount === void 0) { elementCount = elementTypes.length; }
- if (readonly === void 0) { readonly = false; }
- // Infer a tuple type when the contextual type is or contains a tuple-like type
- if (readonly || (contextualType && forEachType(contextualType, isTupleLikeType))) {
- return createTupleType(elementTypes, elementCount - (hasRestElement ? 1 : 0), hasRestElement, readonly);
- }
}
- function isNumericName(name) {
- switch (name.kind) {
- case 154 /* ComputedPropertyName */:
- return isNumericComputedName(name);
- case 75 /* Identifier */:
- return isNumericLiteralName(name.escapedText);
- case 8 /* NumericLiteral */:
- case 10 /* StringLiteral */:
- return isNumericLiteralName(name.text);
- default:
- return false;
+ // We require the dotted function name in an assertion expression to be comprised of identifiers
+ // that reference function, method, class or value module symbols; or variable, property or
+ // parameter symbols with declarations that have explicit type annotations. Such references are
+ // resolvable with no possibility of triggering circularities in control flow analysis.
+ function getTypeOfDottedName(node, diagnostic) {
+ if (!(node.flags & 16777216 /* InWithStatement */)) {
+ switch (node.kind) {
+ case 78 /* Identifier */:
+ var symbol = getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(node));
+ return getExplicitTypeOfSymbol(symbol.flags & 2097152 /* Alias */ ? resolveAlias(symbol) : symbol, diagnostic);
+ case 107 /* ThisKeyword */:
+ return getExplicitThisType(node);
+ case 105 /* SuperKeyword */:
+ return checkSuperExpression(node);
+ case 201 /* PropertyAccessExpression */:
+ var type = getTypeOfDottedName(node.expression, diagnostic);
+ var prop = type && getPropertyOfType(type, node.name.escapedText);
+ return prop && getExplicitTypeOfSymbol(prop, diagnostic);
+ case 207 /* ParenthesizedExpression */:
+ return getTypeOfDottedName(node.expression, diagnostic);
+ }
}
}
- function isNumericComputedName(name) {
- // It seems odd to consider an expression of type Any to result in a numeric name,
- // but this behavior is consistent with checkIndexedAccess
- return isTypeAssignableToKind(checkComputedPropertyName(name), 296 /* NumberLike */);
- }
- function isInfinityOrNaNString(name) {
- return name === "Infinity" || name === "-Infinity" || name === "NaN";
- }
- function isNumericLiteralName(name) {
- // The intent of numeric names is that
- // - they are names with text in a numeric form, and that
- // - setting properties/indexing with them is always equivalent to doing so with the numeric literal 'numLit',
- // acquired by applying the abstract 'ToNumber' operation on the name's text.
- //
- // The subtlety is in the latter portion, as we cannot reliably say that anything that looks like a numeric literal is a numeric name.
- // In fact, it is the case that the text of the name must be equal to 'ToString(numLit)' for this to hold.
- //
- // Consider the property name '"0xF00D"'. When one indexes with '0xF00D', they are actually indexing with the value of 'ToString(0xF00D)'
- // according to the ECMAScript specification, so it is actually as if the user indexed with the string '"61453"'.
- // Thus, the text of all numeric literals equivalent to '61543' such as '0xF00D', '0xf00D', '0170015', etc. are not valid numeric names
- // because their 'ToString' representation is not equal to their original text.
- // This is motivated by ECMA-262 sections 9.3.1, 9.8.1, 11.1.5, and 11.2.1.
- //
- // Here, we test whether 'ToString(ToNumber(name))' is exactly equal to 'name'.
- // The '+' prefix operator is equivalent here to applying the abstract ToNumber operation.
- // Applying the 'toString()' method on a number gives us the abstract ToString operation on a number.
- //
- // Note that this accepts the values 'Infinity', '-Infinity', and 'NaN', and that this is intentional.
- // This is desired behavior, because when indexing with them as numeric entities, you are indexing
- // with the strings '"Infinity"', '"-Infinity"', and '"NaN"' respectively.
- return (+name).toString() === name;
- }
- function checkComputedPropertyName(node) {
- var links = getNodeLinks(node.expression);
- if (!links.resolvedType) {
- links.resolvedType = checkExpression(node.expression);
- // This will allow types number, string, symbol or any. It will also allow enums, the unknown
- // type, and any union of these types (like string | number).
- if (links.resolvedType.flags & 98304 /* Nullable */ ||
- !isTypeAssignableToKind(links.resolvedType, 132 /* StringLike */ | 296 /* NumberLike */ | 12288 /* ESSymbolLike */) &&
- !isTypeAssignableTo(links.resolvedType, stringNumberSymbolType)) {
- error(node, ts.Diagnostics.A_computed_property_name_must_be_of_type_string_number_symbol_or_any);
+ function getEffectsSignature(node) {
+ var links = getNodeLinks(node);
+ var signature = links.effectsSignature;
+ if (signature === undefined) {
+ // A call expression parented by an expression statement is a potential assertion. Other call
+ // expressions are potential type predicate function calls. In order to avoid triggering
+ // circularities in control flow analysis, we use getTypeOfDottedName when resolving the call
+ // target expression of an assertion.
+ var funcType = void 0;
+ if (node.parent.kind === 233 /* ExpressionStatement */) {
+ funcType = getTypeOfDottedName(node.expression, /*diagnostic*/ undefined);
}
- else {
- checkThatExpressionIsProperSymbolReference(node.expression, links.resolvedType, /*reportError*/ true);
+ else if (node.expression.kind !== 105 /* SuperKeyword */) {
+ if (ts.isOptionalChain(node)) {
+ funcType = checkNonNullType(getOptionalExpressionType(checkExpression(node.expression), node.expression), node.expression);
+ }
+ else {
+ funcType = checkNonNullExpression(node.expression);
+ }
}
+ var signatures = getSignaturesOfType(funcType && getApparentType(funcType) || unknownType, 0 /* Call */);
+ var candidate = signatures.length === 1 && !signatures[0].typeParameters ? signatures[0] :
+ ts.some(signatures, hasTypePredicateOrNeverReturnType) ? getResolvedSignature(node) :
+ undefined;
+ signature = links.effectsSignature = candidate && hasTypePredicateOrNeverReturnType(candidate) ? candidate : unknownSignature;
}
- return links.resolvedType;
+ return signature === unknownSignature ? undefined : signature;
}
- function getObjectLiteralIndexInfo(node, offset, properties, kind) {
- var propTypes = [];
- for (var i = 0; i < properties.length; i++) {
- if (kind === 0 /* String */ || isNumericName(node.properties[i + offset].name)) {
- propTypes.push(getTypeOfSymbol(properties[i]));
- }
- }
- var unionType = propTypes.length ? getUnionType(propTypes, 2 /* Subtype */) : undefinedType;
- return createIndexInfo(unionType, isConstContext(node));
+ function hasTypePredicateOrNeverReturnType(signature) {
+ return !!(getTypePredicateOfSignature(signature) ||
+ signature.declaration && (getReturnTypeFromAnnotation(signature.declaration) || unknownType).flags & 131072 /* Never */);
}
- function getImmediateAliasedSymbol(symbol) {
- ts.Debug.assert((symbol.flags & 2097152 /* Alias */) !== 0, "Should only get Alias here.");
- var links = getSymbolLinks(symbol);
- if (!links.immediateTarget) {
- var node = getDeclarationOfAliasSymbol(symbol);
- if (!node)
- return ts.Debug.fail();
- links.immediateTarget = getTargetOfAliasDeclaration(node, /*dontRecursivelyResolve*/ true);
+ function getTypePredicateArgument(predicate, callExpression) {
+ if (predicate.kind === 1 /* Identifier */ || predicate.kind === 3 /* AssertsIdentifier */) {
+ return callExpression.arguments[predicate.parameterIndex];
}
- return links.immediateTarget;
+ var invokedExpression = ts.skipParentheses(callExpression.expression);
+ return ts.isAccessExpression(invokedExpression) ? ts.skipParentheses(invokedExpression.expression) : undefined;
}
- function checkObjectLiteral(node, checkMode) {
- var inDestructuringPattern = ts.isAssignmentTarget(node);
- // Grammar checking
- checkGrammarObjectLiteralExpression(node, inDestructuringPattern);
- var allPropertiesTable = strictNullChecks ? ts.createSymbolTable() : undefined;
- var propertiesTable = ts.createSymbolTable();
- var propertiesArray = [];
- var spread = emptyObjectType;
- var contextualType = getApparentTypeOfContextualType(node);
- var contextualTypeHasPattern = contextualType && contextualType.pattern &&
- (contextualType.pattern.kind === 189 /* ObjectBindingPattern */ || contextualType.pattern.kind === 193 /* ObjectLiteralExpression */);
- var inConstContext = isConstContext(node);
- var checkFlags = inConstContext ? 8 /* Readonly */ : 0;
- var isInJavascript = ts.isInJSFile(node) && !ts.isInJsonFile(node);
- var enumTag = ts.getJSDocEnumTag(node);
- var isJSObjectLiteral = !contextualType && isInJavascript && !enumTag;
- var objectFlags = freshObjectLiteralFlag;
- var patternWithComputedProperties = false;
- var hasComputedStringProperty = false;
- var hasComputedNumberProperty = false;
- // Spreads may cause an early bail; ensure computed names are always checked (this is cached)
- // As otherwise they may not be checked until exports for the type at this position are retrieved,
- // which may never occur.
- for (var _i = 0, _a = node.properties; _i < _a.length; _i++) {
- var elem = _a[_i];
- if (elem.name && ts.isComputedPropertyName(elem.name) && !ts.isWellKnownSymbolSyntactically(elem.name)) {
- checkComputedPropertyName(elem.name);
+ function reportFlowControlError(node) {
+ var block = ts.findAncestor(node, ts.isFunctionOrModuleBlock);
+ var sourceFile = ts.getSourceFileOfNode(node);
+ var span = ts.getSpanOfTokenAtPosition(sourceFile, block.statements.pos);
+ diagnostics.add(ts.createFileDiagnostic(sourceFile, span.start, span.length, ts.Diagnostics.The_containing_function_or_module_body_is_too_large_for_control_flow_analysis));
+ }
+ function isReachableFlowNode(flow) {
+ var result = isReachableFlowNodeWorker(flow, /*noCacheCheck*/ false);
+ lastFlowNode = flow;
+ lastFlowNodeReachable = result;
+ return result;
+ }
+ function isFalseExpression(expr) {
+ var node = ts.skipParentheses(expr);
+ return node.kind === 94 /* FalseKeyword */ || node.kind === 216 /* BinaryExpression */ && (node.operatorToken.kind === 55 /* AmpersandAmpersandToken */ && (isFalseExpression(node.left) || isFalseExpression(node.right)) ||
+ node.operatorToken.kind === 56 /* BarBarToken */ && isFalseExpression(node.left) && isFalseExpression(node.right));
+ }
+ function isReachableFlowNodeWorker(flow, noCacheCheck) {
+ while (true) {
+ if (flow === lastFlowNode) {
+ return lastFlowNodeReachable;
}
- }
- var offset = 0;
- for (var i = 0; i < node.properties.length; i++) {
- var memberDecl = node.properties[i];
- var member = getSymbolOfNode(memberDecl);
- var computedNameType = memberDecl.name && memberDecl.name.kind === 154 /* ComputedPropertyName */ && !ts.isWellKnownSymbolSyntactically(memberDecl.name.expression) ?
- checkComputedPropertyName(memberDecl.name) : undefined;
- if (memberDecl.kind === 281 /* PropertyAssignment */ ||
- memberDecl.kind === 282 /* ShorthandPropertyAssignment */ ||
- ts.isObjectLiteralMethod(memberDecl)) {
- var type = memberDecl.kind === 281 /* PropertyAssignment */ ? checkPropertyAssignment(memberDecl, checkMode) :
- memberDecl.kind === 282 /* ShorthandPropertyAssignment */ ? checkExpressionForMutableLocation(memberDecl.name, checkMode) :
- checkObjectLiteralMethod(memberDecl, checkMode);
- if (isInJavascript) {
- var jsDocType = getTypeForDeclarationFromJSDocComment(memberDecl);
- if (jsDocType) {
- checkTypeAssignableTo(type, jsDocType, memberDecl);
- type = jsDocType;
- }
- else if (enumTag && enumTag.typeExpression) {
- checkTypeAssignableTo(type, getTypeFromTypeNode(enumTag.typeExpression), memberDecl);
- }
- }
- objectFlags |= ts.getObjectFlags(type) & 3670016 /* PropagatingFlags */;
- var nameType = computedNameType && isTypeUsableAsPropertyName(computedNameType) ? computedNameType : undefined;
- var prop = nameType ?
- createSymbol(4 /* Property */ | member.flags, getPropertyNameFromType(nameType), checkFlags | 4096 /* Late */) :
- createSymbol(4 /* Property */ | member.flags, member.escapedName, checkFlags);
- if (nameType) {
- prop.nameType = nameType;
- }
- if (inDestructuringPattern) {
- // If object literal is an assignment pattern and if the assignment pattern specifies a default value
- // for the property, make the property optional.
- var isOptional = (memberDecl.kind === 281 /* PropertyAssignment */ && hasDefaultValue(memberDecl.initializer)) ||
- (memberDecl.kind === 282 /* ShorthandPropertyAssignment */ && memberDecl.objectAssignmentInitializer);
- if (isOptional) {
- prop.flags |= 16777216 /* Optional */;
- }
+ var flags = flow.flags;
+ if (flags & 4096 /* Shared */) {
+ if (!noCacheCheck) {
+ var id = getFlowNodeId(flow);
+ var reachable = flowNodeReachable[id];
+ return reachable !== undefined ? reachable : (flowNodeReachable[id] = isReachableFlowNodeWorker(flow, /*noCacheCheck*/ true));
}
- else if (contextualTypeHasPattern && !(ts.getObjectFlags(contextualType) & 512 /* ObjectLiteralPatternWithComputedProperties */)) {
- // If object literal is contextually typed by the implied type of a binding pattern, and if the
- // binding pattern specifies a default value for the property, make the property optional.
- var impliedProp = getPropertyOfType(contextualType, member.escapedName);
- if (impliedProp) {
- prop.flags |= impliedProp.flags & 16777216 /* Optional */;
+ noCacheCheck = false;
+ }
+ if (flags & (16 /* Assignment */ | 96 /* Condition */ | 256 /* ArrayMutation */)) {
+ flow = flow.antecedent;
+ }
+ else if (flags & 512 /* Call */) {
+ var signature = getEffectsSignature(flow.node);
+ if (signature) {
+ var predicate = getTypePredicateOfSignature(signature);
+ if (predicate && predicate.kind === 3 /* AssertsIdentifier */ && !predicate.type) {
+ var predicateArgument = flow.node.arguments[predicate.parameterIndex];
+ if (predicateArgument && isFalseExpression(predicateArgument)) {
+ return false;
+ }
}
- else if (!compilerOptions.suppressExcessPropertyErrors && !getIndexInfoOfType(contextualType, 0 /* String */)) {
- error(memberDecl.name, ts.Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, symbolToString(member), typeToString(contextualType));
+ if (getReturnTypeOfSignature(signature).flags & 131072 /* Never */) {
+ return false;
}
}
- prop.declarations = member.declarations;
- prop.parent = member.parent;
- if (member.valueDeclaration) {
- prop.valueDeclaration = member.valueDeclaration;
- }
- prop.type = type;
- prop.target = member;
- member = prop;
- allPropertiesTable === null || allPropertiesTable === void 0 ? void 0 : allPropertiesTable.set(prop.escapedName, prop);
+ flow = flow.antecedent;
}
- else if (memberDecl.kind === 283 /* SpreadAssignment */) {
- if (languageVersion < 2 /* ES2015 */) {
- checkExternalEmitHelpers(memberDecl, 2 /* Assign */);
- }
- if (propertiesArray.length > 0) {
- spread = getSpreadType(spread, createObjectLiteralType(), node.symbol, objectFlags, inConstContext);
- propertiesArray = [];
- propertiesTable = ts.createSymbolTable();
- hasComputedStringProperty = false;
- hasComputedNumberProperty = false;
- }
- var type = getReducedType(checkExpression(memberDecl.expression));
- if (!isValidSpreadType(type)) {
- error(memberDecl, ts.Diagnostics.Spread_types_may_only_be_created_from_object_types);
- return errorType;
- }
- if (allPropertiesTable) {
- checkSpreadPropOverrides(type, allPropertiesTable, memberDecl);
- }
- spread = getSpreadType(spread, type, node.symbol, objectFlags, inConstContext);
- offset = i + 1;
- continue;
+ else if (flags & 4 /* BranchLabel */) {
+ // A branching point is reachable if any branch is reachable.
+ return ts.some(flow.antecedents, function (f) { return isReachableFlowNodeWorker(f, /*noCacheCheck*/ false); });
}
- else {
- // TypeScript 1.0 spec (April 2014)
- // A get accessor declaration is processed in the same manner as
- // an ordinary function declaration(section 6.1) with no parameters.
- // A set accessor declaration is processed in the same manner
- // as an ordinary function declaration with a single parameter and a Void return type.
- ts.Debug.assert(memberDecl.kind === 163 /* GetAccessor */ || memberDecl.kind === 164 /* SetAccessor */);
- checkNodeDeferred(memberDecl);
+ else if (flags & 8 /* LoopLabel */) {
+ // A loop is reachable if the control flow path that leads to the top is reachable.
+ flow = flow.antecedents[0];
}
- if (computedNameType && !(computedNameType.flags & 8576 /* StringOrNumberLiteralOrUnique */)) {
- if (isTypeAssignableTo(computedNameType, stringNumberSymbolType)) {
- if (isTypeAssignableTo(computedNameType, numberType)) {
- hasComputedNumberProperty = true;
- }
- else {
- hasComputedStringProperty = true;
- }
- if (inDestructuringPattern) {
- patternWithComputedProperties = true;
- }
+ else if (flags & 128 /* SwitchClause */) {
+ // The control flow path representing an unmatched value in a switch statement with
+ // no default clause is unreachable if the switch statement is exhaustive.
+ if (flow.clauseStart === flow.clauseEnd && isExhaustiveSwitchStatement(flow.switchStatement)) {
+ return false;
}
+ flow = flow.antecedent;
+ }
+ else if (flags & 1024 /* ReduceLabel */) {
+ // Cache is unreliable once we start adjusting labels
+ lastFlowNode = undefined;
+ var target = flow.target;
+ var saveAntecedents = target.antecedents;
+ target.antecedents = flow.antecedents;
+ var result = isReachableFlowNodeWorker(flow.antecedent, /*noCacheCheck*/ false);
+ target.antecedents = saveAntecedents;
+ return result;
}
else {
- propertiesTable.set(member.escapedName, member);
+ return !(flags & 1 /* Unreachable */);
}
- propertiesArray.push(member);
}
- // If object literal is contextually typed by the implied type of a binding pattern, augment the result
- // type with those properties for which the binding pattern specifies a default value.
- // If the object literal is spread into another object literal, skip this step and let the top-level object
- // literal handle it instead.
- if (contextualTypeHasPattern && node.parent.kind !== 283 /* SpreadAssignment */) {
- for (var _b = 0, _c = getPropertiesOfType(contextualType); _b < _c.length; _b++) {
- var prop = _c[_b];
- if (!propertiesTable.get(prop.escapedName) && !getPropertyOfType(spread, prop.escapedName)) {
- if (!(prop.flags & 16777216 /* Optional */)) {
- error(prop.valueDeclaration || prop.bindingElement, ts.Diagnostics.Initializer_provides_no_value_for_this_binding_element_and_the_binding_element_has_no_default_value);
- }
- propertiesTable.set(prop.escapedName, prop);
- propertiesArray.push(prop);
+ }
+ // Return true if the given flow node is preceded by a 'super(...)' call in every possible code path
+ // leading to the node.
+ function isPostSuperFlowNode(flow, noCacheCheck) {
+ while (true) {
+ var flags = flow.flags;
+ if (flags & 4096 /* Shared */) {
+ if (!noCacheCheck) {
+ var id = getFlowNodeId(flow);
+ var postSuper = flowNodePostSuper[id];
+ return postSuper !== undefined ? postSuper : (flowNodePostSuper[id] = isPostSuperFlowNode(flow, /*noCacheCheck*/ true));
}
+ noCacheCheck = false;
}
- }
- if (spread !== emptyObjectType) {
- if (propertiesArray.length > 0) {
- spread = getSpreadType(spread, createObjectLiteralType(), node.symbol, objectFlags, inConstContext);
- propertiesArray = [];
- propertiesTable = ts.createSymbolTable();
- hasComputedStringProperty = false;
- hasComputedNumberProperty = false;
+ if (flags & (16 /* Assignment */ | 96 /* Condition */ | 256 /* ArrayMutation */ | 128 /* SwitchClause */)) {
+ flow = flow.antecedent;
}
- // remap the raw emptyObjectType fed in at the top into a fresh empty object literal type, unique to this use site
- return mapType(spread, function (t) { return t === emptyObjectType ? createObjectLiteralType() : t; });
- }
- return createObjectLiteralType();
- function createObjectLiteralType() {
- var stringIndexInfo = hasComputedStringProperty ? getObjectLiteralIndexInfo(node, offset, propertiesArray, 0 /* String */) : undefined;
- var numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node, offset, propertiesArray, 1 /* Number */) : undefined;
- var result = createAnonymousType(node.symbol, propertiesTable, ts.emptyArray, ts.emptyArray, stringIndexInfo, numberIndexInfo);
- result.objectFlags |= objectFlags | 128 /* ObjectLiteral */ | 1048576 /* ContainsObjectOrArrayLiteral */;
- if (isJSObjectLiteral) {
- result.objectFlags |= 16384 /* JSLiteral */;
+ else if (flags & 512 /* Call */) {
+ if (flow.node.expression.kind === 105 /* SuperKeyword */) {
+ return true;
+ }
+ flow = flow.antecedent;
}
- if (patternWithComputedProperties) {
- result.objectFlags |= 512 /* ObjectLiteralPatternWithComputedProperties */;
+ else if (flags & 4 /* BranchLabel */) {
+ // A branching point is post-super if every branch is post-super.
+ return ts.every(flow.antecedents, function (f) { return isPostSuperFlowNode(f, /*noCacheCheck*/ false); });
}
- if (inDestructuringPattern) {
- result.pattern = node;
+ else if (flags & 8 /* LoopLabel */) {
+ // A loop is post-super if the control flow path that leads to the top is post-super.
+ flow = flow.antecedents[0];
}
- return result;
- }
- }
- function isValidSpreadType(type) {
- if (type.flags & 63176704 /* Instantiable */) {
- var constraint = getBaseConstraintOfType(type);
- if (constraint !== undefined) {
- return isValidSpreadType(constraint);
+ else if (flags & 1024 /* ReduceLabel */) {
+ var target = flow.target;
+ var saveAntecedents = target.antecedents;
+ target.antecedents = flow.antecedents;
+ var result = isPostSuperFlowNode(flow.antecedent, /*noCacheCheck*/ false);
+ target.antecedents = saveAntecedents;
+ return result;
+ }
+ else {
+ // Unreachable nodes are considered post-super to silence errors
+ return !!(flags & 1 /* Unreachable */);
}
}
- return !!(type.flags & (1 /* Any */ | 67108864 /* NonPrimitive */ | 524288 /* Object */ | 58982400 /* InstantiableNonPrimitive */) ||
- getFalsyFlags(type) & 117632 /* DefinitelyFalsy */ && isValidSpreadType(removeDefinitelyFalsyTypes(type)) ||
- type.flags & 3145728 /* UnionOrIntersection */ && ts.every(type.types, isValidSpreadType));
- }
- function checkJsxSelfClosingElementDeferred(node) {
- checkJsxOpeningLikeElementOrOpeningFragment(node);
- resolveUntypedCall(node); // ensure type arguments and parameters are typechecked, even if there is an arity error
- }
- function checkJsxSelfClosingElement(node, _checkMode) {
- checkNodeDeferred(node);
- return getJsxElementTypeAt(node) || anyType;
}
- function checkJsxElementDeferred(node) {
- // Check attributes
- checkJsxOpeningLikeElementOrOpeningFragment(node.openingElement);
- // Perform resolution on the closing tag so that rename/go to definition/etc work
- if (isJsxIntrinsicIdentifier(node.closingElement.tagName)) {
- getIntrinsicTagSymbol(node.closingElement);
+ function getFlowTypeOfReference(reference, declaredType, initialType, flowContainer, couldBeUninitialized) {
+ if (initialType === void 0) { initialType = declaredType; }
+ var key;
+ var isKeySet = false;
+ var flowDepth = 0;
+ if (flowAnalysisDisabled) {
+ return errorType;
}
- else {
- checkExpression(node.closingElement.tagName);
+ if (!reference.flowNode || !couldBeUninitialized && !(declaredType.flags & 536624127 /* Narrowable */)) {
+ return declaredType;
}
- checkJsxChildren(node);
- }
- function checkJsxElement(node, _checkMode) {
- checkNodeDeferred(node);
- return getJsxElementTypeAt(node) || anyType;
- }
- function checkJsxFragment(node) {
- checkJsxOpeningLikeElementOrOpeningFragment(node.openingFragment);
- if (compilerOptions.jsx === 2 /* React */ && (compilerOptions.jsxFactory || ts.getSourceFileOfNode(node).pragmas.has("jsx"))) {
- error(node, compilerOptions.jsxFactory
- ? ts.Diagnostics.JSX_fragment_is_not_supported_when_using_jsxFactory
- : ts.Diagnostics.JSX_fragment_is_not_supported_when_using_an_inline_JSX_factory_pragma);
+ flowInvocationCount++;
+ var sharedFlowStart = sharedFlowCount;
+ var evolvedType = getTypeFromFlowType(getTypeAtFlowNode(reference.flowNode));
+ sharedFlowCount = sharedFlowStart;
+ // When the reference is 'x' in an 'x.length', 'x.push(value)', 'x.unshift(value)' or x[n] = value' operation,
+ // we give type 'any[]' to 'x' instead of using the type determined by control flow analysis such that operations
+ // on empty arrays are possible without implicit any errors and new element types can be inferred without
+ // type mismatch errors.
+ var resultType = ts.getObjectFlags(evolvedType) & 256 /* EvolvingArray */ && isEvolvingArrayOperationTarget(reference) ? autoArrayType : finalizeEvolvingArrayType(evolvedType);
+ if (resultType === unreachableNeverType || reference.parent && reference.parent.kind === 225 /* NonNullExpression */ && getTypeWithFacts(resultType, 2097152 /* NEUndefinedOrNull */).flags & 131072 /* Never */) {
+ return declaredType;
}
- checkJsxChildren(node);
- return getJsxElementTypeAt(node) || anyType;
- }
- /**
- * Returns true iff the JSX element name would be a valid JS identifier, ignoring restrictions about keywords not being identifiers
- */
- function isUnhyphenatedJsxName(name) {
- // - is the only character supported in JSX attribute names that isn't valid in JavaScript identifiers
- return !ts.stringContains(name, "-");
- }
- /**
- * Returns true iff React would emit this tag name as a string rather than an identifier or qualified name
- */
- function isJsxIntrinsicIdentifier(tagName) {
- return tagName.kind === 75 /* Identifier */ && ts.isIntrinsicJsxName(tagName.escapedText);
- }
- function checkJsxAttribute(node, checkMode) {
- return node.initializer
- ? checkExpressionForMutableLocation(node.initializer, checkMode)
- : trueType; // is sugar for
- }
- /**
- * Get attributes type of the JSX opening-like element. The result is from resolving "attributes" property of the opening-like element.
- *
- * @param openingLikeElement a JSX opening-like element
- * @param filter a function to remove attributes that will not participate in checking whether attributes are assignable
- * @return an anonymous type (similar to the one returned by checkObjectLiteral) in which its properties are attributes property.
- * @remarks Because this function calls getSpreadType, it needs to use the same checks as checkObjectLiteral,
- * which also calls getSpreadType.
- */
- function createJsxAttributesTypeFromAttributesProperty(openingLikeElement, checkMode) {
- var attributes = openingLikeElement.attributes;
- var allAttributesTable = strictNullChecks ? ts.createSymbolTable() : undefined;
- var attributesTable = ts.createSymbolTable();
- var spread = emptyJsxObjectType;
- var hasSpreadAnyType = false;
- var typeToIntersect;
- var explicitlySpecifyChildrenAttribute = false;
- var objectFlags = 4096 /* JsxAttributes */;
- var jsxChildrenPropertyName = getJsxElementChildrenPropertyName(getJsxNamespaceAt(openingLikeElement));
- for (var _i = 0, _a = attributes.properties; _i < _a.length; _i++) {
- var attributeDecl = _a[_i];
- var member = attributeDecl.symbol;
- if (ts.isJsxAttribute(attributeDecl)) {
- var exprType = checkJsxAttribute(attributeDecl, checkMode);
- objectFlags |= ts.getObjectFlags(exprType) & 3670016 /* PropagatingFlags */;
- var attributeSymbol = createSymbol(4 /* Property */ | 33554432 /* Transient */ | member.flags, member.escapedName);
- attributeSymbol.declarations = member.declarations;
- attributeSymbol.parent = member.parent;
- if (member.valueDeclaration) {
- attributeSymbol.valueDeclaration = member.valueDeclaration;
- }
- attributeSymbol.type = exprType;
- attributeSymbol.target = member;
- attributesTable.set(attributeSymbol.escapedName, attributeSymbol);
- allAttributesTable === null || allAttributesTable === void 0 ? void 0 : allAttributesTable.set(attributeSymbol.escapedName, attributeSymbol);
- if (attributeDecl.name.escapedText === jsxChildrenPropertyName) {
- explicitlySpecifyChildrenAttribute = true;
- }
+ return resultType;
+ function getOrSetCacheKey() {
+ if (isKeySet) {
+ return key;
}
- else {
- ts.Debug.assert(attributeDecl.kind === 275 /* JsxSpreadAttribute */);
- if (attributesTable.size > 0) {
- spread = getSpreadType(spread, createJsxAttributesType(), attributes.symbol, objectFlags, /*readonly*/ false);
- attributesTable = ts.createSymbolTable();
+ isKeySet = true;
+ return key = getFlowCacheKey(reference, declaredType, initialType, flowContainer);
+ }
+ function getTypeAtFlowNode(flow) {
+ if (flowDepth === 2000) {
+ // We have made 2000 recursive invocations. To avoid overflowing the call stack we report an error
+ // and disable further control flow analysis in the containing function or module body.
+ ts.tracing.instant("check" /* Check */, "getTypeAtFlowNode_DepthLimit", { flowId: flow.id });
+ flowAnalysisDisabled = true;
+ reportFlowControlError(reference);
+ return errorType;
+ }
+ flowDepth++;
+ var sharedFlow;
+ while (true) {
+ var flags = flow.flags;
+ if (flags & 4096 /* Shared */) {
+ // We cache results of flow type resolution for shared nodes that were previously visited in
+ // the same getFlowTypeOfReference invocation. A node is considered shared when it is the
+ // antecedent of more than one node.
+ for (var i = sharedFlowStart; i < sharedFlowCount; i++) {
+ if (sharedFlowNodes[i] === flow) {
+ flowDepth--;
+ return sharedFlowTypes[i];
+ }
+ }
+ sharedFlow = flow;
}
- var exprType = getReducedType(checkExpressionCached(attributeDecl.expression, checkMode));
- if (isTypeAny(exprType)) {
- hasSpreadAnyType = true;
+ var type = void 0;
+ if (flags & 16 /* Assignment */) {
+ type = getTypeAtFlowAssignment(flow);
+ if (!type) {
+ flow = flow.antecedent;
+ continue;
+ }
}
- if (isValidSpreadType(exprType)) {
- spread = getSpreadType(spread, exprType, attributes.symbol, objectFlags, /*readonly*/ false);
- if (allAttributesTable) {
- checkSpreadPropOverrides(exprType, allAttributesTable, attributeDecl);
+ else if (flags & 512 /* Call */) {
+ type = getTypeAtFlowCall(flow);
+ if (!type) {
+ flow = flow.antecedent;
+ continue;
}
}
- else {
- typeToIntersect = typeToIntersect ? getIntersectionType([typeToIntersect, exprType]) : exprType;
+ else if (flags & 96 /* Condition */) {
+ type = getTypeAtFlowCondition(flow);
}
- }
- }
- if (!hasSpreadAnyType) {
- if (attributesTable.size > 0) {
- spread = getSpreadType(spread, createJsxAttributesType(), attributes.symbol, objectFlags, /*readonly*/ false);
- }
- }
- // Handle children attribute
- var parent = openingLikeElement.parent.kind === 266 /* JsxElement */ ? openingLikeElement.parent : undefined;
- // We have to check that openingElement of the parent is the one we are visiting as this may not be true for selfClosingElement
- if (parent && parent.openingElement === openingLikeElement && parent.children.length > 0) {
- var childrenTypes = checkJsxChildren(parent, checkMode);
- if (!hasSpreadAnyType && jsxChildrenPropertyName && jsxChildrenPropertyName !== "") {
- // Error if there is a attribute named "children" explicitly specified and children element.
- // This is because children element will overwrite the value from attributes.
- // Note: we will not warn "children" attribute overwritten if "children" attribute is specified in object spread.
- if (explicitlySpecifyChildrenAttribute) {
- error(attributes, ts.Diagnostics._0_are_specified_twice_The_attribute_named_0_will_be_overwritten, ts.unescapeLeadingUnderscores(jsxChildrenPropertyName));
+ else if (flags & 128 /* SwitchClause */) {
+ type = getTypeAtSwitchClause(flow);
}
- var contextualType = getApparentTypeOfContextualType(openingLikeElement.attributes);
- var childrenContextualType = contextualType && getTypeOfPropertyOfContextualType(contextualType, jsxChildrenPropertyName);
- // If there are children in the body of JSX element, create dummy attribute "children" with the union of children types so that it will pass the attribute checking process
- var childrenPropSymbol = createSymbol(4 /* Property */ | 33554432 /* Transient */, jsxChildrenPropertyName);
- childrenPropSymbol.type = childrenTypes.length === 1 ?
- childrenTypes[0] :
- (getArrayLiteralTupleTypeIfApplicable(childrenTypes, childrenContextualType, /*hasRestElement*/ false) || createArrayType(getUnionType(childrenTypes)));
- // Fake up a property declaration for the children
- childrenPropSymbol.valueDeclaration = ts.createPropertySignature(/*modifiers*/ undefined, ts.unescapeLeadingUnderscores(jsxChildrenPropertyName), /*questionToken*/ undefined, /*type*/ undefined, /*initializer*/ undefined);
- childrenPropSymbol.valueDeclaration.parent = attributes;
- childrenPropSymbol.valueDeclaration.symbol = childrenPropSymbol;
- var childPropMap = ts.createSymbolTable();
- childPropMap.set(jsxChildrenPropertyName, childrenPropSymbol);
- spread = getSpreadType(spread, createAnonymousType(attributes.symbol, childPropMap, ts.emptyArray, ts.emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined), attributes.symbol, objectFlags, /*readonly*/ false);
- }
- }
- if (hasSpreadAnyType) {
- return anyType;
- }
- if (typeToIntersect && spread !== emptyJsxObjectType) {
- return getIntersectionType([typeToIntersect, spread]);
- }
- return typeToIntersect || (spread === emptyJsxObjectType ? createJsxAttributesType() : spread);
- /**
- * Create anonymous type from given attributes symbol table.
- * @param symbol a symbol of JsxAttributes containing attributes corresponding to attributesTable
- * @param attributesTable a symbol table of attributes property
- */
- function createJsxAttributesType() {
- objectFlags |= freshObjectLiteralFlag;
- var result = createAnonymousType(attributes.symbol, attributesTable, ts.emptyArray, ts.emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined);
- result.objectFlags |= objectFlags | 128 /* ObjectLiteral */ | 1048576 /* ContainsObjectOrArrayLiteral */;
- return result;
- }
- }
- function checkJsxChildren(node, checkMode) {
- var childrenTypes = [];
- for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
- var child = _a[_i];
- // In React, JSX text that contains only whitespaces will be ignored so we don't want to type-check that
- // because then type of children property will have constituent of string type.
- if (child.kind === 11 /* JsxText */) {
- if (!child.containsOnlyTriviaWhiteSpaces) {
- childrenTypes.push(stringType);
+ else if (flags & 12 /* Label */) {
+ if (flow.antecedents.length === 1) {
+ flow = flow.antecedents[0];
+ continue;
+ }
+ type = flags & 4 /* BranchLabel */ ?
+ getTypeAtFlowBranchLabel(flow) :
+ getTypeAtFlowLoopLabel(flow);
+ }
+ else if (flags & 256 /* ArrayMutation */) {
+ type = getTypeAtFlowArrayMutation(flow);
+ if (!type) {
+ flow = flow.antecedent;
+ continue;
+ }
}
- }
- else {
- childrenTypes.push(checkExpressionForMutableLocation(child, checkMode));
- }
- }
- return childrenTypes;
- }
- function checkSpreadPropOverrides(type, props, spread) {
- for (var _i = 0, _a = getPropertiesOfType(type); _i < _a.length; _i++) {
- var right = _a[_i];
- var left = props.get(right.escapedName);
- var rightType = getTypeOfSymbol(right);
- if (left && !maybeTypeOfKind(rightType, 98304 /* Nullable */) && !(maybeTypeOfKind(rightType, 3 /* AnyOrUnknown */) && right.flags & 16777216 /* Optional */)) {
- var diagnostic = error(left.valueDeclaration, ts.Diagnostics._0_is_specified_more_than_once_so_this_usage_will_be_overwritten, ts.unescapeLeadingUnderscores(left.escapedName));
- ts.addRelatedInfo(diagnostic, ts.createDiagnosticForNode(spread, ts.Diagnostics.This_spread_always_overwrites_this_property));
- }
- }
- }
- /**
- * Check attributes property of opening-like element. This function is called during chooseOverload to get call signature of a JSX opening-like element.
- * (See "checkApplicableSignatureForJsxOpeningLikeElement" for how the function is used)
- * @param node a JSXAttributes to be resolved of its type
- */
- function checkJsxAttributes(node, checkMode) {
- return createJsxAttributesTypeFromAttributesProperty(node.parent, checkMode);
- }
- function getJsxType(name, location) {
- var namespace = getJsxNamespaceAt(location);
- var exports = namespace && getExportsOfSymbol(namespace);
- var typeSymbol = exports && getSymbol(exports, name, 788968 /* Type */);
- return typeSymbol ? getDeclaredTypeOfSymbol(typeSymbol) : errorType;
- }
- /**
- * Looks up an intrinsic tag name and returns a symbol that either points to an intrinsic
- * property (in which case nodeLinks.jsxFlags will be IntrinsicNamedElement) or an intrinsic
- * string index signature (in which case nodeLinks.jsxFlags will be IntrinsicIndexedElement).
- * May also return unknownSymbol if both of these lookups fail.
- */
- function getIntrinsicTagSymbol(node) {
- var links = getNodeLinks(node);
- if (!links.resolvedSymbol) {
- var intrinsicElementsType = getJsxType(JsxNames.IntrinsicElements, node);
- if (intrinsicElementsType !== errorType) {
- // Property case
- if (!ts.isIdentifier(node.tagName))
- return ts.Debug.fail();
- var intrinsicProp = getPropertyOfType(intrinsicElementsType, node.tagName.escapedText);
- if (intrinsicProp) {
- links.jsxFlags |= 1 /* IntrinsicNamedElement */;
- return links.resolvedSymbol = intrinsicProp;
+ else if (flags & 1024 /* ReduceLabel */) {
+ var target = flow.target;
+ var saveAntecedents = target.antecedents;
+ target.antecedents = flow.antecedents;
+ type = getTypeAtFlowNode(flow.antecedent);
+ target.antecedents = saveAntecedents;
}
- // Intrinsic string indexer case
- var indexSignatureType = getIndexTypeOfType(intrinsicElementsType, 0 /* String */);
- if (indexSignatureType) {
- links.jsxFlags |= 2 /* IntrinsicIndexedElement */;
- return links.resolvedSymbol = intrinsicElementsType.symbol;
+ else if (flags & 2 /* Start */) {
+ // Check if we should continue with the control flow of the containing function.
+ var container = flow.node;
+ if (container && container !== flowContainer &&
+ reference.kind !== 201 /* PropertyAccessExpression */ &&
+ reference.kind !== 202 /* ElementAccessExpression */ &&
+ reference.kind !== 107 /* ThisKeyword */) {
+ flow = container.flowNode;
+ continue;
+ }
+ // At the top of the flow we have the initial type.
+ type = initialType;
}
- // Wasn't found
- error(node, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.idText(node.tagName), "JSX." + JsxNames.IntrinsicElements);
- return links.resolvedSymbol = unknownSymbol;
- }
- else {
- if (noImplicitAny) {
- error(node, ts.Diagnostics.JSX_element_implicitly_has_type_any_because_no_interface_JSX_0_exists, ts.unescapeLeadingUnderscores(JsxNames.IntrinsicElements));
+ else {
+ // Unreachable code errors are reported in the binding phase. Here we
+ // simply return the non-auto declared type to reduce follow-on errors.
+ type = convertAutoToAny(declaredType);
}
- return links.resolvedSymbol = unknownSymbol;
+ if (sharedFlow) {
+ // Record visited node and the associated type in the cache.
+ sharedFlowNodes[sharedFlowCount] = sharedFlow;
+ sharedFlowTypes[sharedFlowCount] = type;
+ sharedFlowCount++;
+ }
+ flowDepth--;
+ return type;
}
}
- return links.resolvedSymbol;
- }
- function getJsxNamespaceAt(location) {
- var links = location && getNodeLinks(location);
- if (links && links.jsxNamespace) {
- return links.jsxNamespace;
+ function getInitialOrAssignedType(flow) {
+ var node = flow.node;
+ return getConstraintForLocation(node.kind === 249 /* VariableDeclaration */ || node.kind === 198 /* BindingElement */ ?
+ getInitialType(node) :
+ getAssignedType(node), reference);
}
- if (!links || links.jsxNamespace !== false) {
- var namespaceName = getJsxNamespace(location);
- var resolvedNamespace = resolveName(location, namespaceName, 1920 /* Namespace */, /*diagnosticMessage*/ undefined, namespaceName, /*isUse*/ false);
- if (resolvedNamespace) {
- var candidate = resolveSymbol(getSymbol(getExportsOfSymbol(resolveSymbol(resolvedNamespace)), JsxNames.JSX, 1920 /* Namespace */));
- if (candidate) {
- if (links) {
- links.jsxNamespace = candidate;
+ function getTypeAtFlowAssignment(flow) {
+ var node = flow.node;
+ // Assignments only narrow the computed type if the declared type is a union type. Thus, we
+ // only need to evaluate the assigned type if the declared type is a union type.
+ if (isMatchingReference(reference, node)) {
+ if (!isReachableFlowNode(flow)) {
+ return unreachableNeverType;
+ }
+ if (ts.getAssignmentTargetKind(node) === 2 /* Compound */) {
+ var flowType = getTypeAtFlowNode(flow.antecedent);
+ return createFlowType(getBaseTypeOfLiteralType(getTypeFromFlowType(flowType)), isIncomplete(flowType));
+ }
+ if (declaredType === autoType || declaredType === autoArrayType) {
+ if (isEmptyArrayAssignment(node)) {
+ return getEvolvingArrayType(neverType);
}
- return candidate;
+ var assignedType = getWidenedLiteralType(getInitialOrAssignedType(flow));
+ return isTypeAssignableTo(assignedType, declaredType) ? assignedType : anyArrayType;
}
- if (links) {
- links.jsxNamespace = false;
+ if (declaredType.flags & 1048576 /* Union */) {
+ return getAssignmentReducedType(declaredType, getInitialOrAssignedType(flow));
}
+ return declaredType;
}
- }
- // JSX global fallback
- return getGlobalSymbol(JsxNames.JSX, 1920 /* Namespace */, /*diagnosticMessage*/ undefined); // TODO: GH#18217
- }
- /**
- * Look into JSX namespace and then look for container with matching name as nameOfAttribPropContainer.
- * Get a single property from that container if existed. Report an error if there are more than one property.
- *
- * @param nameOfAttribPropContainer a string of value JsxNames.ElementAttributesPropertyNameContainer or JsxNames.ElementChildrenAttributeNameContainer
- * if other string is given or the container doesn't exist, return undefined.
- */
- function getNameFromJsxElementAttributesContainer(nameOfAttribPropContainer, jsxNamespace) {
- // JSX.ElementAttributesProperty | JSX.ElementChildrenAttribute [symbol]
- var jsxElementAttribPropInterfaceSym = jsxNamespace && getSymbol(jsxNamespace.exports, nameOfAttribPropContainer, 788968 /* Type */);
- // JSX.ElementAttributesProperty | JSX.ElementChildrenAttribute [type]
- var jsxElementAttribPropInterfaceType = jsxElementAttribPropInterfaceSym && getDeclaredTypeOfSymbol(jsxElementAttribPropInterfaceSym);
- // The properties of JSX.ElementAttributesProperty | JSX.ElementChildrenAttribute
- var propertiesOfJsxElementAttribPropInterface = jsxElementAttribPropInterfaceType && getPropertiesOfType(jsxElementAttribPropInterfaceType);
- if (propertiesOfJsxElementAttribPropInterface) {
- // Element Attributes has zero properties, so the element attributes type will be the class instance type
- if (propertiesOfJsxElementAttribPropInterface.length === 0) {
- return "";
- }
- // Element Attributes has one property, so the element attributes type will be the type of the corresponding
- // property of the class instance type
- else if (propertiesOfJsxElementAttribPropInterface.length === 1) {
- return propertiesOfJsxElementAttribPropInterface[0].escapedName;
+ // We didn't have a direct match. However, if the reference is a dotted name, this
+ // may be an assignment to a left hand part of the reference. For example, for a
+ // reference 'x.y.z', we may be at an assignment to 'x.y' or 'x'. In that case,
+ // return the declared type.
+ if (containsMatchingReference(reference, node)) {
+ if (!isReachableFlowNode(flow)) {
+ return unreachableNeverType;
+ }
+ // A matching dotted name might also be an expando property on a function *expression*,
+ // in which case we continue control flow analysis back to the function's declaration
+ if (ts.isVariableDeclaration(node) && (ts.isInJSFile(node) || ts.isVarConst(node))) {
+ var init = ts.getDeclaredExpandoInitializer(node);
+ if (init && (init.kind === 208 /* FunctionExpression */ || init.kind === 209 /* ArrowFunction */)) {
+ return getTypeAtFlowNode(flow.antecedent);
+ }
+ }
+ return declaredType;
}
- else if (propertiesOfJsxElementAttribPropInterface.length > 1) {
- // More than one property on ElementAttributesProperty is an error
- error(jsxElementAttribPropInterfaceSym.declarations[0], ts.Diagnostics.The_global_type_JSX_0_may_not_have_more_than_one_property, ts.unescapeLeadingUnderscores(nameOfAttribPropContainer));
+ // for (const _ in ref) acts as a nonnull on ref
+ if (ts.isVariableDeclaration(node) && node.parent.parent.kind === 238 /* ForInStatement */ && isMatchingReference(reference, node.parent.parent.expression)) {
+ return getNonNullableTypeIfNeeded(getTypeFromFlowType(getTypeAtFlowNode(flow.antecedent)));
}
+ // Assignment doesn't affect reference
+ return undefined;
}
- return undefined;
- }
- function getJsxLibraryManagedAttributes(jsxNamespace) {
- // JSX.LibraryManagedAttributes [symbol]
- return jsxNamespace && getSymbol(jsxNamespace.exports, JsxNames.LibraryManagedAttributes, 788968 /* Type */);
- }
- /// e.g. "props" for React.d.ts,
- /// or 'undefined' if ElementAttributesProperty doesn't exist (which means all
- /// non-intrinsic elements' attributes type is 'any'),
- /// or '' if it has 0 properties (which means every
- /// non-intrinsic elements' attributes type is the element instance type)
- function getJsxElementPropertiesName(jsxNamespace) {
- return getNameFromJsxElementAttributesContainer(JsxNames.ElementAttributesPropertyNameContainer, jsxNamespace);
- }
- function getJsxElementChildrenPropertyName(jsxNamespace) {
- return getNameFromJsxElementAttributesContainer(JsxNames.ElementChildrenAttributeNameContainer, jsxNamespace);
- }
- function getUninstantiatedJsxSignaturesOfType(elementType, caller) {
- if (elementType.flags & 4 /* String */) {
- return [anySignature];
- }
- else if (elementType.flags & 128 /* StringLiteral */) {
- var intrinsicType = getIntrinsicAttributesTypeFromStringLiteralType(elementType, caller);
- if (!intrinsicType) {
- error(caller, ts.Diagnostics.Property_0_does_not_exist_on_type_1, elementType.value, "JSX." + JsxNames.IntrinsicElements);
- return ts.emptyArray;
+ function narrowTypeByAssertion(type, expr) {
+ var node = ts.skipParentheses(expr);
+ if (node.kind === 94 /* FalseKeyword */) {
+ return unreachableNeverType;
}
- else {
- var fakeSignature = createSignatureForJSXIntrinsic(caller, intrinsicType);
- return [fakeSignature];
+ if (node.kind === 216 /* BinaryExpression */) {
+ if (node.operatorToken.kind === 55 /* AmpersandAmpersandToken */) {
+ return narrowTypeByAssertion(narrowTypeByAssertion(type, node.left), node.right);
+ }
+ if (node.operatorToken.kind === 56 /* BarBarToken */) {
+ return getUnionType([narrowTypeByAssertion(type, node.left), narrowTypeByAssertion(type, node.right)]);
+ }
}
+ return narrowType(type, node, /*assumeTrue*/ true);
}
- var apparentElemType = getApparentType(elementType);
- // Resolve the signatures, preferring constructor
- var signatures = getSignaturesOfType(apparentElemType, 1 /* Construct */);
- if (signatures.length === 0) {
- // No construct signatures, try call signatures
- signatures = getSignaturesOfType(apparentElemType, 0 /* Call */);
- }
- if (signatures.length === 0 && apparentElemType.flags & 1048576 /* Union */) {
- // If each member has some combination of new/call signatures; make a union signature list for those
- signatures = getUnionSignatures(ts.map(apparentElemType.types, function (t) { return getUninstantiatedJsxSignaturesOfType(t, caller); }));
- }
- return signatures;
- }
- function getIntrinsicAttributesTypeFromStringLiteralType(type, location) {
- // If the elemType is a stringLiteral type, we can then provide a check to make sure that the string literal type is one of the Jsx intrinsic element type
- // For example:
- // var CustomTag: "h1" = "h1";
- // Hello World
- var intrinsicElementsType = getJsxType(JsxNames.IntrinsicElements, location);
- if (intrinsicElementsType !== errorType) {
- var stringLiteralTypeName = type.value;
- var intrinsicProp = getPropertyOfType(intrinsicElementsType, ts.escapeLeadingUnderscores(stringLiteralTypeName));
- if (intrinsicProp) {
- return getTypeOfSymbol(intrinsicProp);
- }
- var indexSignatureType = getIndexTypeOfType(intrinsicElementsType, 0 /* String */);
- if (indexSignatureType) {
- return indexSignatureType;
+ function getTypeAtFlowCall(flow) {
+ var signature = getEffectsSignature(flow.node);
+ if (signature) {
+ var predicate = getTypePredicateOfSignature(signature);
+ if (predicate && (predicate.kind === 2 /* AssertsThis */ || predicate.kind === 3 /* AssertsIdentifier */)) {
+ var flowType = getTypeAtFlowNode(flow.antecedent);
+ var type = finalizeEvolvingArrayType(getTypeFromFlowType(flowType));
+ var narrowedType = predicate.type ? narrowTypeByTypePredicate(type, predicate, flow.node, /*assumeTrue*/ true) :
+ predicate.kind === 3 /* AssertsIdentifier */ && predicate.parameterIndex >= 0 && predicate.parameterIndex < flow.node.arguments.length ? narrowTypeByAssertion(type, flow.node.arguments[predicate.parameterIndex]) :
+ type;
+ return narrowedType === type ? flowType : createFlowType(narrowedType, isIncomplete(flowType));
+ }
+ if (getReturnTypeOfSignature(signature).flags & 131072 /* Never */) {
+ return unreachableNeverType;
+ }
}
return undefined;
}
- // If we need to report an error, we already done so here. So just return any to prevent any more error downstream
- return anyType;
- }
- function checkJsxReturnAssignableToAppropriateBound(refKind, elemInstanceType, openingLikeElement) {
- if (refKind === 1 /* Function */) {
- var sfcReturnConstraint = getJsxStatelessElementTypeAt(openingLikeElement);
- if (sfcReturnConstraint) {
- checkTypeRelatedTo(elemInstanceType, sfcReturnConstraint, assignableRelation, openingLikeElement.tagName, ts.Diagnostics.Its_return_type_0_is_not_a_valid_JSX_element, generateInitialErrorChain);
+ function getTypeAtFlowArrayMutation(flow) {
+ if (declaredType === autoType || declaredType === autoArrayType) {
+ var node = flow.node;
+ var expr = node.kind === 203 /* CallExpression */ ?
+ node.expression.expression :
+ node.left.expression;
+ if (isMatchingReference(reference, getReferenceCandidate(expr))) {
+ var flowType = getTypeAtFlowNode(flow.antecedent);
+ var type = getTypeFromFlowType(flowType);
+ if (ts.getObjectFlags(type) & 256 /* EvolvingArray */) {
+ var evolvedType_1 = type;
+ if (node.kind === 203 /* CallExpression */) {
+ for (var _i = 0, _a = node.arguments; _i < _a.length; _i++) {
+ var arg = _a[_i];
+ evolvedType_1 = addEvolvingArrayElementType(evolvedType_1, arg);
+ }
+ }
+ else {
+ // We must get the context free expression type so as to not recur in an uncached fashion on the LHS (which causes exponential blowup in compile time)
+ var indexType = getContextFreeTypeOfExpression(node.left.argumentExpression);
+ if (isTypeAssignableToKind(indexType, 296 /* NumberLike */)) {
+ evolvedType_1 = addEvolvingArrayElementType(evolvedType_1, node.right);
+ }
+ }
+ return evolvedType_1 === type ? flowType : createFlowType(evolvedType_1, isIncomplete(flowType));
+ }
+ return flowType;
+ }
}
+ return undefined;
}
- else if (refKind === 0 /* Component */) {
- var classConstraint = getJsxElementClassTypeAt(openingLikeElement);
- if (classConstraint) {
- // Issue an error if this return type isn't assignable to JSX.ElementClass, failing that
- checkTypeRelatedTo(elemInstanceType, classConstraint, assignableRelation, openingLikeElement.tagName, ts.Diagnostics.Its_instance_type_0_is_not_a_valid_JSX_element, generateInitialErrorChain);
+ function getTypeAtFlowCondition(flow) {
+ var flowType = getTypeAtFlowNode(flow.antecedent);
+ var type = getTypeFromFlowType(flowType);
+ if (type.flags & 131072 /* Never */) {
+ return flowType;
}
- }
- else { // Mixed
- var sfcReturnConstraint = getJsxStatelessElementTypeAt(openingLikeElement);
- var classConstraint = getJsxElementClassTypeAt(openingLikeElement);
- if (!sfcReturnConstraint || !classConstraint) {
- return;
+ // If we have an antecedent type (meaning we're reachable in some way), we first
+ // attempt to narrow the antecedent type. If that produces the never type, and if
+ // the antecedent type is incomplete (i.e. a transient type in a loop), then we
+ // take the type guard as an indication that control *could* reach here once we
+ // have the complete type. We proceed by switching to the silent never type which
+ // doesn't report errors when operators are applied to it. Note that this is the
+ // *only* place a silent never type is ever generated.
+ var assumeTrue = (flow.flags & 32 /* TrueCondition */) !== 0;
+ var nonEvolvingType = finalizeEvolvingArrayType(type);
+ var narrowedType = narrowType(nonEvolvingType, flow.node, assumeTrue);
+ if (narrowedType === nonEvolvingType) {
+ return flowType;
}
- var combined = getUnionType([sfcReturnConstraint, classConstraint]);
- checkTypeRelatedTo(elemInstanceType, combined, assignableRelation, openingLikeElement.tagName, ts.Diagnostics.Its_element_type_0_is_not_a_valid_JSX_element, generateInitialErrorChain);
- }
- function generateInitialErrorChain() {
- var componentName = ts.getTextOfNode(openingLikeElement.tagName);
- return ts.chainDiagnosticMessages(/* details */ undefined, ts.Diagnostics._0_cannot_be_used_as_a_JSX_component, componentName);
+ return createFlowType(narrowedType, isIncomplete(flowType));
}
- }
- /**
- * Get attributes type of the given intrinsic opening-like Jsx element by resolving the tag name.
- * The function is intended to be called from a function which has checked that the opening element is an intrinsic element.
- * @param node an intrinsic JSX opening-like element
- */
- function getIntrinsicAttributesTypeFromJsxOpeningLikeElement(node) {
- ts.Debug.assert(isJsxIntrinsicIdentifier(node.tagName));
- var links = getNodeLinks(node);
- if (!links.resolvedJsxElementAttributesType) {
- var symbol = getIntrinsicTagSymbol(node);
- if (links.jsxFlags & 1 /* IntrinsicNamedElement */) {
- return links.resolvedJsxElementAttributesType = getTypeOfSymbol(symbol);
+ function getTypeAtSwitchClause(flow) {
+ var expr = flow.switchStatement.expression;
+ var flowType = getTypeAtFlowNode(flow.antecedent);
+ var type = getTypeFromFlowType(flowType);
+ if (isMatchingReference(reference, expr)) {
+ type = narrowTypeBySwitchOnDiscriminant(type, flow.switchStatement, flow.clauseStart, flow.clauseEnd);
}
- else if (links.jsxFlags & 2 /* IntrinsicIndexedElement */) {
- return links.resolvedJsxElementAttributesType =
- getIndexTypeOfType(getDeclaredTypeOfSymbol(symbol), 0 /* String */);
+ else if (expr.kind === 211 /* TypeOfExpression */ && isMatchingReference(reference, expr.expression)) {
+ type = narrowBySwitchOnTypeOf(type, flow.switchStatement, flow.clauseStart, flow.clauseEnd);
}
else {
- return links.resolvedJsxElementAttributesType = errorType;
+ if (strictNullChecks) {
+ if (optionalChainContainsReference(expr, reference)) {
+ type = narrowTypeBySwitchOptionalChainContainment(type, flow.switchStatement, flow.clauseStart, flow.clauseEnd, function (t) { return !(t.flags & (32768 /* Undefined */ | 131072 /* Never */)); });
+ }
+ else if (expr.kind === 211 /* TypeOfExpression */ && optionalChainContainsReference(expr.expression, reference)) {
+ type = narrowTypeBySwitchOptionalChainContainment(type, flow.switchStatement, flow.clauseStart, flow.clauseEnd, function (t) { return !(t.flags & 131072 /* Never */ || t.flags & 128 /* StringLiteral */ && t.value === "undefined"); });
+ }
+ }
+ if (isMatchingReferenceDiscriminant(expr, type)) {
+ type = narrowTypeByDiscriminant(type, expr, function (t) { return narrowTypeBySwitchOnDiscriminant(t, flow.switchStatement, flow.clauseStart, flow.clauseEnd); });
+ }
}
+ return createFlowType(type, isIncomplete(flowType));
}
- return links.resolvedJsxElementAttributesType;
- }
- function getJsxElementClassTypeAt(location) {
- var type = getJsxType(JsxNames.ElementClass, location);
- if (type === errorType)
- return undefined;
- return type;
- }
- function getJsxElementTypeAt(location) {
- return getJsxType(JsxNames.Element, location);
- }
- function getJsxStatelessElementTypeAt(location) {
- var jsxElementType = getJsxElementTypeAt(location);
- if (jsxElementType) {
- return getUnionType([jsxElementType, nullType]);
- }
- }
- /**
- * Returns all the properties of the Jsx.IntrinsicElements interface
- */
- function getJsxIntrinsicTagNamesAt(location) {
- var intrinsics = getJsxType(JsxNames.IntrinsicElements, location);
- return intrinsics ? getPropertiesOfType(intrinsics) : ts.emptyArray;
- }
- function checkJsxPreconditions(errorNode) {
- // Preconditions for using JSX
- if ((compilerOptions.jsx || 0 /* None */) === 0 /* None */) {
- error(errorNode, ts.Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided);
+ function getTypeAtFlowBranchLabel(flow) {
+ var antecedentTypes = [];
+ var subtypeReduction = false;
+ var seenIncomplete = false;
+ var bypassFlow;
+ for (var _i = 0, _a = flow.antecedents; _i < _a.length; _i++) {
+ var antecedent = _a[_i];
+ if (!bypassFlow && antecedent.flags & 128 /* SwitchClause */ && antecedent.clauseStart === antecedent.clauseEnd) {
+ // The antecedent is the bypass branch of a potentially exhaustive switch statement.
+ bypassFlow = antecedent;
+ continue;
+ }
+ var flowType = getTypeAtFlowNode(antecedent);
+ var type = getTypeFromFlowType(flowType);
+ // If the type at a particular antecedent path is the declared type and the
+ // reference is known to always be assigned (i.e. when declared and initial types
+ // are the same), there is no reason to process more antecedents since the only
+ // possible outcome is subtypes that will be removed in the final union type anyway.
+ if (type === declaredType && declaredType === initialType) {
+ return type;
+ }
+ ts.pushIfUnique(antecedentTypes, type);
+ // If an antecedent type is not a subset of the declared type, we need to perform
+ // subtype reduction. This happens when a "foreign" type is injected into the control
+ // flow using the instanceof operator or a user defined type predicate.
+ if (!isTypeSubsetOf(type, declaredType)) {
+ subtypeReduction = true;
+ }
+ if (isIncomplete(flowType)) {
+ seenIncomplete = true;
+ }
+ }
+ if (bypassFlow) {
+ var flowType = getTypeAtFlowNode(bypassFlow);
+ var type = getTypeFromFlowType(flowType);
+ // If the bypass flow contributes a type we haven't seen yet and the switch statement
+ // isn't exhaustive, process the bypass flow type. Since exhaustiveness checks increase
+ // the risk of circularities, we only want to perform them when they make a difference.
+ if (!ts.contains(antecedentTypes, type) && !isExhaustiveSwitchStatement(bypassFlow.switchStatement)) {
+ if (type === declaredType && declaredType === initialType) {
+ return type;
+ }
+ antecedentTypes.push(type);
+ if (!isTypeSubsetOf(type, declaredType)) {
+ subtypeReduction = true;
+ }
+ if (isIncomplete(flowType)) {
+ seenIncomplete = true;
+ }
+ }
+ }
+ return createFlowType(getUnionOrEvolvingArrayType(antecedentTypes, subtypeReduction ? 2 /* Subtype */ : 1 /* Literal */), seenIncomplete);
}
- if (getJsxElementTypeAt(errorNode) === undefined) {
- if (noImplicitAny) {
- error(errorNode, ts.Diagnostics.JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist);
+ function getTypeAtFlowLoopLabel(flow) {
+ // If we have previously computed the control flow type for the reference at
+ // this flow loop junction, return the cached type.
+ var id = getFlowNodeId(flow);
+ var cache = flowLoopCaches[id] || (flowLoopCaches[id] = new ts.Map());
+ var key = getOrSetCacheKey();
+ if (!key) {
+ // No cache key is generated when binding patterns are in unnarrowable situations
+ return declaredType;
+ }
+ var cached = cache.get(key);
+ if (cached) {
+ return cached;
+ }
+ // If this flow loop junction and reference are already being processed, return
+ // the union of the types computed for each branch so far, marked as incomplete.
+ // It is possible to see an empty array in cases where loops are nested and the
+ // back edge of the outer loop reaches an inner loop that is already being analyzed.
+ // In such cases we restart the analysis of the inner loop, which will then see
+ // a non-empty in-process array for the outer loop and eventually terminate because
+ // the first antecedent of a loop junction is always the non-looping control flow
+ // path that leads to the top.
+ for (var i = flowLoopStart; i < flowLoopCount; i++) {
+ if (flowLoopNodes[i] === flow && flowLoopKeys[i] === key && flowLoopTypes[i].length) {
+ return createFlowType(getUnionOrEvolvingArrayType(flowLoopTypes[i], 1 /* Literal */), /*incomplete*/ true);
+ }
+ }
+ // Add the flow loop junction and reference to the in-process stack and analyze
+ // each antecedent code path.
+ var antecedentTypes = [];
+ var subtypeReduction = false;
+ var firstAntecedentType;
+ for (var _i = 0, _a = flow.antecedents; _i < _a.length; _i++) {
+ var antecedent = _a[_i];
+ var flowType = void 0;
+ if (!firstAntecedentType) {
+ // The first antecedent of a loop junction is always the non-looping control
+ // flow path that leads to the top.
+ flowType = firstAntecedentType = getTypeAtFlowNode(antecedent);
+ }
+ else {
+ // All but the first antecedent are the looping control flow paths that lead
+ // back to the loop junction. We track these on the flow loop stack.
+ flowLoopNodes[flowLoopCount] = flow;
+ flowLoopKeys[flowLoopCount] = key;
+ flowLoopTypes[flowLoopCount] = antecedentTypes;
+ flowLoopCount++;
+ var saveFlowTypeCache = flowTypeCache;
+ flowTypeCache = undefined;
+ flowType = getTypeAtFlowNode(antecedent);
+ flowTypeCache = saveFlowTypeCache;
+ flowLoopCount--;
+ // If we see a value appear in the cache it is a sign that control flow analysis
+ // was restarted and completed by checkExpressionCached. We can simply pick up
+ // the resulting type and bail out.
+ var cached_1 = cache.get(key);
+ if (cached_1) {
+ return cached_1;
+ }
+ }
+ var type = getTypeFromFlowType(flowType);
+ ts.pushIfUnique(antecedentTypes, type);
+ // If an antecedent type is not a subset of the declared type, we need to perform
+ // subtype reduction. This happens when a "foreign" type is injected into the control
+ // flow using the instanceof operator or a user defined type predicate.
+ if (!isTypeSubsetOf(type, declaredType)) {
+ subtypeReduction = true;
+ }
+ // If the type at a particular antecedent path is the declared type there is no
+ // reason to process more antecedents since the only possible outcome is subtypes
+ // that will be removed in the final union type anyway.
+ if (type === declaredType) {
+ break;
+ }
+ }
+ // The result is incomplete if the first antecedent (the non-looping control flow path)
+ // is incomplete.
+ var result = getUnionOrEvolvingArrayType(antecedentTypes, subtypeReduction ? 2 /* Subtype */ : 1 /* Literal */);
+ if (isIncomplete(firstAntecedentType)) {
+ return createFlowType(result, /*incomplete*/ true);
}
+ cache.set(key, result);
+ return result;
}
- }
- function checkJsxOpeningLikeElementOrOpeningFragment(node) {
- var isNodeOpeningLikeElement = ts.isJsxOpeningLikeElement(node);
- if (isNodeOpeningLikeElement) {
- checkGrammarJsxElement(node);
+ function isMatchingReferenceDiscriminant(expr, computedType) {
+ var type = declaredType.flags & 1048576 /* Union */ ? declaredType : computedType;
+ if (!(type.flags & 1048576 /* Union */) || !ts.isAccessExpression(expr)) {
+ return false;
+ }
+ var name = getAccessedPropertyName(expr);
+ if (name === undefined) {
+ return false;
+ }
+ return isMatchingReference(reference, expr.expression) && isDiscriminantProperty(type, name);
}
- checkJsxPreconditions(node);
- // The reactNamespace/jsxFactory's root symbol should be marked as 'used' so we don't incorrectly elide its import.
- // And if there is no reactNamespace/jsxFactory's symbol in scope when targeting React emit, we should issue an error.
- var reactRefErr = diagnostics && compilerOptions.jsx === 2 /* React */ ? ts.Diagnostics.Cannot_find_name_0 : undefined;
- var reactNamespace = getJsxNamespace(node);
- var reactLocation = isNodeOpeningLikeElement ? node.tagName : node;
- var reactSym = resolveName(reactLocation, reactNamespace, 111551 /* Value */, reactRefErr, reactNamespace, /*isUse*/ true);
- if (reactSym) {
- // Mark local symbol as referenced here because it might not have been marked
- // if jsx emit was not react as there wont be error being emitted
- reactSym.isReferenced = 67108863 /* All */;
- // If react symbol is alias, mark it as refereced
- if (reactSym.flags & 2097152 /* Alias */ && !getTypeOnlyAliasDeclaration(reactSym)) {
- markAliasSymbolAsReferenced(reactSym);
+ function narrowTypeByDiscriminant(type, access, narrowType) {
+ var propName = getAccessedPropertyName(access);
+ if (propName === undefined) {
+ return type;
+ }
+ var propType = getTypeOfPropertyOfType(type, propName);
+ if (!propType) {
+ return type;
}
+ var narrowedPropType = narrowType(propType);
+ return filterType(type, function (t) {
+ var discriminantType = getTypeOfPropertyOrIndexSignature(t, propName);
+ return !(discriminantType.flags & 131072 /* Never */) && isTypeComparableTo(discriminantType, narrowedPropType);
+ });
}
- if (isNodeOpeningLikeElement) {
- var jsxOpeningLikeNode = node;
- var sig = getResolvedSignature(jsxOpeningLikeNode);
- checkJsxReturnAssignableToAppropriateBound(getJsxReferenceKind(jsxOpeningLikeNode), getReturnTypeOfSignature(sig), jsxOpeningLikeNode);
+ function narrowTypeByTruthiness(type, expr, assumeTrue) {
+ if (isMatchingReference(reference, expr)) {
+ return getTypeWithFacts(type, assumeTrue ? 4194304 /* Truthy */ : 8388608 /* Falsy */);
+ }
+ if (strictNullChecks && assumeTrue && optionalChainContainsReference(expr, reference)) {
+ type = getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */);
+ }
+ if (isMatchingReferenceDiscriminant(expr, type)) {
+ return narrowTypeByDiscriminant(type, expr, function (t) { return getTypeWithFacts(t, assumeTrue ? 4194304 /* Truthy */ : 8388608 /* Falsy */); });
+ }
+ return type;
}
- }
- /**
- * Check if a property with the given name is known anywhere in the given type. In an object type, a property
- * is considered known if
- * 1. the object type is empty and the check is for assignability, or
- * 2. if the object type has index signatures, or
- * 3. if the property is actually declared in the object type
- * (this means that 'toString', for example, is not usually a known property).
- * 4. In a union or intersection type,
- * a property is considered known if it is known in any constituent type.
- * @param targetType a type to search a given name in
- * @param name a property name to search
- * @param isComparingJsxAttributes a boolean flag indicating whether we are searching in JsxAttributesType
- */
- function isKnownProperty(targetType, name, isComparingJsxAttributes) {
- if (targetType.flags & 524288 /* Object */) {
- var resolved = resolveStructuredTypeMembers(targetType);
- if (resolved.stringIndexInfo ||
- resolved.numberIndexInfo && isNumericLiteralName(name) ||
- getPropertyOfObjectType(targetType, name) ||
- isComparingJsxAttributes && !isUnhyphenatedJsxName(name)) {
- // For JSXAttributes, if the attribute has a hyphenated name, consider that the attribute to be known.
+ function isTypePresencePossible(type, propName, assumeTrue) {
+ if (getIndexInfoOfType(type, 0 /* String */)) {
return true;
}
- }
- else if (targetType.flags & 3145728 /* UnionOrIntersection */ && isExcessPropertyCheckTarget(targetType)) {
- for (var _i = 0, _a = targetType.types; _i < _a.length; _i++) {
- var t = _a[_i];
- if (isKnownProperty(t, name, isComparingJsxAttributes)) {
- return true;
- }
+ var prop = getPropertyOfType(type, propName);
+ if (prop) {
+ return prop.flags & 16777216 /* Optional */ ? true : assumeTrue;
}
+ return !assumeTrue;
}
- return false;
- }
- function isExcessPropertyCheckTarget(type) {
- return !!(type.flags & 524288 /* Object */ && !(ts.getObjectFlags(type) & 512 /* ObjectLiteralPatternWithComputedProperties */) ||
- type.flags & 67108864 /* NonPrimitive */ ||
- type.flags & 1048576 /* Union */ && ts.some(type.types, isExcessPropertyCheckTarget) ||
- type.flags & 2097152 /* Intersection */ && ts.every(type.types, isExcessPropertyCheckTarget));
- }
- function checkJsxExpression(node, checkMode) {
- checkGrammarJsxExpression(node);
- if (node.expression) {
- var type = checkExpression(node.expression, checkMode);
- if (node.dotDotDotToken && type !== anyType && !isArrayType(type)) {
- error(node, ts.Diagnostics.JSX_spread_child_must_be_an_array_type);
+ function narrowByInKeyword(type, literal, assumeTrue) {
+ if (type.flags & (1048576 /* Union */ | 524288 /* Object */)
+ || isThisTypeParameter(type)
+ || type.flags & 2097152 /* Intersection */ && ts.every(type.types, function (t) { return t.symbol !== globalThisSymbol; })) {
+ var propName_1 = ts.escapeLeadingUnderscores(literal.text);
+ return filterType(type, function (t) { return isTypePresencePossible(t, propName_1, assumeTrue); });
}
return type;
}
- else {
- return errorType;
- }
- }
- function getDeclarationNodeFlagsFromSymbol(s) {
- return s.valueDeclaration ? ts.getCombinedNodeFlags(s.valueDeclaration) : 0;
- }
- /**
- * Return whether this symbol is a member of a prototype somewhere
- * Note that this is not tracked well within the compiler, so the answer may be incorrect.
- */
- function isPrototypeProperty(symbol) {
- if (symbol.flags & 8192 /* Method */ || ts.getCheckFlags(symbol) & 4 /* SyntheticMethod */) {
- return true;
+ function narrowTypeByBinaryExpression(type, expr, assumeTrue) {
+ switch (expr.operatorToken.kind) {
+ case 62 /* EqualsToken */:
+ case 74 /* BarBarEqualsToken */:
+ case 75 /* AmpersandAmpersandEqualsToken */:
+ case 76 /* QuestionQuestionEqualsToken */:
+ return narrowTypeByTruthiness(narrowType(type, expr.right, assumeTrue), expr.left, assumeTrue);
+ case 34 /* EqualsEqualsToken */:
+ case 35 /* ExclamationEqualsToken */:
+ case 36 /* EqualsEqualsEqualsToken */:
+ case 37 /* ExclamationEqualsEqualsToken */:
+ var operator_1 = expr.operatorToken.kind;
+ var left_1 = getReferenceCandidate(expr.left);
+ var right_1 = getReferenceCandidate(expr.right);
+ if (left_1.kind === 211 /* TypeOfExpression */ && ts.isStringLiteralLike(right_1)) {
+ return narrowTypeByTypeof(type, left_1, operator_1, right_1, assumeTrue);
+ }
+ if (right_1.kind === 211 /* TypeOfExpression */ && ts.isStringLiteralLike(left_1)) {
+ return narrowTypeByTypeof(type, right_1, operator_1, left_1, assumeTrue);
+ }
+ if (isMatchingReference(reference, left_1)) {
+ return narrowTypeByEquality(type, operator_1, right_1, assumeTrue);
+ }
+ if (isMatchingReference(reference, right_1)) {
+ return narrowTypeByEquality(type, operator_1, left_1, assumeTrue);
+ }
+ if (strictNullChecks) {
+ if (optionalChainContainsReference(left_1, reference)) {
+ type = narrowTypeByOptionalChainContainment(type, operator_1, right_1, assumeTrue);
+ }
+ else if (optionalChainContainsReference(right_1, reference)) {
+ type = narrowTypeByOptionalChainContainment(type, operator_1, left_1, assumeTrue);
+ }
+ }
+ if (isMatchingReferenceDiscriminant(left_1, type)) {
+ return narrowTypeByDiscriminant(type, left_1, function (t) { return narrowTypeByEquality(t, operator_1, right_1, assumeTrue); });
+ }
+ if (isMatchingReferenceDiscriminant(right_1, type)) {
+ return narrowTypeByDiscriminant(type, right_1, function (t) { return narrowTypeByEquality(t, operator_1, left_1, assumeTrue); });
+ }
+ if (isMatchingConstructorReference(left_1)) {
+ return narrowTypeByConstructor(type, operator_1, right_1, assumeTrue);
+ }
+ if (isMatchingConstructorReference(right_1)) {
+ return narrowTypeByConstructor(type, operator_1, left_1, assumeTrue);
+ }
+ break;
+ case 101 /* InstanceOfKeyword */:
+ return narrowTypeByInstanceof(type, expr, assumeTrue);
+ case 100 /* InKeyword */:
+ var target = getReferenceCandidate(expr.right);
+ if (ts.isStringLiteralLike(expr.left) && isMatchingReference(reference, target)) {
+ return narrowByInKeyword(type, expr.left, assumeTrue);
+ }
+ break;
+ case 27 /* CommaToken */:
+ return narrowType(type, expr.right, assumeTrue);
+ }
+ return type;
}
- if (ts.isInJSFile(symbol.valueDeclaration)) {
- var parent = symbol.valueDeclaration.parent;
- return parent && ts.isBinaryExpression(parent) &&
- ts.getAssignmentDeclarationKind(parent) === 3 /* PrototypeProperty */;
+ function narrowTypeByOptionalChainContainment(type, operator, value, assumeTrue) {
+ // We are in a branch of obj?.foo === value (or any one of the other equality operators). We narrow obj as follows:
+ // When operator is === and type of value excludes undefined, null and undefined is removed from type of obj in true branch.
+ // When operator is !== and type of value excludes undefined, null and undefined is removed from type of obj in false branch.
+ // When operator is == and type of value excludes null and undefined, null and undefined is removed from type of obj in true branch.
+ // When operator is != and type of value excludes null and undefined, null and undefined is removed from type of obj in false branch.
+ // When operator is === and type of value is undefined, null and undefined is removed from type of obj in false branch.
+ // When operator is !== and type of value is undefined, null and undefined is removed from type of obj in true branch.
+ // When operator is == and type of value is null or undefined, null and undefined is removed from type of obj in false branch.
+ // When operator is != and type of value is null or undefined, null and undefined is removed from type of obj in true branch.
+ var equalsOperator = operator === 34 /* EqualsEqualsToken */ || operator === 36 /* EqualsEqualsEqualsToken */;
+ var nullableFlags = operator === 34 /* EqualsEqualsToken */ || operator === 35 /* ExclamationEqualsToken */ ? 98304 /* Nullable */ : 32768 /* Undefined */;
+ var valueType = getTypeOfExpression(value);
+ // Note that we include any and unknown in the exclusion test because their domain includes null and undefined.
+ var removeNullable = equalsOperator !== assumeTrue && everyType(valueType, function (t) { return !!(t.flags & nullableFlags); }) ||
+ equalsOperator === assumeTrue && everyType(valueType, function (t) { return !(t.flags & (3 /* AnyOrUnknown */ | nullableFlags)); });
+ return removeNullable ? getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */) : type;
}
- }
- /**
- * Check whether the requested property access is valid.
- * Returns true if node is a valid property access, and false otherwise.
- * @param node The node to be checked.
- * @param isSuper True if the access is from `super.`.
- * @param type The type of the object whose property is being accessed. (Not the type of the property.)
- * @param prop The symbol for the property being accessed.
- */
- function checkPropertyAccessibility(node, isSuper, type, prop) {
- var flags = ts.getDeclarationModifierFlagsFromSymbol(prop);
- var errorNode = node.kind === 153 /* QualifiedName */ ? node.right : node.kind === 188 /* ImportType */ ? node : node.name;
- if (isSuper) {
- // TS 1.0 spec (April 2014): 4.8.2
- // - In a constructor, instance member function, instance member accessor, or
- // instance member variable initializer where this references a derived class instance,
- // a super property access is permitted and must specify a public instance member function of the base class.
- // - In a static member function or static member accessor
- // where this references the constructor function object of a derived class,
- // a super property access is permitted and must specify a public static member function of the base class.
- if (languageVersion < 2 /* ES2015 */) {
- if (symbolHasNonMethodDeclaration(prop)) {
- error(errorNode, ts.Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword);
- return false;
+ function narrowTypeByEquality(type, operator, value, assumeTrue) {
+ if (type.flags & 1 /* Any */) {
+ return type;
+ }
+ if (operator === 35 /* ExclamationEqualsToken */ || operator === 37 /* ExclamationEqualsEqualsToken */) {
+ assumeTrue = !assumeTrue;
+ }
+ var valueType = getTypeOfExpression(value);
+ if ((type.flags & 2 /* Unknown */) && assumeTrue && (operator === 36 /* EqualsEqualsEqualsToken */ || operator === 37 /* ExclamationEqualsEqualsToken */)) {
+ if (valueType.flags & (131068 /* Primitive */ | 67108864 /* NonPrimitive */)) {
+ return valueType;
}
+ if (valueType.flags & 524288 /* Object */) {
+ return nonPrimitiveType;
+ }
+ return type;
}
- if (flags & 128 /* Abstract */) {
- // A method cannot be accessed in a super property access if the method is abstract.
- // This error could mask a private property access error. But, a member
- // cannot simultaneously be private and abstract, so this will trigger an
- // additional error elsewhere.
- error(errorNode, ts.Diagnostics.Abstract_method_0_in_class_1_cannot_be_accessed_via_super_expression, symbolToString(prop), typeToString(getDeclaringClass(prop)));
- return false;
+ if (valueType.flags & 98304 /* Nullable */) {
+ if (!strictNullChecks) {
+ return type;
+ }
+ var doubleEquals = operator === 34 /* EqualsEqualsToken */ || operator === 35 /* ExclamationEqualsToken */;
+ var facts = doubleEquals ?
+ assumeTrue ? 262144 /* EQUndefinedOrNull */ : 2097152 /* NEUndefinedOrNull */ :
+ valueType.flags & 65536 /* Null */ ?
+ assumeTrue ? 131072 /* EQNull */ : 1048576 /* NENull */ :
+ assumeTrue ? 65536 /* EQUndefined */ : 524288 /* NEUndefined */;
+ return getTypeWithFacts(type, facts);
}
- }
- // Referencing abstract properties within their own constructors is not allowed
- if ((flags & 128 /* Abstract */) && ts.isThisProperty(node) && symbolHasNonMethodDeclaration(prop)) {
- var declaringClassDeclaration = ts.getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop));
- if (declaringClassDeclaration && isNodeUsedDuringClassInitialization(node)) {
- error(errorNode, ts.Diagnostics.Abstract_property_0_in_class_1_cannot_be_accessed_in_the_constructor, symbolToString(prop), ts.getTextOfIdentifierOrLiteral(declaringClassDeclaration.name)); // TODO: GH#18217
- return false;
+ if (assumeTrue) {
+ var filterFn = operator === 34 /* EqualsEqualsToken */ ?
+ (function (t) { return areTypesComparable(t, valueType) || isCoercibleUnderDoubleEquals(t, valueType); }) :
+ function (t) { return areTypesComparable(t, valueType); };
+ return replacePrimitivesWithLiterals(filterType(type, filterFn), valueType);
}
- }
- if (ts.isPropertyAccessExpression(node) && ts.isPrivateIdentifier(node.name)) {
- if (!ts.getContainingClass(node)) {
- error(errorNode, ts.Diagnostics.Private_identifiers_are_not_allowed_outside_class_bodies);
- return false;
+ if (isUnitType(valueType)) {
+ var regularType_1 = getRegularTypeOfLiteralType(valueType);
+ return filterType(type, function (t) { return isUnitType(t) ? !areTypesComparable(t, valueType) : getRegularTypeOfLiteralType(t) !== regularType_1; });
}
- return true;
- }
- // Public properties are otherwise accessible.
- if (!(flags & 24 /* NonPublicAccessibilityModifier */)) {
- return true;
+ return type;
}
- // Property is known to be private or protected at this point
- // Private property is accessible if the property is within the declaring class
- if (flags & 8 /* Private */) {
- var declaringClassDeclaration = ts.getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop));
- if (!isNodeWithinClass(node, declaringClassDeclaration)) {
- error(errorNode, ts.Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(getDeclaringClass(prop)));
- return false;
+ function narrowTypeByTypeof(type, typeOfExpr, operator, literal, assumeTrue) {
+ // We have '==', '!=', '===', or !==' operator with 'typeof xxx' and string literal operands
+ if (operator === 35 /* ExclamationEqualsToken */ || operator === 37 /* ExclamationEqualsEqualsToken */) {
+ assumeTrue = !assumeTrue;
}
- return true;
- }
- // Property is known to be protected at this point
- // All protected properties of a supertype are accessible in a super access
- if (isSuper) {
- return true;
- }
- // Find the first enclosing class that has the declaring classes of the protected constituents
- // of the property as base classes
- var enclosingClass = forEachEnclosingClass(node, function (enclosingDeclaration) {
- var enclosingClass = getDeclaredTypeOfSymbol(getSymbolOfNode(enclosingDeclaration));
- return isClassDerivedFromDeclaringClasses(enclosingClass, prop) ? enclosingClass : undefined;
- });
- // A protected property is accessible if the property is within the declaring class or classes derived from it
- if (!enclosingClass) {
- // allow PropertyAccessibility if context is in function with this parameter
- // static member access is disallow
- var thisParameter = void 0;
- if (flags & 32 /* Static */ || !(thisParameter = getThisParameterFromNodeContext(node)) || !thisParameter.type) {
- error(errorNode, ts.Diagnostics.Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses, symbolToString(prop), typeToString(getDeclaringClass(prop) || type));
- return false;
+ var target = getReferenceCandidate(typeOfExpr.expression);
+ if (!isMatchingReference(reference, target)) {
+ if (strictNullChecks && optionalChainContainsReference(target, reference) && assumeTrue === (literal.text !== "undefined")) {
+ return getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */);
+ }
+ return type;
}
- var thisType = getTypeFromTypeNode(thisParameter.type);
- enclosingClass = ((thisType.flags & 262144 /* TypeParameter */) ? getConstraintOfTypeParameter(thisType) : thisType).target;
- }
- // No further restrictions for static properties
- if (flags & 32 /* Static */) {
- return true;
- }
- if (type.flags & 262144 /* TypeParameter */) {
- // get the original type -- represented as the type constraint of the 'this' type
- type = type.isThisType ? getConstraintOfTypeParameter(type) : getBaseConstraintOfType(type); // TODO: GH#18217 Use a different variable that's allowed to be undefined
- }
- if (!type || !hasBaseType(type, enclosingClass)) {
- error(errorNode, ts.Diagnostics.Property_0_is_protected_and_only_accessible_through_an_instance_of_class_1, symbolToString(prop), typeToString(enclosingClass));
- return false;
+ if (type.flags & 1 /* Any */ && literal.text === "function") {
+ return type;
+ }
+ if (assumeTrue && type.flags & 2 /* Unknown */ && literal.text === "object") {
+ // The pattern x && typeof x === 'object', where x is of type unknown, narrows x to type object. We don't
+ // need to check for the reverse typeof x === 'object' && x since that already narrows correctly.
+ if (typeOfExpr.parent.parent.kind === 216 /* BinaryExpression */) {
+ var expr = typeOfExpr.parent.parent;
+ if (expr.operatorToken.kind === 55 /* AmpersandAmpersandToken */ && expr.right === typeOfExpr.parent && containsTruthyCheck(reference, expr.left)) {
+ return nonPrimitiveType;
+ }
+ }
+ return getUnionType([nonPrimitiveType, nullType]);
+ }
+ var facts = assumeTrue ?
+ typeofEQFacts.get(literal.text) || 128 /* TypeofEQHostObject */ :
+ typeofNEFacts.get(literal.text) || 32768 /* TypeofNEHostObject */;
+ var impliedType = getImpliedTypeFromTypeofGuard(type, literal.text);
+ return getTypeWithFacts(assumeTrue && impliedType ? mapType(type, narrowUnionMemberByTypeof(impliedType)) : type, facts);
}
- return true;
- }
- function getThisParameterFromNodeContext(node) {
- var thisContainer = ts.getThisContainer(node, /* includeArrowFunctions */ false);
- return thisContainer && ts.isFunctionLike(thisContainer) ? ts.getThisParameter(thisContainer) : undefined;
- }
- function symbolHasNonMethodDeclaration(symbol) {
- return !!forEachProperty(symbol, function (prop) { return !(prop.flags & 8192 /* Method */); });
- }
- function checkNonNullExpression(node) {
- return checkNonNullType(checkExpression(node), node);
- }
- function isNullableType(type) {
- return !!((strictNullChecks ? getFalsyFlags(type) : type.flags) & 98304 /* Nullable */);
- }
- function getNonNullableTypeIfNeeded(type) {
- return isNullableType(type) ? getNonNullableType(type) : type;
- }
- function reportObjectPossiblyNullOrUndefinedError(node, flags) {
- error(node, flags & 32768 /* Undefined */ ? flags & 65536 /* Null */ ?
- ts.Diagnostics.Object_is_possibly_null_or_undefined :
- ts.Diagnostics.Object_is_possibly_undefined :
- ts.Diagnostics.Object_is_possibly_null);
- }
- function reportCannotInvokePossiblyNullOrUndefinedError(node, flags) {
- error(node, flags & 32768 /* Undefined */ ? flags & 65536 /* Null */ ?
- ts.Diagnostics.Cannot_invoke_an_object_which_is_possibly_null_or_undefined :
- ts.Diagnostics.Cannot_invoke_an_object_which_is_possibly_undefined :
- ts.Diagnostics.Cannot_invoke_an_object_which_is_possibly_null);
- }
- function checkNonNullTypeWithReporter(type, node, reportError) {
- if (strictNullChecks && type.flags & 2 /* Unknown */) {
- error(node, ts.Diagnostics.Object_is_of_type_unknown);
- return errorType;
+ function narrowTypeBySwitchOptionalChainContainment(type, switchStatement, clauseStart, clauseEnd, clauseCheck) {
+ var everyClauseChecks = clauseStart !== clauseEnd && ts.every(getSwitchClauseTypes(switchStatement).slice(clauseStart, clauseEnd), clauseCheck);
+ return everyClauseChecks ? getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */) : type;
}
- var kind = (strictNullChecks ? getFalsyFlags(type) : type.flags) & 98304 /* Nullable */;
- if (kind) {
- reportError(node, kind);
- var t = getNonNullableType(type);
- return t.flags & (98304 /* Nullable */ | 131072 /* Never */) ? errorType : t;
+ function narrowTypeBySwitchOnDiscriminant(type, switchStatement, clauseStart, clauseEnd) {
+ // We only narrow if all case expressions specify
+ // values with unit types, except for the case where
+ // `type` is unknown. In this instance we map object
+ // types to the nonPrimitive type and narrow with that.
+ var switchTypes = getSwitchClauseTypes(switchStatement);
+ if (!switchTypes.length) {
+ return type;
+ }
+ var clauseTypes = switchTypes.slice(clauseStart, clauseEnd);
+ var hasDefaultClause = clauseStart === clauseEnd || ts.contains(clauseTypes, neverType);
+ if ((type.flags & 2 /* Unknown */) && !hasDefaultClause) {
+ var groundClauseTypes = void 0;
+ for (var i = 0; i < clauseTypes.length; i += 1) {
+ var t = clauseTypes[i];
+ if (t.flags & (131068 /* Primitive */ | 67108864 /* NonPrimitive */)) {
+ if (groundClauseTypes !== undefined) {
+ groundClauseTypes.push(t);
+ }
+ }
+ else if (t.flags & 524288 /* Object */) {
+ if (groundClauseTypes === undefined) {
+ groundClauseTypes = clauseTypes.slice(0, i);
+ }
+ groundClauseTypes.push(nonPrimitiveType);
+ }
+ else {
+ return type;
+ }
+ }
+ return getUnionType(groundClauseTypes === undefined ? clauseTypes : groundClauseTypes);
+ }
+ var discriminantType = getUnionType(clauseTypes);
+ var caseType = discriminantType.flags & 131072 /* Never */ ? neverType :
+ replacePrimitivesWithLiterals(filterType(type, function (t) { return areTypesComparable(discriminantType, t); }), discriminantType);
+ if (!hasDefaultClause) {
+ return caseType;
+ }
+ var defaultType = filterType(type, function (t) { return !(isUnitType(t) && ts.contains(switchTypes, getRegularTypeOfLiteralType(t))); });
+ return caseType.flags & 131072 /* Never */ ? defaultType : getUnionType([caseType, defaultType]);
}
- return type;
- }
- function checkNonNullType(type, node) {
- return checkNonNullTypeWithReporter(type, node, reportObjectPossiblyNullOrUndefinedError);
- }
- function checkNonNullNonVoidType(type, node) {
- var nonNullType = checkNonNullType(type, node);
- if (nonNullType !== errorType && nonNullType.flags & 16384 /* Void */) {
- error(node, ts.Diagnostics.Object_is_possibly_undefined);
+ function getImpliedTypeFromTypeofGuard(type, text) {
+ switch (text) {
+ case "function":
+ return type.flags & 1 /* Any */ ? type : globalFunctionType;
+ case "object":
+ return type.flags & 2 /* Unknown */ ? getUnionType([nonPrimitiveType, nullType]) : type;
+ default:
+ return typeofTypesByName.get(text);
+ }
}
- return nonNullType;
- }
- function checkPropertyAccessExpression(node) {
- return node.flags & 32 /* OptionalChain */ ? checkPropertyAccessChain(node) :
- checkPropertyAccessExpressionOrQualifiedName(node, node.expression, checkNonNullExpression(node.expression), node.name);
- }
- function checkPropertyAccessChain(node) {
- var leftType = checkExpression(node.expression);
- var nonOptionalType = getOptionalExpressionType(leftType, node.expression);
- return propagateOptionalTypeMarker(checkPropertyAccessExpressionOrQualifiedName(node, node.expression, checkNonNullType(nonOptionalType, node.expression), node.name), node, nonOptionalType !== leftType);
- }
- function checkQualifiedName(node) {
- return checkPropertyAccessExpressionOrQualifiedName(node, node.left, checkNonNullExpression(node.left), node.right);
- }
- function isMethodAccessForCall(node) {
- while (node.parent.kind === 200 /* ParenthesizedExpression */) {
- node = node.parent;
+ // When narrowing a union type by a `typeof` guard using type-facts alone, constituent types that are
+ // super-types of the implied guard will be retained in the final type: this is because type-facts only
+ // filter. Instead, we would like to replace those union constituents with the more precise type implied by
+ // the guard. For example: narrowing `{} | undefined` by `"boolean"` should produce the type `boolean`, not
+ // the filtered type `{}`. For this reason we narrow constituents of the union individually, in addition to
+ // filtering by type-facts.
+ function narrowUnionMemberByTypeof(candidate) {
+ return function (type) {
+ if (isTypeSubtypeOf(type, candidate)) {
+ return type;
+ }
+ if (isTypeSubtypeOf(candidate, type)) {
+ return candidate;
+ }
+ if (type.flags & 465829888 /* Instantiable */) {
+ var constraint = getBaseConstraintOfType(type) || anyType;
+ if (isTypeSubtypeOf(candidate, constraint)) {
+ return getIntersectionType([type, candidate]);
+ }
+ }
+ return type;
+ };
}
- return ts.isCallOrNewExpression(node.parent) && node.parent.expression === node;
- }
- // Lookup the private identifier lexically.
- function lookupSymbolForPrivateIdentifierDeclaration(propName, location) {
- for (var containingClass = ts.getContainingClass(location); !!containingClass; containingClass = ts.getContainingClass(containingClass)) {
- var symbol = containingClass.symbol;
- var name = ts.getSymbolNameForPrivateIdentifier(symbol, propName);
- var prop = (symbol.members && symbol.members.get(name)) || (symbol.exports && symbol.exports.get(name));
- if (prop) {
- return prop;
+ function narrowBySwitchOnTypeOf(type, switchStatement, clauseStart, clauseEnd) {
+ var switchWitnesses = getSwitchClauseTypeOfWitnesses(switchStatement, /*retainDefault*/ true);
+ if (!switchWitnesses.length) {
+ return type;
}
+ // Equal start and end denotes implicit fallthrough; undefined marks explicit default clause
+ var defaultCaseLocation = ts.findIndex(switchWitnesses, function (elem) { return elem === undefined; });
+ var hasDefaultClause = clauseStart === clauseEnd || (defaultCaseLocation >= clauseStart && defaultCaseLocation < clauseEnd);
+ var clauseWitnesses;
+ var switchFacts;
+ if (defaultCaseLocation > -1) {
+ // We no longer need the undefined denoting an explicit default case. Remove the undefined and
+ // fix-up clauseStart and clauseEnd. This means that we don't have to worry about undefined in the
+ // witness array.
+ var witnesses = switchWitnesses.filter(function (witness) { return witness !== undefined; });
+ // The adjusted clause start and end after removing the `default` statement.
+ var fixedClauseStart = defaultCaseLocation < clauseStart ? clauseStart - 1 : clauseStart;
+ var fixedClauseEnd = defaultCaseLocation < clauseEnd ? clauseEnd - 1 : clauseEnd;
+ clauseWitnesses = witnesses.slice(fixedClauseStart, fixedClauseEnd);
+ switchFacts = getFactsFromTypeofSwitch(fixedClauseStart, fixedClauseEnd, witnesses, hasDefaultClause);
+ }
+ else {
+ clauseWitnesses = switchWitnesses.slice(clauseStart, clauseEnd);
+ switchFacts = getFactsFromTypeofSwitch(clauseStart, clauseEnd, switchWitnesses, hasDefaultClause);
+ }
+ if (hasDefaultClause) {
+ return filterType(type, function (t) { return (getTypeFacts(t) & switchFacts) === switchFacts; });
+ }
+ /*
+ The implied type is the raw type suggested by a
+ value being caught in this clause.
+
+ When the clause contains a default case we ignore
+ the implied type and try to narrow using any facts
+ we can learn: see `switchFacts`.
+
+ Example:
+ switch (typeof x) {
+ case 'number':
+ case 'string': break;
+ default: break;
+ case 'number':
+ case 'boolean': break
+ }
+
+ In the first clause (case `number` and `string`) the
+ implied type is number | string.
+
+ In the default clause we de not compute an implied type.
+
+ In the third clause (case `number` and `boolean`)
+ the naive implied type is number | boolean, however
+ we use the type facts to narrow the implied type to
+ boolean. We know that number cannot be selected
+ because it is caught in the first clause.
+ */
+ var impliedType = getTypeWithFacts(getUnionType(clauseWitnesses.map(function (text) { return getImpliedTypeFromTypeofGuard(type, text) || type; })), switchFacts);
+ return getTypeWithFacts(mapType(type, narrowUnionMemberByTypeof(impliedType)), switchFacts);
}
- }
- function getPrivateIdentifierPropertyOfType(leftType, lexicallyScopedIdentifier) {
- return getPropertyOfType(leftType, lexicallyScopedIdentifier.escapedName);
- }
- function checkPrivateIdentifierPropertyAccess(leftType, right, lexicallyScopedIdentifier) {
- // Either the identifier could not be looked up in the lexical scope OR the lexically scoped identifier did not exist on the type.
- // Find a private identifier with the same description on the type.
- var propertyOnType;
- var properties = getPropertiesOfType(leftType);
- if (properties) {
- ts.forEach(properties, function (symbol) {
- var decl = symbol.valueDeclaration;
- if (decl && ts.isNamedDeclaration(decl) && ts.isPrivateIdentifier(decl.name) && decl.name.escapedText === right.escapedText) {
- propertyOnType = symbol;
- return true;
- }
- });
+ function isMatchingConstructorReference(expr) {
+ return (ts.isPropertyAccessExpression(expr) && ts.idText(expr.name) === "constructor" ||
+ ts.isElementAccessExpression(expr) && ts.isStringLiteralLike(expr.argumentExpression) && expr.argumentExpression.text === "constructor") &&
+ isMatchingReference(reference, expr.expression);
}
- var diagName = diagnosticName(right);
- if (propertyOnType) {
- var typeValueDecl = propertyOnType.valueDeclaration;
- var typeClass_1 = ts.getContainingClass(typeValueDecl);
- ts.Debug.assert(!!typeClass_1);
- // We found a private identifier property with the same description.
- // Either:
- // - There is a lexically scoped private identifier AND it shadows the one we found on the type.
- // - It is an attempt to access the private identifier outside of the class.
- if (lexicallyScopedIdentifier) {
- var lexicalValueDecl = lexicallyScopedIdentifier.valueDeclaration;
- var lexicalClass = ts.getContainingClass(lexicalValueDecl);
- ts.Debug.assert(!!lexicalClass);
- if (ts.findAncestor(lexicalClass, function (n) { return typeClass_1 === n; })) {
- var diagnostic = error(right, ts.Diagnostics.The_property_0_cannot_be_accessed_on_type_1_within_this_class_because_it_is_shadowed_by_another_private_identifier_with_the_same_spelling, diagName, typeToString(leftType));
- ts.addRelatedInfo(diagnostic, ts.createDiagnosticForNode(lexicalValueDecl, ts.Diagnostics.The_shadowing_declaration_of_0_is_defined_here, diagName), ts.createDiagnosticForNode(typeValueDecl, ts.Diagnostics.The_declaration_of_0_that_you_probably_intended_to_use_is_defined_here, diagName));
- return true;
+ function narrowTypeByConstructor(type, operator, identifier, assumeTrue) {
+ // Do not narrow when checking inequality.
+ if (assumeTrue ? (operator !== 34 /* EqualsEqualsToken */ && operator !== 36 /* EqualsEqualsEqualsToken */) : (operator !== 35 /* ExclamationEqualsToken */ && operator !== 37 /* ExclamationEqualsEqualsToken */)) {
+ return type;
+ }
+ // Get the type of the constructor identifier expression, if it is not a function then do not narrow.
+ var identifierType = getTypeOfExpression(identifier);
+ if (!isFunctionType(identifierType) && !isConstructorType(identifierType)) {
+ return type;
+ }
+ // Get the prototype property of the type identifier so we can find out its type.
+ var prototypeProperty = getPropertyOfType(identifierType, "prototype");
+ if (!prototypeProperty) {
+ return type;
+ }
+ // Get the type of the prototype, if it is undefined, or the global `Object` or `Function` types then do not narrow.
+ var prototypeType = getTypeOfSymbol(prototypeProperty);
+ var candidate = !isTypeAny(prototypeType) ? prototypeType : undefined;
+ if (!candidate || candidate === globalObjectType || candidate === globalFunctionType) {
+ return type;
+ }
+ // If the type that is being narrowed is `any` then just return the `candidate` type since every type is a subtype of `any`.
+ if (isTypeAny(type)) {
+ return candidate;
+ }
+ // Filter out types that are not considered to be "constructed by" the `candidate` type.
+ return filterType(type, function (t) { return isConstructedBy(t, candidate); });
+ function isConstructedBy(source, target) {
+ // If either the source or target type are a class type then we need to check that they are the same exact type.
+ // This is because you may have a class `A` that defines some set of properties, and another class `B`
+ // that defines the same set of properties as class `A`, in that case they are structurally the same
+ // type, but when you do something like `instanceOfA.constructor === B` it will return false.
+ if (source.flags & 524288 /* Object */ && ts.getObjectFlags(source) & 1 /* Class */ ||
+ target.flags & 524288 /* Object */ && ts.getObjectFlags(target) & 1 /* Class */) {
+ return source.symbol === target.symbol;
}
+ // For all other types just check that the `source` type is a subtype of the `target` type.
+ return isTypeSubtypeOf(source, target);
}
- error(right, ts.Diagnostics.Property_0_is_not_accessible_outside_class_1_because_it_has_a_private_identifier, diagName, diagnosticName(typeClass_1.name || anon));
- return true;
- }
- return false;
- }
- function checkPropertyAccessExpressionOrQualifiedName(node, left, leftType, right) {
- var parentSymbol = getNodeLinks(left).resolvedSymbol;
- var assignmentKind = ts.getAssignmentTargetKind(node);
- var apparentType = getApparentType(assignmentKind !== 0 /* None */ || isMethodAccessForCall(node) ? getWidenedType(leftType) : leftType);
- if (ts.isPrivateIdentifier(right)) {
- checkExternalEmitHelpers(node, 262144 /* ClassPrivateFieldGet */);
}
- var isAnyLike = isTypeAny(apparentType) || apparentType === silentNeverType;
- var prop;
- if (ts.isPrivateIdentifier(right)) {
- var lexicallyScopedSymbol = lookupSymbolForPrivateIdentifierDeclaration(right.escapedText, right);
- if (isAnyLike) {
- if (lexicallyScopedSymbol) {
- return apparentType;
+ function narrowTypeByInstanceof(type, expr, assumeTrue) {
+ var left = getReferenceCandidate(expr.left);
+ if (!isMatchingReference(reference, left)) {
+ if (assumeTrue && strictNullChecks && optionalChainContainsReference(left, reference)) {
+ return getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */);
}
- if (!ts.getContainingClass(right)) {
- grammarErrorOnNode(right, ts.Diagnostics.Private_identifiers_are_not_allowed_outside_class_bodies);
- return anyType;
+ return type;
+ }
+ // Check that right operand is a function type with a prototype property
+ var rightType = getTypeOfExpression(expr.right);
+ if (!isTypeDerivedFrom(rightType, globalFunctionType)) {
+ return type;
+ }
+ var targetType;
+ var prototypeProperty = getPropertyOfType(rightType, "prototype");
+ if (prototypeProperty) {
+ // Target type is type of the prototype property
+ var prototypePropertyType = getTypeOfSymbol(prototypeProperty);
+ if (!isTypeAny(prototypePropertyType)) {
+ targetType = prototypePropertyType;
}
}
- prop = lexicallyScopedSymbol ? getPrivateIdentifierPropertyOfType(leftType, lexicallyScopedSymbol) : undefined;
- // Check for private-identifier-specific shadowing and lexical-scoping errors.
- if (!prop && checkPrivateIdentifierPropertyAccess(leftType, right, lexicallyScopedSymbol)) {
- return errorType;
+ // Don't narrow from 'any' if the target type is exactly 'Object' or 'Function'
+ if (isTypeAny(type) && (targetType === globalObjectType || targetType === globalFunctionType)) {
+ return type;
+ }
+ if (!targetType) {
+ var constructSignatures = getSignaturesOfType(rightType, 1 /* Construct */);
+ targetType = constructSignatures.length ?
+ getUnionType(ts.map(constructSignatures, function (signature) { return getReturnTypeOfSignature(getErasedSignature(signature)); })) :
+ emptyObjectType;
+ }
+ // We can't narrow a union based off instanceof without negated types see #31576 for more info
+ if (!assumeTrue && rightType.flags & 1048576 /* Union */) {
+ var nonConstructorTypeInUnion = ts.find(rightType.types, function (t) { return !isConstructorType(t); });
+ if (!nonConstructorTypeInUnion)
+ return type;
}
+ return getNarrowedType(type, targetType, assumeTrue, isTypeDerivedFrom);
}
- else {
- if (isAnyLike) {
- if (ts.isIdentifier(left) && parentSymbol) {
- markAliasReferenced(parentSymbol, node);
+ function getNarrowedType(type, candidate, assumeTrue, isRelated) {
+ if (!assumeTrue) {
+ return filterType(type, function (t) { return !isRelated(t, candidate); });
+ }
+ // If the current type is a union type, remove all constituents that couldn't be instances of
+ // the candidate type. If one or more constituents remain, return a union of those.
+ if (type.flags & 1048576 /* Union */) {
+ var assignableType = filterType(type, function (t) { return isRelated(t, candidate); });
+ if (!(assignableType.flags & 131072 /* Never */)) {
+ return assignableType;
}
- return apparentType;
}
- prop = getPropertyOfType(apparentType, right.escapedText);
- }
- if (ts.isIdentifier(left) && parentSymbol && !(prop && isConstEnumOrConstEnumOnlyModule(prop))) {
- markAliasReferenced(parentSymbol, node);
+ // If the candidate type is a subtype of the target type, narrow to the candidate type,
+ // if the target type is a subtype of the candidate type, narrow to the target type,
+ // otherwise, narrow to an intersection of the two types.
+ return isTypeSubtypeOf(candidate, type) ? candidate : isTypeSubtypeOf(type, candidate) ? type : getIntersectionType([type, candidate]);
}
- var propType;
- if (!prop) {
- var indexInfo = !ts.isPrivateIdentifier(right) && (assignmentKind === 0 /* None */ || !isGenericObjectType(leftType) || isThisTypeParameter(leftType)) ? getIndexInfoOfType(apparentType, 0 /* String */) : undefined;
- if (!(indexInfo && indexInfo.type)) {
- if (isJSLiteralType(leftType)) {
- return anyType;
+ function narrowTypeByCallExpression(type, callExpression, assumeTrue) {
+ if (hasMatchingArgument(callExpression, reference)) {
+ var signature = assumeTrue || !ts.isCallChain(callExpression) ? getEffectsSignature(callExpression) : undefined;
+ var predicate = signature && getTypePredicateOfSignature(signature);
+ if (predicate && (predicate.kind === 0 /* This */ || predicate.kind === 1 /* Identifier */)) {
+ return narrowTypeByTypePredicate(type, predicate, callExpression, assumeTrue);
}
- if (leftType.symbol === globalThisSymbol) {
- if (globalThisSymbol.exports.has(right.escapedText) && (globalThisSymbol.exports.get(right.escapedText).flags & 418 /* BlockScoped */)) {
- error(right, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.unescapeLeadingUnderscores(right.escapedText), typeToString(leftType));
+ }
+ return type;
+ }
+ function narrowTypeByTypePredicate(type, predicate, callExpression, assumeTrue) {
+ // Don't narrow from 'any' if the predicate type is exactly 'Object' or 'Function'
+ if (predicate.type && !(isTypeAny(type) && (predicate.type === globalObjectType || predicate.type === globalFunctionType))) {
+ var predicateArgument = getTypePredicateArgument(predicate, callExpression);
+ if (predicateArgument) {
+ if (isMatchingReference(reference, predicateArgument)) {
+ return getNarrowedType(type, predicate.type, assumeTrue, isTypeSubtypeOf);
}
- else if (noImplicitAny) {
- error(right, ts.Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature, typeToString(leftType));
+ if (strictNullChecks && assumeTrue && optionalChainContainsReference(predicateArgument, reference) &&
+ !(getTypeFacts(predicate.type) & 65536 /* EQUndefined */)) {
+ type = getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */);
+ }
+ if (isMatchingReferenceDiscriminant(predicateArgument, type)) {
+ return narrowTypeByDiscriminant(type, predicateArgument, function (t) { return getNarrowedType(t, predicate.type, assumeTrue, isTypeSubtypeOf); });
}
- return anyType;
- }
- if (right.escapedText && !checkAndReportErrorForExtendingInterface(node)) {
- reportNonexistentProperty(right, isThisTypeParameter(leftType) ? apparentType : leftType);
}
- return errorType;
}
- if (indexInfo.isReadonly && (ts.isAssignmentTarget(node) || ts.isDeleteTarget(node))) {
- error(node, ts.Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(apparentType));
+ return type;
+ }
+ // Narrow the given type based on the given expression having the assumed boolean value. The returned type
+ // will be a subtype or the same type as the argument.
+ function narrowType(type, expr, assumeTrue) {
+ // for `a?.b`, we emulate a synthetic `a !== null && a !== undefined` condition for `a`
+ if (ts.isExpressionOfOptionalChainRoot(expr) ||
+ ts.isBinaryExpression(expr.parent) && expr.parent.operatorToken.kind === 60 /* QuestionQuestionToken */ && expr.parent.left === expr) {
+ return narrowTypeByOptionality(type, expr, assumeTrue);
+ }
+ switch (expr.kind) {
+ case 78 /* Identifier */:
+ case 107 /* ThisKeyword */:
+ case 105 /* SuperKeyword */:
+ case 201 /* PropertyAccessExpression */:
+ case 202 /* ElementAccessExpression */:
+ return narrowTypeByTruthiness(type, expr, assumeTrue);
+ case 203 /* CallExpression */:
+ return narrowTypeByCallExpression(type, expr, assumeTrue);
+ case 207 /* ParenthesizedExpression */:
+ case 225 /* NonNullExpression */:
+ return narrowType(type, expr.expression, assumeTrue);
+ case 216 /* BinaryExpression */:
+ return narrowTypeByBinaryExpression(type, expr, assumeTrue);
+ case 214 /* PrefixUnaryExpression */:
+ if (expr.operator === 53 /* ExclamationToken */) {
+ return narrowType(type, expr.operand, !assumeTrue);
+ }
+ break;
}
- propType = indexInfo.type;
+ return type;
}
- else {
- checkPropertyNotUsedBeforeDeclaration(prop, node, right);
- markPropertyAsReferenced(prop, node, left.kind === 104 /* ThisKeyword */);
- getNodeLinks(node).resolvedSymbol = prop;
- checkPropertyAccessibility(node, left.kind === 102 /* SuperKeyword */, apparentType, prop);
- if (isAssignmentToReadonlyEntity(node, prop, assignmentKind)) {
- error(right, ts.Diagnostics.Cannot_assign_to_0_because_it_is_a_read_only_property, ts.idText(right));
- return errorType;
+ function narrowTypeByOptionality(type, expr, assumePresent) {
+ if (isMatchingReference(reference, expr)) {
+ return getTypeWithFacts(type, assumePresent ? 2097152 /* NEUndefinedOrNull */ : 262144 /* EQUndefinedOrNull */);
+ }
+ if (isMatchingReferenceDiscriminant(expr, type)) {
+ return narrowTypeByDiscriminant(type, expr, function (t) { return getTypeWithFacts(t, assumePresent ? 2097152 /* NEUndefinedOrNull */ : 262144 /* EQUndefinedOrNull */); });
}
- propType = getConstraintForLocation(getTypeOfSymbol(prop), node);
+ return type;
}
- return getFlowTypeOfAccessExpression(node, prop, propType, right);
}
- function getFlowTypeOfAccessExpression(node, prop, propType, errorNode) {
- // Only compute control flow type if this is a property access expression that isn't an
- // assignment target, and the referenced property was declared as a variable, property,
- // accessor, or optional method.
- var assignmentKind = ts.getAssignmentTargetKind(node);
- if (!ts.isAccessExpression(node) ||
- assignmentKind === 1 /* Definite */ ||
- prop && !(prop.flags & (3 /* Variable */ | 4 /* Property */ | 98304 /* Accessor */)) && !(prop.flags & 8192 /* Method */ && propType.flags & 1048576 /* Union */)) {
- return propType;
- }
- // If strict null checks and strict property initialization checks are enabled, if we have
- // a this.xxx property access, if the property is an instance property without an initializer,
- // and if we are in a constructor of the same class as the property declaration, assume that
- // the property is uninitialized at the top of the control flow.
- var assumeUninitialized = false;
- if (strictNullChecks && strictPropertyInitialization && node.expression.kind === 104 /* ThisKeyword */) {
- var declaration = prop && prop.valueDeclaration;
- if (declaration && isInstancePropertyWithoutInitializer(declaration)) {
- var flowContainer = getControlFlowContainer(node);
- if (flowContainer.kind === 162 /* Constructor */ && flowContainer.parent === declaration.parent && !(declaration.flags & 8388608 /* Ambient */)) {
- assumeUninitialized = true;
+ function getTypeOfSymbolAtLocation(symbol, location) {
+ symbol = symbol.exportSymbol || symbol;
+ // If we have an identifier or a property access at the given location, if the location is
+ // an dotted name expression, and if the location is not an assignment target, obtain the type
+ // of the expression (which will reflect control flow analysis). If the expression indeed
+ // resolved to the given symbol, return the narrowed type.
+ if (location.kind === 78 /* Identifier */) {
+ if (ts.isRightSideOfQualifiedNameOrPropertyAccess(location)) {
+ location = location.parent;
+ }
+ if (ts.isExpressionNode(location) && !ts.isAssignmentTarget(location)) {
+ var type = getTypeOfExpression(location);
+ if (getExportSymbolOfValueSymbolIfExported(getNodeLinks(location).resolvedSymbol) === symbol) {
+ return type;
}
}
}
- else if (strictNullChecks && prop && prop.valueDeclaration &&
- ts.isPropertyAccessExpression(prop.valueDeclaration) &&
- ts.getAssignmentDeclarationPropertyAccessKind(prop.valueDeclaration) &&
- getControlFlowContainer(node) === getControlFlowContainer(prop.valueDeclaration)) {
- assumeUninitialized = true;
- }
- var flowType = getFlowTypeOfReference(node, propType, assumeUninitialized ? getOptionalType(propType) : propType);
- if (assumeUninitialized && !(getFalsyFlags(propType) & 32768 /* Undefined */) && getFalsyFlags(flowType) & 32768 /* Undefined */) {
- error(errorNode, ts.Diagnostics.Property_0_is_used_before_being_assigned, symbolToString(prop)); // TODO: GH#18217
- // Return the declared type to reduce follow-on errors
- return propType;
- }
- return assignmentKind ? getBaseTypeOfLiteralType(flowType) : flowType;
- }
- function checkPropertyNotUsedBeforeDeclaration(prop, node, right) {
- var valueDeclaration = prop.valueDeclaration;
- if (!valueDeclaration || ts.getSourceFileOfNode(node).isDeclarationFile) {
- return;
- }
- var diagnosticMessage;
- var declarationName = ts.idText(right);
- if (isInPropertyInitializer(node)
- && !(ts.isAccessExpression(node) && ts.isAccessExpression(node.expression))
- && !isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right)
- && !isPropertyDeclaredInAncestorClass(prop)) {
- diagnosticMessage = error(right, ts.Diagnostics.Property_0_is_used_before_its_initialization, declarationName);
- }
- else if (valueDeclaration.kind === 245 /* ClassDeclaration */ &&
- node.parent.kind !== 169 /* TypeReference */ &&
- !(valueDeclaration.flags & 8388608 /* Ambient */) &&
- !isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right)) {
- diagnosticMessage = error(right, ts.Diagnostics.Class_0_used_before_its_declaration, declarationName);
- }
- if (diagnosticMessage) {
- ts.addRelatedInfo(diagnosticMessage, ts.createDiagnosticForNode(valueDeclaration, ts.Diagnostics._0_is_declared_here, declarationName));
- }
+ // The location isn't a reference to the given symbol, meaning we're being asked
+ // a hypothetical question of what type the symbol would have if there was a reference
+ // to it at the given location. Since we have no control flow information for the
+ // hypothetical reference (control flow information is created and attached by the
+ // binder), we simply return the declared type of the symbol.
+ return getTypeOfSymbol(symbol);
}
- function isInPropertyInitializer(node) {
- return !!ts.findAncestor(node, function (node) {
- switch (node.kind) {
- case 159 /* PropertyDeclaration */:
- return true;
- case 281 /* PropertyAssignment */:
- case 161 /* MethodDeclaration */:
- case 163 /* GetAccessor */:
- case 164 /* SetAccessor */:
- case 283 /* SpreadAssignment */:
- case 154 /* ComputedPropertyName */:
- case 221 /* TemplateSpan */:
- case 276 /* JsxExpression */:
- case 273 /* JsxAttribute */:
- case 274 /* JsxAttributes */:
- case 275 /* JsxSpreadAttribute */:
- case 268 /* JsxOpeningElement */:
- case 216 /* ExpressionWithTypeArguments */:
- case 279 /* HeritageClause */:
- return false;
- default:
- return ts.isExpressionNode(node) ? false : "quit";
- }
+ function getControlFlowContainer(node) {
+ return ts.findAncestor(node.parent, function (node) {
+ return ts.isFunctionLike(node) && !ts.getImmediatelyInvokedFunctionExpression(node) ||
+ node.kind === 257 /* ModuleBlock */ ||
+ node.kind === 297 /* SourceFile */ ||
+ node.kind === 163 /* PropertyDeclaration */;
});
}
- /**
- * It's possible that "prop.valueDeclaration" is a local declaration, but the property was also declared in a superclass.
- * In that case we won't consider it used before its declaration, because it gets its value from the superclass' declaration.
- */
- function isPropertyDeclaredInAncestorClass(prop) {
- if (!(prop.parent.flags & 32 /* Class */)) {
- return false;
- }
- var classType = getTypeOfSymbol(prop.parent);
- while (true) {
- classType = classType.symbol && getSuperClass(classType);
- if (!classType) {
- return false;
- }
- var superProperty = getPropertyOfType(classType, prop.escapedName);
- if (superProperty && superProperty.valueDeclaration) {
- return true;
+ // Check if a parameter is assigned anywhere within its declaring function.
+ function isParameterAssigned(symbol) {
+ var func = ts.getRootDeclaration(symbol.valueDeclaration).parent;
+ var links = getNodeLinks(func);
+ if (!(links.flags & 8388608 /* AssignmentsMarked */)) {
+ links.flags |= 8388608 /* AssignmentsMarked */;
+ if (!hasParentWithAssignmentsMarked(func)) {
+ markParameterAssignments(func);
}
}
+ return symbol.isAssigned || false;
}
- function getSuperClass(classType) {
- var x = getBaseTypes(classType);
- if (x.length === 0) {
- return undefined;
- }
- return getIntersectionType(x);
+ function hasParentWithAssignmentsMarked(node) {
+ return !!ts.findAncestor(node.parent, function (node) { return ts.isFunctionLike(node) && !!(getNodeLinks(node).flags & 8388608 /* AssignmentsMarked */); });
}
- function reportNonexistentProperty(propNode, containingType) {
- var errorInfo;
- var relatedInfo;
- if (!ts.isPrivateIdentifier(propNode) && containingType.flags & 1048576 /* Union */ && !(containingType.flags & 131068 /* Primitive */)) {
- for (var _i = 0, _a = containingType.types; _i < _a.length; _i++) {
- var subtype = _a[_i];
- if (!getPropertyOfType(subtype, propNode.escapedText) && !getIndexInfoOfType(subtype, 0 /* String */)) {
- errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.declarationNameToString(propNode), typeToString(subtype));
- break;
+ function markParameterAssignments(node) {
+ if (node.kind === 78 /* Identifier */) {
+ if (ts.isAssignmentTarget(node)) {
+ var symbol = getResolvedSymbol(node);
+ if (symbol.valueDeclaration && ts.getRootDeclaration(symbol.valueDeclaration).kind === 160 /* Parameter */) {
+ symbol.isAssigned = true;
}
}
}
- if (typeHasStaticProperty(propNode.escapedText, containingType)) {
- errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_is_a_static_member_of_type_1, ts.declarationNameToString(propNode), typeToString(containingType));
- }
else {
- var promisedType = getPromisedTypeOfPromise(containingType);
- if (promisedType && getPropertyOfType(promisedType, propNode.escapedText)) {
- errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.declarationNameToString(propNode), typeToString(containingType));
- relatedInfo = ts.createDiagnosticForNode(propNode, ts.Diagnostics.Did_you_forget_to_use_await);
- }
- else {
- var suggestion = getSuggestedSymbolForNonexistentProperty(propNode, containingType);
- if (suggestion !== undefined) {
- var suggestedName = ts.symbolName(suggestion);
- errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2, ts.declarationNameToString(propNode), typeToString(containingType), suggestedName);
- relatedInfo = suggestion.valueDeclaration && ts.createDiagnosticForNode(suggestion.valueDeclaration, ts.Diagnostics._0_is_declared_here, suggestedName);
- }
- else {
- errorInfo = ts.chainDiagnosticMessages(elaborateNeverIntersection(errorInfo, containingType), ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.declarationNameToString(propNode), typeToString(containingType));
- }
- }
- }
- var resultDiagnostic = ts.createDiagnosticForNodeFromMessageChain(propNode, errorInfo);
- if (relatedInfo) {
- ts.addRelatedInfo(resultDiagnostic, relatedInfo);
+ ts.forEachChild(node, markParameterAssignments);
}
- diagnostics.add(resultDiagnostic);
- }
- function typeHasStaticProperty(propName, containingType) {
- var prop = containingType.symbol && getPropertyOfType(getTypeOfSymbol(containingType.symbol), propName);
- return prop !== undefined && prop.valueDeclaration && ts.hasModifier(prop.valueDeclaration, 32 /* Static */);
}
- function getSuggestedSymbolForNonexistentProperty(name, containingType) {
- return getSpellingSuggestionForName(ts.isString(name) ? name : ts.idText(name), getPropertiesOfType(containingType), 111551 /* Value */);
+ function isConstVariable(symbol) {
+ return symbol.flags & 3 /* Variable */ && (getDeclarationNodeFlagsFromSymbol(symbol) & 2 /* Const */) !== 0 && getTypeOfSymbol(symbol) !== autoArrayType;
}
- function getSuggestionForNonexistentProperty(name, containingType) {
- var suggestion = getSuggestedSymbolForNonexistentProperty(name, containingType);
- return suggestion && ts.symbolName(suggestion);
+ /** remove undefined from the annotated type of a parameter when there is an initializer (that doesn't include undefined) */
+ function removeOptionalityFromDeclaredType(declaredType, declaration) {
+ if (pushTypeResolution(declaration.symbol, 2 /* DeclaredType */)) {
+ var annotationIncludesUndefined = strictNullChecks &&
+ declaration.kind === 160 /* Parameter */ &&
+ declaration.initializer &&
+ getFalsyFlags(declaredType) & 32768 /* Undefined */ &&
+ !(getFalsyFlags(checkExpression(declaration.initializer)) & 32768 /* Undefined */);
+ popTypeResolution();
+ return annotationIncludesUndefined ? getTypeWithFacts(declaredType, 524288 /* NEUndefined */) : declaredType;
+ }
+ else {
+ reportCircularityError(declaration.symbol);
+ return declaredType;
+ }
}
- function getSuggestedSymbolForNonexistentSymbol(location, outerName, meaning) {
- ts.Debug.assert(outerName !== undefined, "outername should always be defined");
- var result = resolveNameHelper(location, outerName, meaning, /*nameNotFoundMessage*/ undefined, outerName, /*isUse*/ false, /*excludeGlobals*/ false, function (symbols, name, meaning) {
- ts.Debug.assertEqual(outerName, name, "name should equal outerName");
- var symbol = getSymbol(symbols, name, meaning);
- // Sometimes the symbol is found when location is a return type of a function: `typeof x` and `x` is declared in the body of the function
- // So the table *contains* `x` but `x` isn't actually in scope.
- // However, resolveNameHelper will continue and call this callback again, so we'll eventually get a correct suggestion.
- return symbol || getSpellingSuggestionForName(ts.unescapeLeadingUnderscores(name), ts.arrayFrom(symbols.values()), meaning);
- });
- return result;
+ function isConstraintPosition(node) {
+ var parent = node.parent;
+ return parent.kind === 201 /* PropertyAccessExpression */ ||
+ parent.kind === 203 /* CallExpression */ && parent.expression === node ||
+ parent.kind === 202 /* ElementAccessExpression */ && parent.expression === node ||
+ parent.kind === 198 /* BindingElement */ && parent.name === node && !!parent.initializer;
}
- function getSuggestionForNonexistentSymbol(location, outerName, meaning) {
- var symbolResult = getSuggestedSymbolForNonexistentSymbol(location, outerName, meaning);
- return symbolResult && ts.symbolName(symbolResult);
+ function typeHasNullableConstraint(type) {
+ return type.flags & 58982400 /* InstantiableNonPrimitive */ && maybeTypeOfKind(getBaseConstraintOfType(type) || unknownType, 98304 /* Nullable */);
}
- function getSuggestedSymbolForNonexistentModule(name, targetModule) {
- return targetModule.exports && getSpellingSuggestionForName(ts.idText(name), getExportsOfModuleAsArray(targetModule), 2623475 /* ModuleMember */);
+ function getConstraintForLocation(type, node) {
+ // When a node is the left hand expression of a property access, element access, or call expression,
+ // and the type of the node includes type variables with constraints that are nullable, we fetch the
+ // apparent type of the node *before* performing control flow analysis such that narrowings apply to
+ // the constraint type.
+ if (type && isConstraintPosition(node) && forEachType(type, typeHasNullableConstraint)) {
+ return mapType(getWidenedType(type), getBaseConstraintOrType);
+ }
+ return type;
}
- function getSuggestionForNonexistentExport(name, targetModule) {
- var suggestion = getSuggestedSymbolForNonexistentModule(name, targetModule);
- return suggestion && ts.symbolName(suggestion);
+ function isExportOrExportExpression(location) {
+ return !!ts.findAncestor(location, function (e) { return e.parent && ts.isExportAssignment(e.parent) && e.parent.expression === e && ts.isEntityNameExpression(e); });
}
- function getSuggestionForNonexistentIndexSignature(objectType, expr, keyedType) {
- // check if object type has setter or getter
- function hasProp(name) {
- var prop = getPropertyOfObjectType(objectType, name);
- if (prop) {
- var s = getSingleCallSignature(getTypeOfSymbol(prop));
- return !!s && getMinArgumentCount(s) >= 1 && isTypeAssignableTo(keyedType, getTypeAtPosition(s, 0));
+ function markAliasReferenced(symbol, location) {
+ if (isNonLocalAlias(symbol, /*excludes*/ 111551 /* Value */) && !isInTypeQuery(location) && !getTypeOnlyAliasDeclaration(symbol)) {
+ if (compilerOptions.preserveConstEnums && isExportOrExportExpression(location) || !isConstEnumOrConstEnumOnlyModule(resolveAlias(symbol))) {
+ markAliasSymbolAsReferenced(symbol);
+ }
+ else {
+ markConstEnumAliasAsReferenced(symbol);
}
- return false;
- }
- ;
- var suggestedMethod = ts.isAssignmentTarget(expr) ? "set" : "get";
- if (!hasProp(suggestedMethod)) {
- return undefined;
- }
- var suggestion = ts.tryGetPropertyAccessOrIdentifierToString(expr.expression);
- if (suggestion === undefined) {
- suggestion = suggestedMethod;
- }
- else {
- suggestion += "." + suggestedMethod;
}
- return suggestion;
}
- /**
- * Given a name and a list of symbols whose names are *not* equal to the name, return a spelling suggestion if there is one that is close enough.
- * Names less than length 3 only check for case-insensitive equality, not levenshtein distance.
- *
- * If there is a candidate that's the same except for case, return that.
- * If there is a candidate that's within one edit of the name, return that.
- * Otherwise, return the candidate with the smallest Levenshtein distance,
- * except for candidates:
- * * With no name
- * * Whose meaning doesn't match the `meaning` parameter.
- * * Whose length differs from the target name by more than 0.34 of the length of the name.
- * * Whose levenshtein distance is more than 0.4 of the length of the name
- * (0.4 allows 1 substitution/transposition for every 5 characters,
- * and 1 insertion/deletion at 3 characters)
- */
- function getSpellingSuggestionForName(name, symbols, meaning) {
- return ts.getSpellingSuggestion(name, symbols, getCandidateName);
- function getCandidateName(candidate) {
- var candidateName = ts.symbolName(candidate);
- if (ts.startsWith(candidateName, "\"")) {
- return undefined;
- }
- if (candidate.flags & meaning) {
- return candidateName;
- }
- if (candidate.flags & 2097152 /* Alias */) {
- var alias = tryResolveAlias(candidate);
- if (alias && alias.flags & meaning) {
- return candidateName;
+ function checkIdentifier(node) {
+ var symbol = getResolvedSymbol(node);
+ if (symbol === unknownSymbol) {
+ return errorType;
+ }
+ // As noted in ECMAScript 6 language spec, arrow functions never have an arguments objects.
+ // Although in down-level emit of arrow function, we emit it using function expression which means that
+ // arguments objects will be bound to the inner object; emitting arrow function natively in ES6, arguments objects
+ // will be bound to non-arrow function that contain this arrow function. This results in inconsistent behavior.
+ // To avoid that we will give an error to users if they use arguments objects in arrow function so that they
+ // can explicitly bound arguments objects
+ if (symbol === argumentsSymbol) {
+ var container = ts.getContainingFunction(node);
+ if (languageVersion < 2 /* ES2015 */) {
+ if (container.kind === 209 /* ArrowFunction */) {
+ error(node, ts.Diagnostics.The_arguments_object_cannot_be_referenced_in_an_arrow_function_in_ES3_and_ES5_Consider_using_a_standard_function_expression);
+ }
+ else if (ts.hasSyntacticModifier(container, 256 /* Async */)) {
+ error(node, ts.Diagnostics.The_arguments_object_cannot_be_referenced_in_an_async_function_or_method_in_ES3_and_ES5_Consider_using_a_standard_function_or_method);
}
}
- return undefined;
+ getNodeLinks(container).flags |= 8192 /* CaptureArguments */;
+ return getTypeOfSymbol(symbol);
}
- }
- function markPropertyAsReferenced(prop, nodeForCheckWriteOnly, isThisAccess) {
- var valueDeclaration = prop && (prop.flags & 106500 /* ClassMember */) && prop.valueDeclaration;
- if (!valueDeclaration) {
- return;
+ // We should only mark aliases as referenced if there isn't a local value declaration
+ // for the symbol. Also, don't mark any property access expression LHS - checkPropertyAccessExpression will handle that
+ if (!(node.parent && ts.isPropertyAccessExpression(node.parent) && node.parent.expression === node)) {
+ markAliasReferenced(symbol, node);
}
- var hasPrivateModifier = ts.hasModifier(valueDeclaration, 8 /* Private */);
- var hasPrivateIdentifier = ts.isNamedDeclaration(prop.valueDeclaration) && ts.isPrivateIdentifier(prop.valueDeclaration.name);
- if (!hasPrivateModifier && !hasPrivateIdentifier) {
- return;
+ var localOrExportSymbol = getExportSymbolOfValueSymbolIfExported(symbol);
+ var sourceSymbol = localOrExportSymbol.flags & 2097152 /* Alias */ ? resolveAlias(localOrExportSymbol) : localOrExportSymbol;
+ if (getDeclarationNodeFlagsFromSymbol(sourceSymbol) & 134217728 /* Deprecated */ && isUncalledFunctionReference(node.parent, sourceSymbol)) {
+ errorOrSuggestion(/* isError */ false, node, ts.Diagnostics._0_is_deprecated, node.escapedText);
}
- if (nodeForCheckWriteOnly && ts.isWriteOnlyAccess(nodeForCheckWriteOnly) && !(prop.flags & 65536 /* SetAccessor */)) {
- return;
+ var declaration = localOrExportSymbol.valueDeclaration;
+ if (localOrExportSymbol.flags & 32 /* Class */) {
+ // Due to the emit for class decorators, any reference to the class from inside of the class body
+ // must instead be rewritten to point to a temporary variable to avoid issues with the double-bind
+ // behavior of class names in ES6.
+ if (declaration.kind === 252 /* ClassDeclaration */
+ && ts.nodeIsDecorated(declaration)) {
+ var container = ts.getContainingClass(node);
+ while (container !== undefined) {
+ if (container === declaration && container.name !== node) {
+ getNodeLinks(declaration).flags |= 16777216 /* ClassWithConstructorReference */;
+ getNodeLinks(node).flags |= 33554432 /* ConstructorReferenceInClass */;
+ break;
+ }
+ container = ts.getContainingClass(container);
+ }
+ }
+ else if (declaration.kind === 221 /* ClassExpression */) {
+ // When we emit a class expression with static members that contain a reference
+ // to the constructor in the initializer, we will need to substitute that
+ // binding with an alias as the class name is not in scope.
+ var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false);
+ while (container.kind !== 297 /* SourceFile */) {
+ if (container.parent === declaration) {
+ if (container.kind === 163 /* PropertyDeclaration */ && ts.hasSyntacticModifier(container, 32 /* Static */)) {
+ getNodeLinks(declaration).flags |= 16777216 /* ClassWithConstructorReference */;
+ getNodeLinks(node).flags |= 33554432 /* ConstructorReferenceInClass */;
+ }
+ break;
+ }
+ container = ts.getThisContainer(container, /*includeArrowFunctions*/ false);
+ }
+ }
}
- if (isThisAccess) {
- // Find any FunctionLikeDeclaration because those create a new 'this' binding. But this should only matter for methods (or getters/setters).
- var containingMethod = ts.findAncestor(nodeForCheckWriteOnly, ts.isFunctionLikeDeclaration);
- if (containingMethod && containingMethod.symbol === prop) {
- return;
+ checkNestedBlockScopedBinding(node, symbol);
+ var type = getConstraintForLocation(getTypeOfSymbol(localOrExportSymbol), node);
+ var assignmentKind = ts.getAssignmentTargetKind(node);
+ if (assignmentKind) {
+ if (!(localOrExportSymbol.flags & 3 /* Variable */) &&
+ !(ts.isInJSFile(node) && localOrExportSymbol.flags & 512 /* ValueModule */)) {
+ error(node, ts.Diagnostics.Cannot_assign_to_0_because_it_is_not_a_variable, symbolToString(symbol));
+ return errorType;
+ }
+ if (isReadonlySymbol(localOrExportSymbol)) {
+ if (localOrExportSymbol.flags & 3 /* Variable */) {
+ error(node, ts.Diagnostics.Cannot_assign_to_0_because_it_is_a_constant, symbolToString(symbol));
+ }
+ else {
+ error(node, ts.Diagnostics.Cannot_assign_to_0_because_it_is_a_read_only_property, symbolToString(symbol));
+ }
+ return errorType;
}
}
- (ts.getCheckFlags(prop) & 1 /* Instantiated */ ? getSymbolLinks(prop).target : prop).isReferenced = 67108863 /* All */;
- }
- function isValidPropertyAccess(node, propertyName) {
- switch (node.kind) {
- case 194 /* PropertyAccessExpression */:
- return isValidPropertyAccessWithType(node, node.expression.kind === 102 /* SuperKeyword */, propertyName, getWidenedType(checkExpression(node.expression)));
- case 153 /* QualifiedName */:
- return isValidPropertyAccessWithType(node, /*isSuper*/ false, propertyName, getWidenedType(checkExpression(node.left)));
- case 188 /* ImportType */:
- return isValidPropertyAccessWithType(node, /*isSuper*/ false, propertyName, getTypeFromTypeNode(node));
+ var isAlias = localOrExportSymbol.flags & 2097152 /* Alias */;
+ // We only narrow variables and parameters occurring in a non-assignment position. For all other
+ // entities we simply return the declared type.
+ if (localOrExportSymbol.flags & 3 /* Variable */) {
+ if (assignmentKind === 1 /* Definite */) {
+ return type;
+ }
}
- }
- function isValidPropertyAccessForCompletions(node, type, property) {
- return isValidPropertyAccessWithType(node, node.kind === 194 /* PropertyAccessExpression */ && node.expression.kind === 102 /* SuperKeyword */, property.escapedName, type);
- // Previously we validated the 'this' type of methods but this adversely affected performance. See #31377 for more context.
- }
- function isValidPropertyAccessWithType(node, isSuper, propertyName, type) {
- if (type === errorType || isTypeAny(type)) {
- return true;
+ else if (isAlias) {
+ declaration = ts.find(symbol.declarations, isSomeImportDeclaration);
}
- var prop = getPropertyOfType(type, propertyName);
- if (prop) {
- if (ts.isPropertyAccessExpression(node) && prop.valueDeclaration && ts.isPrivateIdentifierPropertyDeclaration(prop.valueDeclaration)) {
- var declClass_1 = ts.getContainingClass(prop.valueDeclaration);
- return !ts.isOptionalChain(node) && !!ts.findAncestor(node, function (parent) { return parent === declClass_1; });
- }
- return checkPropertyAccessibility(node, isSuper, type, prop);
+ else {
+ return type;
}
- // In js files properties of unions are allowed in completion
- return ts.isInJSFile(node) && (type.flags & 1048576 /* Union */) !== 0 && type.types.some(function (elementType) { return isValidPropertyAccessWithType(node, isSuper, propertyName, elementType); });
- }
- /**
- * Return the symbol of the for-in variable declared or referenced by the given for-in statement.
- */
- function getForInVariableSymbol(node) {
- var initializer = node.initializer;
- if (initializer.kind === 243 /* VariableDeclarationList */) {
- var variable = initializer.declarations[0];
- if (variable && !ts.isBindingPattern(variable.name)) {
- return getSymbolOfNode(variable);
- }
+ if (!declaration) {
+ return type;
}
- else if (initializer.kind === 75 /* Identifier */) {
- return getResolvedSymbol(initializer);
+ // The declaration container is the innermost function that encloses the declaration of the variable
+ // or parameter. The flow container is the innermost function starting with which we analyze the control
+ // flow graph to determine the control flow based type.
+ var isParameter = ts.getRootDeclaration(declaration).kind === 160 /* Parameter */;
+ var declarationContainer = getControlFlowContainer(declaration);
+ var flowContainer = getControlFlowContainer(node);
+ var isOuterVariable = flowContainer !== declarationContainer;
+ var isSpreadDestructuringAssignmentTarget = node.parent && node.parent.parent && ts.isSpreadAssignment(node.parent) && isDestructuringAssignmentTarget(node.parent.parent);
+ var isModuleExports = symbol.flags & 134217728 /* ModuleExports */;
+ // When the control flow originates in a function expression or arrow function and we are referencing
+ // a const variable or parameter from an outer function, we extend the origin of the control flow
+ // analysis to include the immediately enclosing function.
+ while (flowContainer !== declarationContainer && (flowContainer.kind === 208 /* FunctionExpression */ ||
+ flowContainer.kind === 209 /* ArrowFunction */ || ts.isObjectLiteralOrClassExpressionMethod(flowContainer)) &&
+ (isConstVariable(localOrExportSymbol) || isParameter && !isParameterAssigned(localOrExportSymbol))) {
+ flowContainer = getControlFlowContainer(flowContainer);
}
- return undefined;
- }
- /**
- * Return true if the given type is considered to have numeric property names.
- */
- function hasNumericPropertyNames(type) {
- return getIndexTypeOfType(type, 1 /* Number */) && !getIndexTypeOfType(type, 0 /* String */);
- }
- /**
- * Return true if given node is an expression consisting of an identifier (possibly parenthesized)
- * that references a for-in variable for an object with numeric property names.
- */
- function isForInVariableForNumericPropertyNames(expr) {
- var e = ts.skipParentheses(expr);
- if (e.kind === 75 /* Identifier */) {
- var symbol = getResolvedSymbol(e);
- if (symbol.flags & 3 /* Variable */) {
- var child = expr;
- var node = expr.parent;
- while (node) {
- if (node.kind === 231 /* ForInStatement */ &&
- child === node.statement &&
- getForInVariableSymbol(node) === symbol &&
- hasNumericPropertyNames(getTypeOfExpression(node.expression))) {
- return true;
- }
- child = node;
- node = node.parent;
+ // We only look for uninitialized variables in strict null checking mode, and only when we can analyze
+ // the entire control flow graph from the variable's declaration (i.e. when the flow container and
+ // declaration container are the same).
+ var assumeInitialized = isParameter || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports || ts.isBindingElement(declaration) ||
+ type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & (3 /* AnyOrUnknown */ | 16384 /* Void */)) !== 0 ||
+ isInTypeQuery(node) || node.parent.kind === 270 /* ExportSpecifier */) ||
+ node.parent.kind === 225 /* NonNullExpression */ ||
+ declaration.kind === 249 /* VariableDeclaration */ && declaration.exclamationToken ||
+ declaration.flags & 8388608 /* Ambient */;
+ var initialType = assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, declaration) : type) :
+ type === autoType || type === autoArrayType ? undefinedType :
+ getOptionalType(type);
+ var flowType = getFlowTypeOfReference(node, type, initialType, flowContainer, !assumeInitialized);
+ // A variable is considered uninitialized when it is possible to analyze the entire control flow graph
+ // from declaration to use, and when the variable's declared type doesn't include undefined but the
+ // control flow based type does include undefined.
+ if (!isEvolvingArrayOperationTarget(node) && (type === autoType || type === autoArrayType)) {
+ if (flowType === autoType || flowType === autoArrayType) {
+ if (noImplicitAny) {
+ error(ts.getNameOfDeclaration(declaration), ts.Diagnostics.Variable_0_implicitly_has_type_1_in_some_locations_where_its_type_cannot_be_determined, symbolToString(symbol), typeToString(flowType));
+ error(node, ts.Diagnostics.Variable_0_implicitly_has_an_1_type, symbolToString(symbol), typeToString(flowType));
}
+ return convertAutoToAny(flowType);
}
}
- return false;
- }
- function checkIndexedAccess(node) {
- return node.flags & 32 /* OptionalChain */ ? checkElementAccessChain(node) :
- checkElementAccessExpression(node, checkNonNullExpression(node.expression));
+ else if (!assumeInitialized && !(getFalsyFlags(type) & 32768 /* Undefined */) && getFalsyFlags(flowType) & 32768 /* Undefined */) {
+ error(node, ts.Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol));
+ // Return the declared type to reduce follow-on errors
+ return type;
+ }
+ return assignmentKind ? getBaseTypeOfLiteralType(flowType) : flowType;
}
- function checkElementAccessChain(node) {
- var exprType = checkExpression(node.expression);
- var nonOptionalType = getOptionalExpressionType(exprType, node.expression);
- return propagateOptionalTypeMarker(checkElementAccessExpression(node, checkNonNullType(nonOptionalType, node.expression)), node, nonOptionalType !== exprType);
+ function isInsideFunction(node, threshold) {
+ return !!ts.findAncestor(node, function (n) { return n === threshold ? "quit" : ts.isFunctionLike(n); });
}
- function checkElementAccessExpression(node, exprType) {
- var objectType = ts.getAssignmentTargetKind(node) !== 0 /* None */ || isMethodAccessForCall(node) ? getWidenedType(exprType) : exprType;
- var indexExpression = node.argumentExpression;
- var indexType = checkExpression(indexExpression);
- if (objectType === errorType || objectType === silentNeverType) {
- return objectType;
- }
- if (isConstEnumObjectType(objectType) && !ts.isStringLiteralLike(indexExpression)) {
- error(indexExpression, ts.Diagnostics.A_const_enum_member_can_only_be_accessed_using_a_string_literal);
- return errorType;
- }
- var effectiveIndexType = isForInVariableForNumericPropertyNames(indexExpression) ? numberType : indexType;
- var accessFlags = ts.isAssignmentTarget(node) ?
- 2 /* Writing */ | (isGenericObjectType(objectType) && !isThisTypeParameter(objectType) ? 1 /* NoIndexSignatures */ : 0) :
- 0 /* None */;
- var indexedAccessType = getIndexedAccessTypeOrUndefined(objectType, effectiveIndexType, node, accessFlags) || errorType;
- return checkIndexedAccessIndexType(getFlowTypeOfAccessExpression(node, indexedAccessType.symbol, indexedAccessType, indexExpression), node);
+ function getPartOfForStatementContainingNode(node, container) {
+ return ts.findAncestor(node, function (n) { return n === container ? "quit" : n === container.initializer || n === container.condition || n === container.incrementor || n === container.statement; });
}
- function checkThatExpressionIsProperSymbolReference(expression, expressionType, reportError) {
- if (expressionType === errorType) {
- // There is already an error, so no need to report one.
- return false;
- }
- if (!ts.isWellKnownSymbolSyntactically(expression)) {
- return false;
+ function checkNestedBlockScopedBinding(node, symbol) {
+ if (languageVersion >= 2 /* ES2015 */ ||
+ (symbol.flags & (2 /* BlockScopedVariable */ | 32 /* Class */)) === 0 ||
+ ts.isSourceFile(symbol.valueDeclaration) ||
+ symbol.valueDeclaration.parent.kind === 287 /* CatchClause */) {
+ return;
}
- // Make sure the property type is the primitive symbol type
- if ((expressionType.flags & 12288 /* ESSymbolLike */) === 0) {
- if (reportError) {
- error(expression, ts.Diagnostics.A_computed_property_name_of_the_form_0_must_be_of_type_symbol, ts.getTextOfNode(expression));
+ // 1. walk from the use site up to the declaration and check
+ // if there is anything function like between declaration and use-site (is binding/class is captured in function).
+ // 2. walk from the declaration up to the boundary of lexical environment and check
+ // if there is an iteration statement in between declaration and boundary (is binding/class declared inside iteration statement)
+ var container = ts.getEnclosingBlockScopeContainer(symbol.valueDeclaration);
+ var usedInFunction = isInsideFunction(node.parent, container);
+ var current = container;
+ var containedInIterationStatement = false;
+ while (current && !ts.nodeStartsNewLexicalEnvironment(current)) {
+ if (ts.isIterationStatement(current, /*lookInLabeledStatements*/ false)) {
+ containedInIterationStatement = true;
+ break;
}
- return false;
- }
- // The name is Symbol., so make sure Symbol actually resolves to the
- // global Symbol object
- var leftHandSide = expression.expression;
- var leftHandSideSymbol = getResolvedSymbol(leftHandSide);
- if (!leftHandSideSymbol) {
- return false;
- }
- var globalESSymbol = getGlobalESSymbolConstructorSymbol(/*reportErrors*/ true);
- if (!globalESSymbol) {
- // Already errored when we tried to look up the symbol
- return false;
+ current = current.parent;
}
- if (leftHandSideSymbol !== globalESSymbol) {
- if (reportError) {
- error(leftHandSide, ts.Diagnostics.Symbol_reference_does_not_refer_to_the_global_Symbol_constructor_object);
+ if (containedInIterationStatement) {
+ if (usedInFunction) {
+ // mark iteration statement as containing block-scoped binding captured in some function
+ var capturesBlockScopeBindingInLoopBody = true;
+ if (ts.isForStatement(container)) {
+ var varDeclList = ts.getAncestor(symbol.valueDeclaration, 250 /* VariableDeclarationList */);
+ if (varDeclList && varDeclList.parent === container) {
+ var part = getPartOfForStatementContainingNode(node.parent, container);
+ if (part) {
+ var links = getNodeLinks(part);
+ links.flags |= 131072 /* ContainsCapturedBlockScopeBinding */;
+ var capturedBindings = links.capturedBlockScopeBindings || (links.capturedBlockScopeBindings = []);
+ ts.pushIfUnique(capturedBindings, symbol);
+ if (part === container.initializer) {
+ capturesBlockScopeBindingInLoopBody = false; // Initializer is outside of loop body
+ }
+ }
+ }
+ }
+ if (capturesBlockScopeBindingInLoopBody) {
+ getNodeLinks(current).flags |= 65536 /* LoopWithCapturedBlockScopedBinding */;
+ }
}
- return false;
+ // mark variables that are declared in loop initializer and reassigned inside the body of ForStatement.
+ // if body of ForStatement will be converted to function then we'll need a extra machinery to propagate reassigned values back.
+ if (ts.isForStatement(container)) {
+ var varDeclList = ts.getAncestor(symbol.valueDeclaration, 250 /* VariableDeclarationList */);
+ if (varDeclList && varDeclList.parent === container && isAssignedInBodyOfForStatement(node, container)) {
+ getNodeLinks(symbol.valueDeclaration).flags |= 4194304 /* NeedsLoopOutParameter */;
+ }
+ }
+ // set 'declared inside loop' bit on the block-scoped binding
+ getNodeLinks(symbol.valueDeclaration).flags |= 524288 /* BlockScopedBindingInLoop */;
+ }
+ if (usedInFunction) {
+ getNodeLinks(symbol.valueDeclaration).flags |= 262144 /* CapturedBlockScopedBinding */;
}
- return true;
}
- function callLikeExpressionMayHaveTypeArguments(node) {
- return ts.isCallOrNewExpression(node) || ts.isTaggedTemplateExpression(node) || ts.isJsxOpeningLikeElement(node);
+ function isBindingCapturedByNode(node, decl) {
+ var links = getNodeLinks(node);
+ return !!links && ts.contains(links.capturedBlockScopeBindings, getSymbolOfNode(decl));
}
- function resolveUntypedCall(node) {
- if (callLikeExpressionMayHaveTypeArguments(node)) {
- // Check type arguments even though we will give an error that untyped calls may not accept type arguments.
- // This gets us diagnostics for the type arguments and marks them as referenced.
- ts.forEach(node.typeArguments, checkSourceElement);
+ function isAssignedInBodyOfForStatement(node, container) {
+ // skip parenthesized nodes
+ var current = node;
+ while (current.parent.kind === 207 /* ParenthesizedExpression */) {
+ current = current.parent;
}
- if (node.kind === 198 /* TaggedTemplateExpression */) {
- checkExpression(node.template);
+ // check if node is used as LHS in some assignment expression
+ var isAssigned = false;
+ if (ts.isAssignmentTarget(current)) {
+ isAssigned = true;
}
- else if (ts.isJsxOpeningLikeElement(node)) {
- checkExpression(node.attributes);
+ else if ((current.parent.kind === 214 /* PrefixUnaryExpression */ || current.parent.kind === 215 /* PostfixUnaryExpression */)) {
+ var expr = current.parent;
+ isAssigned = expr.operator === 45 /* PlusPlusToken */ || expr.operator === 46 /* MinusMinusToken */;
}
- else if (node.kind !== 157 /* Decorator */) {
- ts.forEach(node.arguments, function (argument) {
- checkExpression(argument);
- });
+ if (!isAssigned) {
+ return false;
}
- return anySignature;
- }
- function resolveErrorCall(node) {
- resolveUntypedCall(node);
- return unknownSignature;
+ // at this point we know that node is the target of assignment
+ // now check that modification happens inside the statement part of the ForStatement
+ return !!ts.findAncestor(current, function (n) { return n === container ? "quit" : n === container.statement; });
}
- // Re-order candidate signatures into the result array. Assumes the result array to be empty.
- // The candidate list orders groups in reverse, but within a group signatures are kept in declaration order
- // A nit here is that we reorder only signatures that belong to the same symbol,
- // so order how inherited signatures are processed is still preserved.
- // interface A { (x: string): void }
- // interface B extends A { (x: 'foo'): string }
- // const b: B;
- // b('foo') // <- here overloads should be processed as [(x:'foo'): string, (x: string): void]
- function reorderCandidates(signatures, result, callChainFlags) {
- var lastParent;
- var lastSymbol;
- var cutoffIndex = 0;
- var index;
- var specializedIndex = -1;
- var spliceIndex;
- ts.Debug.assert(!result.length);
- for (var _i = 0, signatures_7 = signatures; _i < signatures_7.length; _i++) {
- var signature = signatures_7[_i];
- var symbol = signature.declaration && getSymbolOfNode(signature.declaration);
- var parent = signature.declaration && signature.declaration.parent;
- if (!lastSymbol || symbol === lastSymbol) {
- if (lastParent && parent === lastParent) {
- index = index + 1;
- }
- else {
- lastParent = parent;
- index = cutoffIndex;
- }
- }
- else {
- // current declaration belongs to a different symbol
- // set cutoffIndex so re-orderings in the future won't change result set from 0 to cutoffIndex
- index = cutoffIndex = result.length;
- lastParent = parent;
- }
- lastSymbol = symbol;
- // specialized signatures always need to be placed before non-specialized signatures regardless
- // of the cutoff position; see GH#1133
- if (signatureHasLiteralTypes(signature)) {
- specializedIndex++;
- spliceIndex = specializedIndex;
- // The cutoff index always needs to be greater than or equal to the specialized signature index
- // in order to prevent non-specialized signatures from being added before a specialized
- // signature.
- cutoffIndex++;
- }
- else {
- spliceIndex = index;
- }
- result.splice(spliceIndex, 0, callChainFlags ? getOptionalCallSignature(signature, callChainFlags) : signature);
+ function captureLexicalThis(node, container) {
+ getNodeLinks(node).flags |= 2 /* LexicalThis */;
+ if (container.kind === 163 /* PropertyDeclaration */ || container.kind === 166 /* Constructor */) {
+ var classNode = container.parent;
+ getNodeLinks(classNode).flags |= 4 /* CaptureThis */;
+ }
+ else {
+ getNodeLinks(container).flags |= 4 /* CaptureThis */;
}
}
- function isSpreadArgument(arg) {
- return !!arg && (arg.kind === 213 /* SpreadElement */ || arg.kind === 220 /* SyntheticExpression */ && arg.isSpread);
- }
- function getSpreadArgumentIndex(args) {
- return ts.findIndex(args, isSpreadArgument);
+ function findFirstSuperCall(node) {
+ return ts.isSuperCall(node) ? node :
+ ts.isFunctionLike(node) ? undefined :
+ ts.forEachChild(node, findFirstSuperCall);
}
- function acceptsVoid(t) {
- return !!(t.flags & 16384 /* Void */);
+ /**
+ * Check if the given class-declaration extends null then return true.
+ * Otherwise, return false
+ * @param classDecl a class declaration to check if it extends null
+ */
+ function classDeclarationExtendsNull(classDecl) {
+ var classSymbol = getSymbolOfNode(classDecl);
+ var classInstanceType = getDeclaredTypeOfSymbol(classSymbol);
+ var baseConstructorType = getBaseConstructorTypeOfClass(classInstanceType);
+ return baseConstructorType === nullWideningType;
}
- function hasCorrectArity(node, args, signature, signatureHelpTrailingComma) {
- if (signatureHelpTrailingComma === void 0) { signatureHelpTrailingComma = false; }
- var argCount;
- var callIsIncomplete = false; // In incomplete call we want to be lenient when we have too few arguments
- var effectiveParameterCount = getParameterCount(signature);
- var effectiveMinimumArguments = getMinArgumentCount(signature);
- if (node.kind === 198 /* TaggedTemplateExpression */) {
- argCount = args.length;
- if (node.template.kind === 211 /* TemplateExpression */) {
- // If a tagged template expression lacks a tail literal, the call is incomplete.
- // Specifically, a template only can end in a TemplateTail or a Missing literal.
- var lastSpan = ts.last(node.template.templateSpans); // we should always have at least one span.
- callIsIncomplete = ts.nodeIsMissing(lastSpan.literal) || !!lastSpan.literal.isUnterminated;
- }
- else {
- // If the template didn't end in a backtick, or its beginning occurred right prior to EOF,
- // then this might actually turn out to be a TemplateHead in the future;
- // so we consider the call to be incomplete.
- var templateLiteral = node.template;
- ts.Debug.assert(templateLiteral.kind === 14 /* NoSubstitutionTemplateLiteral */);
- callIsIncomplete = !!templateLiteral.isUnterminated;
+ function checkThisBeforeSuper(node, container, diagnosticMessage) {
+ var containingClassDecl = container.parent;
+ var baseTypeNode = ts.getClassExtendsHeritageElement(containingClassDecl);
+ // If a containing class does not have extends clause or the class extends null
+ // skip checking whether super statement is called before "this" accessing.
+ if (baseTypeNode && !classDeclarationExtendsNull(containingClassDecl)) {
+ if (node.flowNode && !isPostSuperFlowNode(node.flowNode, /*noCacheCheck*/ false)) {
+ error(node, diagnosticMessage);
}
}
- else if (node.kind === 157 /* Decorator */) {
- argCount = getDecoratorArgumentCount(node, signature);
+ }
+ function checkThisExpression(node) {
+ // Stop at the first arrow function so that we can
+ // tell whether 'this' needs to be captured.
+ var container = ts.getThisContainer(node, /* includeArrowFunctions */ true);
+ var capturedByArrowFunction = false;
+ if (container.kind === 166 /* Constructor */) {
+ checkThisBeforeSuper(node, container, ts.Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class);
}
- else if (ts.isJsxOpeningLikeElement(node)) {
- callIsIncomplete = node.attributes.end === node.end;
- if (callIsIncomplete) {
- return true;
- }
- argCount = effectiveMinimumArguments === 0 ? args.length : 1;
- effectiveParameterCount = args.length === 0 ? effectiveParameterCount : 1; // class may have argumentless ctor functions - still resolve ctor and compare vs props member type
- effectiveMinimumArguments = Math.min(effectiveMinimumArguments, 1); // sfc may specify context argument - handled by framework and not typechecked
+ // Now skip arrow functions to get the "real" owner of 'this'.
+ if (container.kind === 209 /* ArrowFunction */) {
+ container = ts.getThisContainer(container, /* includeArrowFunctions */ false);
+ capturedByArrowFunction = true;
}
- else {
- if (!node.arguments) {
- // This only happens when we have something of the form: 'new C'
- ts.Debug.assert(node.kind === 197 /* NewExpression */);
- return getMinArgumentCount(signature) === 0;
+ switch (container.kind) {
+ case 256 /* ModuleDeclaration */:
+ error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_module_or_namespace_body);
+ // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
+ break;
+ case 255 /* EnumDeclaration */:
+ error(node, ts.Diagnostics.this_cannot_be_referenced_in_current_location);
+ // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
+ break;
+ case 166 /* Constructor */:
+ if (isInConstructorArgumentInitializer(node, container)) {
+ error(node, ts.Diagnostics.this_cannot_be_referenced_in_constructor_arguments);
+ // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
+ }
+ break;
+ case 163 /* PropertyDeclaration */:
+ case 162 /* PropertySignature */:
+ if (ts.hasSyntacticModifier(container, 32 /* Static */) && !(compilerOptions.target === 99 /* ESNext */ && compilerOptions.useDefineForClassFields)) {
+ error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_static_property_initializer);
+ // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
+ }
+ break;
+ case 158 /* ComputedPropertyName */:
+ error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_computed_property_name);
+ break;
+ }
+ // When targeting es6, mark that we'll need to capture `this` in its lexically bound scope.
+ if (capturedByArrowFunction && languageVersion < 2 /* ES2015 */) {
+ captureLexicalThis(node, container);
+ }
+ var type = tryGetThisTypeAt(node, /*includeGlobalThis*/ true, container);
+ if (noImplicitThis) {
+ var globalThisType_1 = getTypeOfSymbol(globalThisSymbol);
+ if (type === globalThisType_1 && capturedByArrowFunction) {
+ error(node, ts.Diagnostics.The_containing_arrow_function_captures_the_global_value_of_this);
}
- argCount = signatureHelpTrailingComma ? args.length + 1 : args.length;
- // If we are missing the close parenthesis, the call is incomplete.
- callIsIncomplete = node.arguments.end === node.end;
- // If a spread argument is present, check that it corresponds to a rest parameter or at least that it's in the valid range.
- var spreadArgIndex = getSpreadArgumentIndex(args);
- if (spreadArgIndex >= 0) {
- return spreadArgIndex >= getMinArgumentCount(signature) && (hasEffectiveRestParameter(signature) || spreadArgIndex < getParameterCount(signature));
+ else if (!type) {
+ // With noImplicitThis, functions may not reference 'this' if it has type 'any'
+ var diag = error(node, ts.Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation);
+ if (!ts.isSourceFile(container)) {
+ var outsideThis = tryGetThisTypeAt(container);
+ if (outsideThis && outsideThis !== globalThisType_1) {
+ ts.addRelatedInfo(diag, ts.createDiagnosticForNode(container, ts.Diagnostics.An_outer_value_of_this_is_shadowed_by_this_container));
+ }
+ }
}
}
- // Too many arguments implies incorrect arity.
- if (!hasEffectiveRestParameter(signature) && argCount > effectiveParameterCount) {
- return false;
+ return type || anyType;
+ }
+ function tryGetThisTypeAt(node, includeGlobalThis, container) {
+ if (includeGlobalThis === void 0) { includeGlobalThis = true; }
+ if (container === void 0) { container = ts.getThisContainer(node, /*includeArrowFunctions*/ false); }
+ var isInJS = ts.isInJSFile(node);
+ if (ts.isFunctionLike(container) &&
+ (!isInParameterInitializerBeforeContainingFunction(node) || ts.getThisParameter(container))) {
+ var thisType = getThisTypeOfDeclaration(container) || isInJS && getTypeForThisExpressionFromJSDoc(container);
+ // Note: a parameter initializer should refer to class-this unless function-this is explicitly annotated.
+ // If this is a function in a JS file, it might be a class method.
+ if (!thisType) {
+ var className = getClassNameFromPrototypeMethod(container);
+ if (isInJS && className) {
+ var classSymbol = checkExpression(className).symbol;
+ if (classSymbol && classSymbol.members && (classSymbol.flags & 16 /* Function */)) {
+ thisType = getDeclaredTypeOfSymbol(classSymbol).thisType;
+ }
+ }
+ else if (isJSConstructor(container)) {
+ thisType = getDeclaredTypeOfSymbol(getMergedSymbol(container.symbol)).thisType;
+ }
+ thisType || (thisType = getContextualThisParameterType(container));
+ }
+ if (thisType) {
+ return getFlowTypeOfReference(node, thisType);
+ }
}
- // If the call is incomplete, we should skip the lower bound check.
- // JSX signatures can have extra parameters provided by the library which we don't check
- if (callIsIncomplete || argCount >= effectiveMinimumArguments) {
- return true;
+ if (ts.isClassLike(container.parent)) {
+ var symbol = getSymbolOfNode(container.parent);
+ var type = ts.hasSyntacticModifier(container, 32 /* Static */) ? getTypeOfSymbol(symbol) : getDeclaredTypeOfSymbol(symbol).thisType;
+ return getFlowTypeOfReference(node, type);
}
- for (var i = argCount; i < effectiveMinimumArguments; i++) {
- var type = getTypeAtPosition(signature, i);
- if (filterType(type, acceptsVoid).flags & 131072 /* Never */) {
- return false;
+ if (ts.isSourceFile(container)) {
+ // look up in the source file's locals or exports
+ if (container.commonJsModuleIndicator) {
+ var fileSymbol = getSymbolOfNode(container);
+ return fileSymbol && getTypeOfSymbol(fileSymbol);
+ }
+ else if (container.externalModuleIndicator) {
+ // TODO: Maybe issue a better error than 'object is possibly undefined'
+ return undefinedType;
+ }
+ else if (includeGlobalThis) {
+ return getTypeOfSymbol(globalThisSymbol);
}
}
- return true;
- }
- function hasCorrectTypeArgumentArity(signature, typeArguments) {
- // If the user supplied type arguments, but the number of type arguments does not match
- // the declared number of type parameters, the call has an incorrect arity.
- var numTypeParameters = ts.length(signature.typeParameters);
- var minTypeArgumentCount = getMinTypeArgumentCount(signature.typeParameters);
- return !ts.some(typeArguments) ||
- (typeArguments.length >= minTypeArgumentCount && typeArguments.length <= numTypeParameters);
- }
- // If type has a single call signature and no other members, return that signature. Otherwise, return undefined.
- function getSingleCallSignature(type) {
- return getSingleSignature(type, 0 /* Call */, /*allowMembers*/ false);
- }
- function getSingleCallOrConstructSignature(type) {
- return getSingleSignature(type, 0 /* Call */, /*allowMembers*/ false) ||
- getSingleSignature(type, 1 /* Construct */, /*allowMembers*/ false);
}
- function getSingleSignature(type, kind, allowMembers) {
- if (type.flags & 524288 /* Object */) {
- var resolved = resolveStructuredTypeMembers(type);
- if (allowMembers || resolved.properties.length === 0 && !resolved.stringIndexInfo && !resolved.numberIndexInfo) {
- if (kind === 0 /* Call */ && resolved.callSignatures.length === 1 && resolved.constructSignatures.length === 0) {
- return resolved.callSignatures[0];
- }
- if (kind === 1 /* Construct */ && resolved.constructSignatures.length === 1 && resolved.callSignatures.length === 0) {
- return resolved.constructSignatures[0];
- }
+ function getExplicitThisType(node) {
+ var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false);
+ if (ts.isFunctionLike(container)) {
+ var signature = getSignatureFromDeclaration(container);
+ if (signature.thisParameter) {
+ return getExplicitTypeOfSymbol(signature.thisParameter);
}
}
- return undefined;
- }
- // Instantiate a generic signature in the context of a non-generic signature (section 3.8.5 in TypeScript spec)
- function instantiateSignatureInContextOf(signature, contextualSignature, inferenceContext, compareTypes) {
- var context = createInferenceContext(signature.typeParameters, signature, 0 /* None */, compareTypes);
- // We clone the inferenceContext to avoid fixing. For example, when the source signature is (x: T) => T[] and
- // the contextual signature is (...args: A) => B, we want to infer the element type of A's constraint (say 'any')
- // for T but leave it possible to later infer '[any]' back to A.
- var restType = getEffectiveRestType(contextualSignature);
- var mapper = inferenceContext && (restType && restType.flags & 262144 /* TypeParameter */ ? inferenceContext.nonFixingMapper : inferenceContext.mapper);
- var sourceSignature = mapper ? instantiateSignature(contextualSignature, mapper) : contextualSignature;
- applyToParameterTypes(sourceSignature, signature, function (source, target) {
- // Type parameters from outer context referenced by source type are fixed by instantiation of the source type
- inferTypes(context.inferences, source, target);
- });
- if (!inferenceContext) {
- applyToReturnTypes(contextualSignature, signature, function (source, target) {
- inferTypes(context.inferences, source, target, 32 /* ReturnType */);
- });
+ if (ts.isClassLike(container.parent)) {
+ var symbol = getSymbolOfNode(container.parent);
+ return ts.hasSyntacticModifier(container, 32 /* Static */) ? getTypeOfSymbol(symbol) : getDeclaredTypeOfSymbol(symbol).thisType;
}
- return getSignatureInstantiation(signature, getInferredTypes(context), ts.isInJSFile(contextualSignature.declaration));
- }
- function inferJsxTypeArguments(node, signature, checkMode, context) {
- var paramType = getEffectiveFirstArgumentForJsxSignature(signature, node);
- var checkAttrType = checkExpressionWithContextualType(node.attributes, paramType, context, checkMode);
- inferTypes(context.inferences, checkAttrType, paramType);
- return getInferredTypes(context);
}
- function inferTypeArguments(node, signature, args, checkMode, context) {
- if (ts.isJsxOpeningLikeElement(node)) {
- return inferJsxTypeArguments(node, signature, checkMode, context);
+ function getClassNameFromPrototypeMethod(container) {
+ // Check if it's the RHS of a x.prototype.y = function [name]() { .... }
+ if (container.kind === 208 /* FunctionExpression */ &&
+ ts.isBinaryExpression(container.parent) &&
+ ts.getAssignmentDeclarationKind(container.parent) === 3 /* PrototypeProperty */) {
+ // Get the 'x' of 'x.prototype.y = container'
+ return container.parent // x.prototype.y = container
+ .left // x.prototype.y
+ .expression // x.prototype
+ .expression; // x
}
- // If a contextual type is available, infer from that type to the return type of the call expression. For
- // example, given a 'function wrap(cb: (x: T) => U): (x: T) => U' and a call expression
- // 'let f: (x: string) => number = wrap(s => s.length)', we infer from the declared type of 'f' to the
- // return type of 'wrap'.
- if (node.kind !== 157 /* Decorator */) {
- var contextualType = getContextualType(node);
- if (contextualType) {
- // We clone the inference context to avoid disturbing a resolution in progress for an
- // outer call expression. Effectively we just want a snapshot of whatever has been
- // inferred for any outer call expression so far.
- var outerContext = getInferenceContext(node);
- var outerMapper = getMapperFromContext(cloneInferenceContext(outerContext, 1 /* NoDefault */));
- var instantiatedType = instantiateType(contextualType, outerMapper);
- // If the contextual type is a generic function type with a single call signature, we
- // instantiate the type with its own type parameters and type arguments. This ensures that
- // the type parameters are not erased to type any during type inference such that they can
- // be inferred as actual types from the contextual type. For example:
- // declare function arrayMap(f: (x: T) => U): (a: T[]) => U[];
- // const boxElements: (a: A[]) => { value: A }[] = arrayMap(value => ({ value }));
- // Above, the type of the 'value' parameter is inferred to be 'A'.
- var contextualSignature = getSingleCallSignature(instantiatedType);
- var inferenceSourceType = contextualSignature && contextualSignature.typeParameters ?
- getOrCreateTypeFromSignature(getSignatureInstantiationWithoutFillingInTypeArguments(contextualSignature, contextualSignature.typeParameters)) :
- instantiatedType;
- var inferenceTargetType = getReturnTypeOfSignature(signature);
- // Inferences made from return types have lower priority than all other inferences.
- inferTypes(context.inferences, inferenceSourceType, inferenceTargetType, 32 /* ReturnType */);
- // Create a type mapper for instantiating generic contextual types using the inferences made
- // from the return type. We need a separate inference pass here because (a) instantiation of
- // the source type uses the outer context's return mapper (which excludes inferences made from
- // outer arguments), and (b) we don't want any further inferences going into this context.
- var returnContext = createInferenceContext(signature.typeParameters, signature, context.flags);
- var returnSourceType = instantiateType(contextualType, outerContext && outerContext.returnMapper);
- inferTypes(returnContext.inferences, returnSourceType, inferenceTargetType);
- context.returnMapper = ts.some(returnContext.inferences, hasInferenceCandidates) ? getMapperFromContext(cloneInferredPartOfContext(returnContext)) : undefined;
- }
+ // x.prototype = { method() { } }
+ else if (container.kind === 165 /* MethodDeclaration */ &&
+ container.parent.kind === 200 /* ObjectLiteralExpression */ &&
+ ts.isBinaryExpression(container.parent.parent) &&
+ ts.getAssignmentDeclarationKind(container.parent.parent) === 6 /* Prototype */) {
+ return container.parent.parent.left.expression;
}
- var thisType = getThisTypeOfSignature(signature);
- if (thisType) {
- var thisArgumentNode = getThisArgumentOfCall(node);
- var thisArgumentType = thisArgumentNode ? checkExpression(thisArgumentNode) : voidType;
- inferTypes(context.inferences, thisArgumentType, thisType);
+ // x.prototype = { method: function() { } }
+ else if (container.kind === 208 /* FunctionExpression */ &&
+ container.parent.kind === 288 /* PropertyAssignment */ &&
+ container.parent.parent.kind === 200 /* ObjectLiteralExpression */ &&
+ ts.isBinaryExpression(container.parent.parent.parent) &&
+ ts.getAssignmentDeclarationKind(container.parent.parent.parent) === 6 /* Prototype */) {
+ return container.parent.parent.parent.left.expression;
}
- var restType = getNonArrayRestType(signature);
- var argCount = restType ? Math.min(getParameterCount(signature) - 1, args.length) : args.length;
- for (var i = 0; i < argCount; i++) {
- var arg = args[i];
- if (arg.kind !== 215 /* OmittedExpression */) {
- var paramType = getTypeAtPosition(signature, i);
- var argType = checkExpressionWithContextualType(arg, paramType, context, checkMode);
- inferTypes(context.inferences, argType, paramType);
+ // Object.defineProperty(x, "method", { value: function() { } });
+ // Object.defineProperty(x, "method", { set: (x: () => void) => void });
+ // Object.defineProperty(x, "method", { get: () => function() { }) });
+ else if (container.kind === 208 /* FunctionExpression */ &&
+ ts.isPropertyAssignment(container.parent) &&
+ ts.isIdentifier(container.parent.name) &&
+ (container.parent.name.escapedText === "value" || container.parent.name.escapedText === "get" || container.parent.name.escapedText === "set") &&
+ ts.isObjectLiteralExpression(container.parent.parent) &&
+ ts.isCallExpression(container.parent.parent.parent) &&
+ container.parent.parent.parent.arguments[2] === container.parent.parent &&
+ ts.getAssignmentDeclarationKind(container.parent.parent.parent) === 9 /* ObjectDefinePrototypeProperty */) {
+ return container.parent.parent.parent.arguments[0].expression;
+ }
+ // Object.defineProperty(x, "method", { value() { } });
+ // Object.defineProperty(x, "method", { set(x: () => void) {} });
+ // Object.defineProperty(x, "method", { get() { return () => {} } });
+ else if (ts.isMethodDeclaration(container) &&
+ ts.isIdentifier(container.name) &&
+ (container.name.escapedText === "value" || container.name.escapedText === "get" || container.name.escapedText === "set") &&
+ ts.isObjectLiteralExpression(container.parent) &&
+ ts.isCallExpression(container.parent.parent) &&
+ container.parent.parent.arguments[2] === container.parent &&
+ ts.getAssignmentDeclarationKind(container.parent.parent) === 9 /* ObjectDefinePrototypeProperty */) {
+ return container.parent.parent.arguments[0].expression;
+ }
+ }
+ function getTypeForThisExpressionFromJSDoc(node) {
+ var jsdocType = ts.getJSDocType(node);
+ if (jsdocType && jsdocType.kind === 308 /* JSDocFunctionType */) {
+ var jsDocFunctionType = jsdocType;
+ if (jsDocFunctionType.parameters.length > 0 &&
+ jsDocFunctionType.parameters[0].name &&
+ jsDocFunctionType.parameters[0].name.escapedText === "this" /* This */) {
+ return getTypeFromTypeNode(jsDocFunctionType.parameters[0].type);
}
}
- if (restType) {
- var spreadType = getSpreadArgumentType(args, argCount, args.length, restType, context);
- inferTypes(context.inferences, spreadType, restType);
+ var thisTag = ts.getJSDocThisTag(node);
+ if (thisTag && thisTag.typeExpression) {
+ return getTypeFromTypeNode(thisTag.typeExpression);
}
- return getInferredTypes(context);
}
- function getArrayifiedType(type) {
- return type.flags & 1048576 /* Union */ ? mapType(type, getArrayifiedType) :
- type.flags & (1 /* Any */ | 63176704 /* Instantiable */) || isMutableArrayOrTuple(type) ? type :
- isTupleType(type) ? createTupleType(getTypeArguments(type), type.target.minLength, type.target.hasRestElement, /*readonly*/ false, type.target.associatedNames) :
- createArrayType(getIndexedAccessType(type, numberType));
+ function isInConstructorArgumentInitializer(node, constructorDecl) {
+ return !!ts.findAncestor(node, function (n) { return ts.isFunctionLikeDeclaration(n) ? "quit" : n.kind === 160 /* Parameter */ && n.parent === constructorDecl; });
}
- function getSpreadArgumentType(args, index, argCount, restType, context) {
- if (index >= argCount - 1) {
- var arg = args[argCount - 1];
- if (isSpreadArgument(arg)) {
- // We are inferring from a spread expression in the last argument position, i.e. both the parameter
- // and the argument are ...x forms.
- return arg.kind === 220 /* SyntheticExpression */ ?
- createArrayType(arg.type) :
- getArrayifiedType(checkExpressionWithContextualType(arg.expression, restType, context, 0 /* Normal */));
+ function checkSuperExpression(node) {
+ var isCallExpression = node.parent.kind === 203 /* CallExpression */ && node.parent.expression === node;
+ var immediateContainer = ts.getSuperContainer(node, /*stopOnFunctions*/ true);
+ var container = immediateContainer;
+ var needToCaptureLexicalThis = false;
+ // adjust the container reference in case if super is used inside arrow functions with arbitrarily deep nesting
+ if (!isCallExpression) {
+ while (container && container.kind === 209 /* ArrowFunction */) {
+ container = ts.getSuperContainer(container, /*stopOnFunctions*/ true);
+ needToCaptureLexicalThis = languageVersion < 2 /* ES2015 */;
}
}
- var types = [];
- var spreadIndex = -1;
- for (var i = index; i < argCount; i++) {
- var contextualType = getIndexedAccessType(restType, getLiteralType(i - index));
- var argType = checkExpressionWithContextualType(args[i], contextualType, context, 0 /* Normal */);
- if (spreadIndex < 0 && isSpreadArgument(args[i])) {
- spreadIndex = i - index;
+ var canUseSuperExpression = isLegalUsageOfSuperExpression(container);
+ var nodeCheckFlag = 0;
+ if (!canUseSuperExpression) {
+ // issue more specific error if super is used in computed property name
+ // class A { foo() { return "1" }}
+ // class B {
+ // [super.foo()]() {}
+ // }
+ var current = ts.findAncestor(node, function (n) { return n === container ? "quit" : n.kind === 158 /* ComputedPropertyName */; });
+ if (current && current.kind === 158 /* ComputedPropertyName */) {
+ error(node, ts.Diagnostics.super_cannot_be_referenced_in_a_computed_property_name);
}
- var hasPrimitiveContextualType = maybeTypeOfKind(contextualType, 131068 /* Primitive */ | 4194304 /* Index */);
- types.push(hasPrimitiveContextualType ? getRegularTypeOfLiteralType(argType) : getWidenedLiteralType(argType));
- }
- return spreadIndex < 0 ?
- createTupleType(types) :
- createTupleType(ts.append(types.slice(0, spreadIndex), getUnionType(types.slice(spreadIndex))), spreadIndex, /*hasRestElement*/ true);
- }
- function checkTypeArguments(signature, typeArgumentNodes, reportErrors, headMessage) {
- var isJavascript = ts.isInJSFile(signature.declaration);
- var typeParameters = signature.typeParameters;
- var typeArgumentTypes = fillMissingTypeArguments(ts.map(typeArgumentNodes, getTypeFromTypeNode), typeParameters, getMinTypeArgumentCount(typeParameters), isJavascript);
- var mapper;
- for (var i = 0; i < typeArgumentNodes.length; i++) {
- ts.Debug.assert(typeParameters[i] !== undefined, "Should not call checkTypeArguments with too many type arguments");
- var constraint = getConstraintOfTypeParameter(typeParameters[i]);
- if (constraint) {
- var errorInfo = reportErrors && headMessage ? (function () { return ts.chainDiagnosticMessages(/*details*/ undefined, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1); }) : undefined;
- var typeArgumentHeadMessage = headMessage || ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1;
- if (!mapper) {
- mapper = createTypeMapper(typeParameters, typeArgumentTypes);
- }
- var typeArgument = typeArgumentTypes[i];
- if (!checkTypeAssignableTo(typeArgument, getTypeWithThisArgument(instantiateType(constraint, mapper), typeArgument), reportErrors ? typeArgumentNodes[i] : undefined, typeArgumentHeadMessage, errorInfo)) {
- return undefined;
- }
+ else if (isCallExpression) {
+ error(node, ts.Diagnostics.Super_calls_are_not_permitted_outside_constructors_or_in_nested_functions_inside_constructors);
+ }
+ else if (!container || !container.parent || !(ts.isClassLike(container.parent) || container.parent.kind === 200 /* ObjectLiteralExpression */)) {
+ error(node, ts.Diagnostics.super_can_only_be_referenced_in_members_of_derived_classes_or_object_literal_expressions);
+ }
+ else {
+ error(node, ts.Diagnostics.super_property_access_is_permitted_only_in_a_constructor_member_function_or_member_accessor_of_a_derived_class);
}
+ return errorType;
}
- return typeArgumentTypes;
- }
- function getJsxReferenceKind(node) {
- if (isJsxIntrinsicIdentifier(node.tagName)) {
- return 2 /* Mixed */;
+ if (!isCallExpression && immediateContainer.kind === 166 /* Constructor */) {
+ checkThisBeforeSuper(node, container, ts.Diagnostics.super_must_be_called_before_accessing_a_property_of_super_in_the_constructor_of_a_derived_class);
}
- var tagType = getApparentType(checkExpression(node.tagName));
- if (ts.length(getSignaturesOfType(tagType, 1 /* Construct */))) {
- return 0 /* Component */;
+ if (ts.hasSyntacticModifier(container, 32 /* Static */) || isCallExpression) {
+ nodeCheckFlag = 512 /* SuperStatic */;
}
- if (ts.length(getSignaturesOfType(tagType, 0 /* Call */))) {
- return 1 /* Function */;
+ else {
+ nodeCheckFlag = 256 /* SuperInstance */;
}
- return 2 /* Mixed */;
- }
- /**
- * Check if the given signature can possibly be a signature called by the JSX opening-like element.
- * @param node a JSX opening-like element we are trying to figure its call signature
- * @param signature a candidate signature we are trying whether it is a call signature
- * @param relation a relationship to check parameter and argument type
- */
- function checkApplicableSignatureForJsxOpeningLikeElement(node, signature, relation, checkMode, reportErrors, containingMessageChain, errorOutputContainer) {
- // Stateless function components can have maximum of three arguments: "props", "context", and "updater".
- // However "context" and "updater" are implicit and can't be specify by users. Only the first parameter, props,
- // can be specified by users through attributes property.
- var paramType = getEffectiveFirstArgumentForJsxSignature(signature, node);
- var attributesType = checkExpressionWithContextualType(node.attributes, paramType, /*inferenceContext*/ undefined, checkMode);
- return checkTagNameDoesNotExpectTooManyArguments() && checkTypeRelatedToAndOptionallyElaborate(attributesType, paramType, relation, reportErrors ? node.tagName : undefined, node.attributes,
- /*headMessage*/ undefined, containingMessageChain, errorOutputContainer);
- function checkTagNameDoesNotExpectTooManyArguments() {
- var _a;
- var tagType = ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node) && !isJsxIntrinsicIdentifier(node.tagName) ? checkExpression(node.tagName) : undefined;
- if (!tagType) {
- return true;
+ getNodeLinks(node).flags |= nodeCheckFlag;
+ // Due to how we emit async functions, we need to specialize the emit for an async method that contains a `super` reference.
+ // This is due to the fact that we emit the body of an async function inside of a generator function. As generator
+ // functions cannot reference `super`, we emit a helper inside of the method body, but outside of the generator. This helper
+ // uses an arrow function, which is permitted to reference `super`.
+ //
+ // There are two primary ways we can access `super` from within an async method. The first is getting the value of a property
+ // or indexed access on super, either as part of a right-hand-side expression or call expression. The second is when setting the value
+ // of a property or indexed access, either as part of an assignment expression or destructuring assignment.
+ //
+ // The simplest case is reading a value, in which case we will emit something like the following:
+ //
+ // // ts
+ // ...
+ // async asyncMethod() {
+ // let x = await super.asyncMethod();
+ // return x;
+ // }
+ // ...
+ //
+ // // js
+ // ...
+ // asyncMethod() {
+ // const _super = Object.create(null, {
+ // asyncMethod: { get: () => super.asyncMethod },
+ // });
+ // return __awaiter(this, arguments, Promise, function *() {
+ // let x = yield _super.asyncMethod.call(this);
+ // return x;
+ // });
+ // }
+ // ...
+ //
+ // The more complex case is when we wish to assign a value, especially as part of a destructuring assignment. As both cases
+ // are legal in ES6, but also likely less frequent, we only emit setters if there is an assignment:
+ //
+ // // ts
+ // ...
+ // async asyncMethod(ar: Promise) {
+ // [super.a, super.b] = await ar;
+ // }
+ // ...
+ //
+ // // js
+ // ...
+ // asyncMethod(ar) {
+ // const _super = Object.create(null, {
+ // a: { get: () => super.a, set: (v) => super.a = v },
+ // b: { get: () => super.b, set: (v) => super.b = v }
+ // };
+ // return __awaiter(this, arguments, Promise, function *() {
+ // [_super.a, _super.b] = yield ar;
+ // });
+ // }
+ // ...
+ //
+ // Creating an object that has getter and setters instead of just an accessor function is required for destructuring assignments
+ // as a call expression cannot be used as the target of a destructuring assignment while a property access can.
+ //
+ // For element access expressions (`super[x]`), we emit a generic helper that forwards the element access in both situations.
+ if (container.kind === 165 /* MethodDeclaration */ && ts.hasSyntacticModifier(container, 256 /* Async */)) {
+ if (ts.isSuperProperty(node.parent) && ts.isAssignmentTarget(node.parent)) {
+ getNodeLinks(container).flags |= 4096 /* AsyncMethodWithSuperBinding */;
}
- var tagCallSignatures = getSignaturesOfType(tagType, 0 /* Call */);
- if (!ts.length(tagCallSignatures)) {
- return true;
+ else {
+ getNodeLinks(container).flags |= 2048 /* AsyncMethodWithSuper */;
}
- var factory = getJsxFactoryEntity(node);
- if (!factory) {
- return true;
+ }
+ if (needToCaptureLexicalThis) {
+ // call expressions are allowed only in constructors so they should always capture correct 'this'
+ // super property access expressions can also appear in arrow functions -
+ // in this case they should also use correct lexical this
+ captureLexicalThis(node.parent, container);
+ }
+ if (container.parent.kind === 200 /* ObjectLiteralExpression */) {
+ if (languageVersion < 2 /* ES2015 */) {
+ error(node, ts.Diagnostics.super_is_only_allowed_in_members_of_object_literal_expressions_when_option_target_is_ES2015_or_higher);
+ return errorType;
}
- var factorySymbol = resolveEntityName(factory, 111551 /* Value */, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, node);
- if (!factorySymbol) {
- return true;
+ else {
+ // for object literal assume that type of 'super' is 'any'
+ return anyType;
}
- var factoryType = getTypeOfSymbol(factorySymbol);
- var callSignatures = getSignaturesOfType(factoryType, 0 /* Call */);
- if (!ts.length(callSignatures)) {
- return true;
+ }
+ // at this point the only legal case for parent is ClassLikeDeclaration
+ var classLikeDeclaration = container.parent;
+ if (!ts.getClassExtendsHeritageElement(classLikeDeclaration)) {
+ error(node, ts.Diagnostics.super_can_only_be_referenced_in_a_derived_class);
+ return errorType;
+ }
+ var classType = getDeclaredTypeOfSymbol(getSymbolOfNode(classLikeDeclaration));
+ var baseClassType = classType && getBaseTypes(classType)[0];
+ if (!baseClassType) {
+ return errorType;
+ }
+ if (container.kind === 166 /* Constructor */ && isInConstructorArgumentInitializer(node, container)) {
+ // issue custom error message for super property access in constructor arguments (to be aligned with old compiler)
+ error(node, ts.Diagnostics.super_cannot_be_referenced_in_constructor_arguments);
+ return errorType;
+ }
+ return nodeCheckFlag === 512 /* SuperStatic */
+ ? getBaseConstructorTypeOfClass(classType)
+ : getTypeWithThisArgument(baseClassType, classType.thisType);
+ function isLegalUsageOfSuperExpression(container) {
+ if (!container) {
+ return false;
}
- var hasFirstParamSignatures = false;
- var maxParamCount = 0;
- // Check that _some_ first parameter expects a FC-like thing, and that some overload of the SFC expects an acceptable number of arguments
- for (var _i = 0, callSignatures_1 = callSignatures; _i < callSignatures_1.length; _i++) {
- var sig = callSignatures_1[_i];
- var firstparam = getTypeAtPosition(sig, 0);
- var signaturesOfParam = getSignaturesOfType(firstparam, 0 /* Call */);
- if (!ts.length(signaturesOfParam))
- continue;
- for (var _b = 0, signaturesOfParam_1 = signaturesOfParam; _b < signaturesOfParam_1.length; _b++) {
- var paramSig = signaturesOfParam_1[_b];
- hasFirstParamSignatures = true;
- if (hasEffectiveRestParameter(paramSig)) {
- return true; // some signature has a rest param, so function components can have an arbitrary number of arguments
+ if (isCallExpression) {
+ // TS 1.0 SPEC (April 2014): 4.8.1
+ // Super calls are only permitted in constructors of derived classes
+ return container.kind === 166 /* Constructor */;
+ }
+ else {
+ // TS 1.0 SPEC (April 2014)
+ // 'super' property access is allowed
+ // - In a constructor, instance member function, instance member accessor, or instance member variable initializer where this references a derived class instance
+ // - In a static member function or static member accessor
+ // topmost container must be something that is directly nested in the class declaration\object literal expression
+ if (ts.isClassLike(container.parent) || container.parent.kind === 200 /* ObjectLiteralExpression */) {
+ if (ts.hasSyntacticModifier(container, 32 /* Static */)) {
+ return container.kind === 165 /* MethodDeclaration */ ||
+ container.kind === 164 /* MethodSignature */ ||
+ container.kind === 167 /* GetAccessor */ ||
+ container.kind === 168 /* SetAccessor */;
}
- var paramCount = getParameterCount(paramSig);
- if (paramCount > maxParamCount) {
- maxParamCount = paramCount;
+ else {
+ return container.kind === 165 /* MethodDeclaration */ ||
+ container.kind === 164 /* MethodSignature */ ||
+ container.kind === 167 /* GetAccessor */ ||
+ container.kind === 168 /* SetAccessor */ ||
+ container.kind === 163 /* PropertyDeclaration */ ||
+ container.kind === 162 /* PropertySignature */ ||
+ container.kind === 166 /* Constructor */;
}
}
}
- if (!hasFirstParamSignatures) {
- // Not a single signature had a first parameter which expected a signature - for back compat, and
- // to guard against generic factories which won't have signatures directly, do not error
- return true;
- }
- var absoluteMinArgCount = Infinity;
- for (var _c = 0, tagCallSignatures_1 = tagCallSignatures; _c < tagCallSignatures_1.length; _c++) {
- var tagSig = tagCallSignatures_1[_c];
- var tagRequiredArgCount = getMinArgumentCount(tagSig);
- if (tagRequiredArgCount < absoluteMinArgCount) {
- absoluteMinArgCount = tagRequiredArgCount;
- }
- }
- if (absoluteMinArgCount <= maxParamCount) {
- return true; // some signature accepts the number of arguments the function component provides
- }
- if (reportErrors) {
- var diag = ts.createDiagnosticForNode(node.tagName, ts.Diagnostics.Tag_0_expects_at_least_1_arguments_but_the_JSX_factory_2_provides_at_most_3, ts.entityNameToString(node.tagName), absoluteMinArgCount, ts.entityNameToString(factory), maxParamCount);
- var tagNameDeclaration = (_a = getSymbolAtLocation(node.tagName)) === null || _a === void 0 ? void 0 : _a.valueDeclaration;
- if (tagNameDeclaration) {
- ts.addRelatedInfo(diag, ts.createDiagnosticForNode(tagNameDeclaration, ts.Diagnostics._0_is_declared_here, ts.entityNameToString(node.tagName)));
- }
- if (errorOutputContainer && errorOutputContainer.skipLogging) {
- (errorOutputContainer.errors || (errorOutputContainer.errors = [])).push(diag);
- }
- if (!errorOutputContainer.skipLogging) {
- diagnostics.add(diag);
- }
- }
return false;
}
}
- function getSignatureApplicabilityError(node, args, signature, relation, checkMode, reportErrors, containingMessageChain) {
- var errorOutputContainer = { errors: undefined, skipLogging: true };
- if (ts.isJsxOpeningLikeElement(node)) {
- if (!checkApplicableSignatureForJsxOpeningLikeElement(node, signature, relation, checkMode, reportErrors, containingMessageChain, errorOutputContainer)) {
- ts.Debug.assert(!reportErrors || !!errorOutputContainer.errors, "jsx should have errors when reporting errors");
- return errorOutputContainer.errors || ts.emptyArray;
- }
+ function getContainingObjectLiteral(func) {
+ return (func.kind === 165 /* MethodDeclaration */ ||
+ func.kind === 167 /* GetAccessor */ ||
+ func.kind === 168 /* SetAccessor */) && func.parent.kind === 200 /* ObjectLiteralExpression */ ? func.parent :
+ func.kind === 208 /* FunctionExpression */ && func.parent.kind === 288 /* PropertyAssignment */ ? func.parent.parent :
+ undefined;
+ }
+ function getThisTypeArgument(type) {
+ return ts.getObjectFlags(type) & 4 /* Reference */ && type.target === globalThisType ? getTypeArguments(type)[0] : undefined;
+ }
+ function getThisTypeFromContextualType(type) {
+ return mapType(type, function (t) {
+ return t.flags & 2097152 /* Intersection */ ? ts.forEach(t.types, getThisTypeArgument) : getThisTypeArgument(t);
+ });
+ }
+ function getContextualThisParameterType(func) {
+ if (func.kind === 209 /* ArrowFunction */) {
return undefined;
}
- var thisType = getThisTypeOfSignature(signature);
- if (thisType && thisType !== voidType && node.kind !== 197 /* NewExpression */) {
- // If the called expression is not of the form `x.f` or `x["f"]`, then sourceType = voidType
- // If the signature's 'this' type is voidType, then the check is skipped -- anything is compatible.
- // If the expression is a new expression, then the check is skipped.
- var thisArgumentNode = getThisArgumentOfCall(node);
- var thisArgumentType = void 0;
- if (thisArgumentNode) {
- thisArgumentType = checkExpression(thisArgumentNode);
- if (ts.isOptionalChainRoot(thisArgumentNode.parent)) {
- thisArgumentType = getNonNullableType(thisArgumentType);
- }
- else if (ts.isOptionalChain(thisArgumentNode.parent)) {
- thisArgumentType = removeOptionalTypeMarker(thisArgumentType);
- }
- }
- else {
- thisArgumentType = voidType;
- }
- var errorNode = reportErrors ? (thisArgumentNode || node) : undefined;
- var headMessage_1 = ts.Diagnostics.The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1;
- if (!checkTypeRelatedTo(thisArgumentType, thisType, relation, errorNode, headMessage_1, containingMessageChain, errorOutputContainer)) {
- ts.Debug.assert(!reportErrors || !!errorOutputContainer.errors, "this parameter should have errors when reporting errors");
- return errorOutputContainer.errors || ts.emptyArray;
- }
- }
- var headMessage = ts.Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1;
- var restType = getNonArrayRestType(signature);
- var argCount = restType ? Math.min(getParameterCount(signature) - 1, args.length) : args.length;
- for (var i = 0; i < argCount; i++) {
- var arg = args[i];
- if (arg.kind !== 215 /* OmittedExpression */) {
- var paramType = getTypeAtPosition(signature, i);
- var argType = checkExpressionWithContextualType(arg, paramType, /*inferenceContext*/ undefined, checkMode);
- // If one or more arguments are still excluded (as indicated by CheckMode.SkipContextSensitive),
- // we obtain the regular type of any object literal arguments because we may not have inferred complete
- // parameter types yet and therefore excess property checks may yield false positives (see #17041).
- var checkArgType = checkMode & 4 /* SkipContextSensitive */ ? getRegularTypeOfObjectLiteral(argType) : argType;
- if (!checkTypeRelatedToAndOptionallyElaborate(checkArgType, paramType, relation, reportErrors ? arg : undefined, arg, headMessage, containingMessageChain, errorOutputContainer)) {
- ts.Debug.assert(!reportErrors || !!errorOutputContainer.errors, "parameter should have errors when reporting errors");
- maybeAddMissingAwaitInfo(arg, checkArgType, paramType);
- return errorOutputContainer.errors || ts.emptyArray;
+ if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) {
+ var contextualSignature = getContextualSignature(func);
+ if (contextualSignature) {
+ var thisParameter = contextualSignature.thisParameter;
+ if (thisParameter) {
+ return getTypeOfSymbol(thisParameter);
}
}
}
- if (restType) {
- var spreadType = getSpreadArgumentType(args, argCount, args.length, restType, /*context*/ undefined);
- var errorNode = reportErrors ? argCount < args.length ? args[argCount] : node : undefined;
- if (!checkTypeRelatedTo(spreadType, restType, relation, errorNode, headMessage, /*containingMessageChain*/ undefined, errorOutputContainer)) {
- ts.Debug.assert(!reportErrors || !!errorOutputContainer.errors, "rest parameter should have errors when reporting errors");
- maybeAddMissingAwaitInfo(errorNode, spreadType, restType);
- return errorOutputContainer.errors || ts.emptyArray;
- }
- }
- return undefined;
- function maybeAddMissingAwaitInfo(errorNode, source, target) {
- if (errorNode && reportErrors && errorOutputContainer.errors && errorOutputContainer.errors.length) {
- // Bail if target is Promise-like---something else is wrong
- if (getAwaitedTypeOfPromise(target)) {
- return;
+ var inJs = ts.isInJSFile(func);
+ if (noImplicitThis || inJs) {
+ var containingLiteral = getContainingObjectLiteral(func);
+ if (containingLiteral) {
+ // We have an object literal method. Check if the containing object literal has a contextual type
+ // that includes a ThisType. If so, T is the contextual type for 'this'. We continue looking in
+ // any directly enclosing object literals.
+ var contextualType = getApparentTypeOfContextualType(containingLiteral);
+ var literal = containingLiteral;
+ var type = contextualType;
+ while (type) {
+ var thisType = getThisTypeFromContextualType(type);
+ if (thisType) {
+ return instantiateType(thisType, getMapperFromContext(getInferenceContext(containingLiteral)));
+ }
+ if (literal.parent.kind !== 288 /* PropertyAssignment */) {
+ break;
+ }
+ literal = literal.parent.parent;
+ type = getApparentTypeOfContextualType(literal);
}
- var awaitedTypeOfSource = getAwaitedTypeOfPromise(source);
- if (awaitedTypeOfSource && isTypeRelatedTo(awaitedTypeOfSource, target, relation)) {
- ts.addRelatedInfo(errorOutputContainer.errors[0], ts.createDiagnosticForNode(errorNode, ts.Diagnostics.Did_you_forget_to_use_await));
+ // There was no contextual ThisType for the containing object literal, so the contextual type
+ // for 'this' is the non-null form of the contextual type for the containing object literal or
+ // the type of the object literal itself.
+ return getWidenedType(contextualType ? getNonNullableType(contextualType) : checkExpressionCached(containingLiteral));
+ }
+ // In an assignment of the form 'obj.xxx = function(...)' or 'obj[xxx] = function(...)', the
+ // contextual type for 'this' is 'obj'.
+ var parent = ts.walkUpParenthesizedExpressions(func.parent);
+ if (parent.kind === 216 /* BinaryExpression */ && parent.operatorToken.kind === 62 /* EqualsToken */) {
+ var target = parent.left;
+ if (ts.isAccessExpression(target)) {
+ var expression = target.expression;
+ // Don't contextually type `this` as `exports` in `exports.Point = function(x, y) { this.x = x; this.y = y; }`
+ if (inJs && ts.isIdentifier(expression)) {
+ var sourceFile = ts.getSourceFileOfNode(parent);
+ if (sourceFile.commonJsModuleIndicator && getResolvedSymbol(expression) === sourceFile.symbol) {
+ return undefined;
+ }
+ }
+ return getWidenedType(checkExpressionCached(expression));
}
}
}
+ return undefined;
}
- /**
- * Returns the this argument in calls like x.f(...) and x[f](...). Undefined otherwise.
- */
- function getThisArgumentOfCall(node) {
- if (node.kind === 196 /* CallExpression */) {
- var callee = ts.skipOuterExpressions(node.expression);
- if (ts.isAccessExpression(callee)) {
- return callee.expression;
- }
+ // Return contextual type of parameter or undefined if no contextual type is available
+ function getContextuallyTypedParameterType(parameter) {
+ var func = parameter.parent;
+ if (!isContextSensitiveFunctionOrObjectLiteralMethod(func)) {
+ return undefined;
}
- }
- function createSyntheticExpression(parent, type, isSpread) {
- var result = ts.createNode(220 /* SyntheticExpression */, parent.pos, parent.end);
- result.parent = parent;
- result.type = type;
- result.isSpread = isSpread || false;
- return result;
- }
- /**
- * Returns the effective arguments for an expression that works like a function invocation.
- */
- function getEffectiveCallArguments(node) {
- if (node.kind === 198 /* TaggedTemplateExpression */) {
- var template = node.template;
- var args_3 = [createSyntheticExpression(template, getGlobalTemplateStringsArrayType())];
- if (template.kind === 211 /* TemplateExpression */) {
- ts.forEach(template.templateSpans, function (span) {
- args_3.push(span.expression);
- });
+ var iife = ts.getImmediatelyInvokedFunctionExpression(func);
+ if (iife && iife.arguments) {
+ var args = getEffectiveCallArguments(iife);
+ var indexOfParameter = func.parameters.indexOf(parameter);
+ if (parameter.dotDotDotToken) {
+ return getSpreadArgumentType(args, indexOfParameter, args.length, anyType, /*context*/ undefined, 0 /* Normal */);
}
- return args_3;
- }
- if (node.kind === 157 /* Decorator */) {
- return getEffectiveDecoratorArguments(node);
- }
- if (ts.isJsxOpeningLikeElement(node)) {
- return node.attributes.properties.length > 0 || (ts.isJsxOpeningElement(node) && node.parent.children.length > 0) ? [node.attributes] : ts.emptyArray;
+ var links = getNodeLinks(iife);
+ var cached = links.resolvedSignature;
+ links.resolvedSignature = anySignature;
+ var type = indexOfParameter < args.length ?
+ getWidenedLiteralType(checkExpression(args[indexOfParameter])) :
+ parameter.initializer ? undefined : undefinedWideningType;
+ links.resolvedSignature = cached;
+ return type;
}
- var args = node.arguments || ts.emptyArray;
- var length = args.length;
- if (length && isSpreadArgument(args[length - 1]) && getSpreadArgumentIndex(args) === length - 1) {
- // We have a spread argument in the last position and no other spread arguments. If the type
- // of the argument is a tuple type, spread the tuple elements into the argument list. We can
- // call checkExpressionCached because spread expressions never have a contextual type.
- var spreadArgument_1 = args[length - 1];
- var type = flowLoopCount ? checkExpression(spreadArgument_1.expression) : checkExpressionCached(spreadArgument_1.expression);
- if (isTupleType(type)) {
- var typeArguments = getTypeArguments(type);
- var restIndex_2 = type.target.hasRestElement ? typeArguments.length - 1 : -1;
- var syntheticArgs = ts.map(typeArguments, function (t, i) { return createSyntheticExpression(spreadArgument_1, t, /*isSpread*/ i === restIndex_2); });
- return ts.concatenate(args.slice(0, length - 1), syntheticArgs);
- }
+ var contextualSignature = getContextualSignature(func);
+ if (contextualSignature) {
+ var index = func.parameters.indexOf(parameter) - (ts.getThisParameter(func) ? 1 : 0);
+ return parameter.dotDotDotToken && ts.lastOrUndefined(func.parameters) === parameter ?
+ getRestTypeAtPosition(contextualSignature, index) :
+ tryGetTypeAtPosition(contextualSignature, index);
}
- return args;
}
- /**
- * Returns the synthetic argument list for a decorator invocation.
- */
- function getEffectiveDecoratorArguments(node) {
- var parent = node.parent;
- var expr = node.expression;
- switch (parent.kind) {
- case 245 /* ClassDeclaration */:
- case 214 /* ClassExpression */:
- // For a class decorator, the `target` is the type of the class (e.g. the
- // "static" or "constructor" side of the class).
- return [
- createSyntheticExpression(expr, getTypeOfSymbol(getSymbolOfNode(parent)))
- ];
- case 156 /* Parameter */:
- // A parameter declaration decorator will have three arguments (see
- // `ParameterDecorator` in core.d.ts).
- var func = parent.parent;
- return [
- createSyntheticExpression(expr, parent.parent.kind === 162 /* Constructor */ ? getTypeOfSymbol(getSymbolOfNode(func)) : errorType),
- createSyntheticExpression(expr, anyType),
- createSyntheticExpression(expr, numberType)
- ];
- case 159 /* PropertyDeclaration */:
- case 161 /* MethodDeclaration */:
- case 163 /* GetAccessor */:
- case 164 /* SetAccessor */:
- // A method or accessor declaration decorator will have two or three arguments (see
- // `PropertyDecorator` and `MethodDecorator` in core.d.ts). If we are emitting decorators
- // for ES3, we will only pass two arguments.
- var hasPropDesc = parent.kind !== 159 /* PropertyDeclaration */ && languageVersion !== 0 /* ES3 */;
- return [
- createSyntheticExpression(expr, getParentTypeOfClassElement(parent)),
- createSyntheticExpression(expr, getClassElementPropertyKeyType(parent)),
- createSyntheticExpression(expr, hasPropDesc ? createTypedPropertyDescriptorType(getTypeOfNode(parent)) : anyType)
- ];
+ function getContextualTypeForVariableLikeDeclaration(declaration) {
+ var typeNode = ts.getEffectiveTypeAnnotationNode(declaration);
+ if (typeNode) {
+ return getTypeFromTypeNode(typeNode);
}
- return ts.Debug.fail();
- }
- /**
- * Returns the argument count for a decorator node that works like a function invocation.
- */
- function getDecoratorArgumentCount(node, signature) {
- switch (node.parent.kind) {
- case 245 /* ClassDeclaration */:
- case 214 /* ClassExpression */:
- return 1;
- case 159 /* PropertyDeclaration */:
- return 2;
- case 161 /* MethodDeclaration */:
- case 163 /* GetAccessor */:
- case 164 /* SetAccessor */:
- // For ES3 or decorators with only two parameters we supply only two arguments
- return languageVersion === 0 /* ES3 */ || signature.parameters.length <= 2 ? 2 : 3;
- case 156 /* Parameter */:
- return 3;
- default:
- return ts.Debug.fail();
+ switch (declaration.kind) {
+ case 160 /* Parameter */:
+ return getContextuallyTypedParameterType(declaration);
+ case 198 /* BindingElement */:
+ return getContextualTypeForBindingElement(declaration);
+ case 163 /* PropertyDeclaration */:
+ if (ts.hasSyntacticModifier(declaration, 32 /* Static */)) {
+ return getContextualTypeForStaticPropertyDeclaration(declaration);
+ }
+ // By default, do nothing and return undefined - only the above cases have context implied by a parent
}
}
- function getDiagnosticSpanForCallNode(node, doNotIncludeArguments) {
- var start;
- var length;
- var sourceFile = ts.getSourceFileOfNode(node);
- if (ts.isPropertyAccessExpression(node.expression)) {
- var nameSpan = ts.getErrorSpanForNode(sourceFile, node.expression.name);
- start = nameSpan.start;
- length = doNotIncludeArguments ? nameSpan.length : node.end - start;
+ function getContextualTypeForBindingElement(declaration) {
+ var parent = declaration.parent.parent;
+ var name = declaration.propertyName || declaration.name;
+ var parentType = getContextualTypeForVariableLikeDeclaration(parent) ||
+ parent.kind !== 198 /* BindingElement */ && parent.initializer && checkDeclarationInitializer(parent);
+ if (!parentType || ts.isBindingPattern(name) || ts.isComputedNonLiteralName(name))
+ return undefined;
+ if (parent.name.kind === 197 /* ArrayBindingPattern */) {
+ var index = ts.indexOfNode(declaration.parent.elements, declaration);
+ if (index < 0)
+ return undefined;
+ return getContextualTypeForElementExpression(parentType, index);
}
- else {
- var expressionSpan = ts.getErrorSpanForNode(sourceFile, node.expression);
- start = expressionSpan.start;
- length = doNotIncludeArguments ? expressionSpan.length : node.end - start;
+ var nameType = getLiteralTypeFromPropertyName(name);
+ if (isTypeUsableAsPropertyName(nameType)) {
+ var text = getPropertyNameFromType(nameType);
+ return getTypeOfPropertyOfType(parentType, text);
}
- return { start: start, length: length, sourceFile: sourceFile };
}
- function getDiagnosticForCallNode(node, message, arg0, arg1, arg2, arg3) {
- if (ts.isCallExpression(node)) {
- var _a = getDiagnosticSpanForCallNode(node), sourceFile = _a.sourceFile, start = _a.start, length_5 = _a.length;
- return ts.createFileDiagnostic(sourceFile, start, length_5, message, arg0, arg1, arg2, arg3);
- }
- else {
- return ts.createDiagnosticForNode(node, message, arg0, arg1, arg2, arg3);
- }
+ function getContextualTypeForStaticPropertyDeclaration(declaration) {
+ var parentType = ts.isExpression(declaration.parent) && getContextualType(declaration.parent);
+ if (!parentType)
+ return undefined;
+ return getTypeOfPropertyOfContextualType(parentType, getSymbolOfNode(declaration).escapedName);
}
- function getArgumentArityError(node, signatures, args) {
- var min = Number.POSITIVE_INFINITY;
- var max = Number.NEGATIVE_INFINITY;
- var belowArgCount = Number.NEGATIVE_INFINITY;
- var aboveArgCount = Number.POSITIVE_INFINITY;
- var argCount = args.length;
- var closestSignature;
- for (var _i = 0, signatures_8 = signatures; _i < signatures_8.length; _i++) {
- var sig = signatures_8[_i];
- var minCount = getMinArgumentCount(sig);
- var maxCount = getParameterCount(sig);
- if (minCount < argCount && minCount > belowArgCount)
- belowArgCount = minCount;
- if (argCount < maxCount && maxCount < aboveArgCount)
- aboveArgCount = maxCount;
- if (minCount < min) {
- min = minCount;
- closestSignature = sig;
+ // In a variable, parameter or property declaration with a type annotation,
+ // the contextual type of an initializer expression is the type of the variable, parameter or property.
+ // Otherwise, in a parameter declaration of a contextually typed function expression,
+ // the contextual type of an initializer expression is the contextual type of the parameter.
+ // Otherwise, in a variable or parameter declaration with a binding pattern name,
+ // the contextual type of an initializer expression is the type implied by the binding pattern.
+ // Otherwise, in a binding pattern inside a variable or parameter declaration,
+ // the contextual type of an initializer expression is the type annotation of the containing declaration, if present.
+ function getContextualTypeForInitializerExpression(node, contextFlags) {
+ var declaration = node.parent;
+ if (ts.hasInitializer(declaration) && node === declaration.initializer) {
+ var result = getContextualTypeForVariableLikeDeclaration(declaration);
+ if (result) {
+ return result;
}
- max = Math.max(max, maxCount);
- }
- var hasRestParameter = ts.some(signatures, hasEffectiveRestParameter);
- var paramRange = hasRestParameter ? min :
- min < max ? min + "-" + max :
- min;
- var hasSpreadArgument = getSpreadArgumentIndex(args) > -1;
- if (argCount <= max && hasSpreadArgument) {
- argCount--;
- }
- var spanArray;
- var related;
- var error = hasRestParameter || hasSpreadArgument ? hasRestParameter && hasSpreadArgument ? ts.Diagnostics.Expected_at_least_0_arguments_but_got_1_or_more :
- hasRestParameter ? ts.Diagnostics.Expected_at_least_0_arguments_but_got_1 :
- ts.Diagnostics.Expected_0_arguments_but_got_1_or_more : ts.Diagnostics.Expected_0_arguments_but_got_1;
- if (closestSignature && getMinArgumentCount(closestSignature) > argCount && closestSignature.declaration) {
- var paramDecl = closestSignature.declaration.parameters[closestSignature.thisParameter ? argCount + 1 : argCount];
- if (paramDecl) {
- related = ts.createDiagnosticForNode(paramDecl, ts.isBindingPattern(paramDecl.name) ? ts.Diagnostics.An_argument_matching_this_binding_pattern_was_not_provided : ts.Diagnostics.An_argument_for_0_was_not_provided, !paramDecl.name ? argCount : !ts.isBindingPattern(paramDecl.name) ? ts.idText(ts.getFirstIdentifier(paramDecl.name)) : undefined);
+ if (!(contextFlags & 8 /* SkipBindingPatterns */) && ts.isBindingPattern(declaration.name)) { // This is less a contextual type and more an implied shape - in some cases, this may be undesirable
+ return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false);
}
}
- if (min < argCount && argCount < max) {
- return getDiagnosticForCallNode(node, ts.Diagnostics.No_overload_expects_0_arguments_but_overloads_do_exist_that_expect_either_1_or_2_arguments, argCount, belowArgCount, aboveArgCount);
- }
- if (!hasSpreadArgument && argCount < min) {
- var diagnostic_1 = getDiagnosticForCallNode(node, error, paramRange, argCount);
- return related ? ts.addRelatedInfo(diagnostic_1, related) : diagnostic_1;
- }
- if (hasRestParameter || hasSpreadArgument) {
- spanArray = ts.createNodeArray(args);
- if (hasSpreadArgument && argCount) {
- var nextArg = ts.elementAt(args, getSpreadArgumentIndex(args) + 1) || undefined;
- spanArray = ts.createNodeArray(args.slice(max > argCount && nextArg ? args.indexOf(nextArg) : Math.min(max, args.length - 1)));
+ return undefined;
+ }
+ function getContextualTypeForReturnExpression(node) {
+ var func = ts.getContainingFunction(node);
+ if (func) {
+ var contextualReturnType = getContextualReturnType(func);
+ if (contextualReturnType) {
+ var functionFlags = ts.getFunctionFlags(func);
+ if (functionFlags & 1 /* Generator */) { // Generator or AsyncGenerator function
+ var use = functionFlags & 2 /* Async */ ? 2 /* AsyncGeneratorReturnType */ : 1 /* GeneratorReturnType */;
+ var iterationTypes = getIterationTypesOfIterable(contextualReturnType, use, /*errorNode*/ undefined);
+ if (!iterationTypes) {
+ return undefined;
+ }
+ contextualReturnType = iterationTypes.returnType;
+ // falls through to unwrap Promise for AsyncGenerators
+ }
+ if (functionFlags & 2 /* Async */) { // Async function or AsyncGenerator function
+ var contextualAwaitedType = mapType(contextualReturnType, getAwaitedType);
+ return contextualAwaitedType && getUnionType([contextualAwaitedType, createPromiseLikeType(contextualAwaitedType)]);
+ }
+ return contextualReturnType; // Regular function or Generator function
}
}
- else {
- spanArray = ts.createNodeArray(args.slice(max));
- }
- spanArray.pos = ts.first(spanArray).pos;
- spanArray.end = ts.last(spanArray).end;
- if (spanArray.end === spanArray.pos) {
- spanArray.end++;
+ return undefined;
+ }
+ function getContextualTypeForAwaitOperand(node, contextFlags) {
+ var contextualType = getContextualType(node, contextFlags);
+ if (contextualType) {
+ var contextualAwaitedType = getAwaitedType(contextualType);
+ return contextualAwaitedType && getUnionType([contextualAwaitedType, createPromiseLikeType(contextualAwaitedType)]);
}
- var diagnostic = ts.createDiagnosticForNodeArray(ts.getSourceFileOfNode(node), spanArray, error, paramRange, argCount);
- return related ? ts.addRelatedInfo(diagnostic, related) : diagnostic;
+ return undefined;
}
- function getTypeArgumentArityError(node, signatures, typeArguments) {
- var argCount = typeArguments.length;
- // No overloads exist
- if (signatures.length === 1) {
- var sig = signatures[0];
- var min_1 = getMinTypeArgumentCount(sig.typeParameters);
- var max = ts.length(sig.typeParameters);
- return ts.createDiagnosticForNodeArray(ts.getSourceFileOfNode(node), typeArguments, ts.Diagnostics.Expected_0_type_arguments_but_got_1, min_1 < max ? min_1 + "-" + max : min_1, argCount);
+ function getContextualTypeForYieldOperand(node) {
+ var func = ts.getContainingFunction(node);
+ if (func) {
+ var functionFlags = ts.getFunctionFlags(func);
+ var contextualReturnType = getContextualReturnType(func);
+ if (contextualReturnType) {
+ return node.asteriskToken
+ ? contextualReturnType
+ : getIterationTypeOfGeneratorFunctionReturnType(0 /* Yield */, contextualReturnType, (functionFlags & 2 /* Async */) !== 0);
+ }
}
- // Overloads exist
- var belowArgCount = -Infinity;
- var aboveArgCount = Infinity;
- for (var _i = 0, signatures_9 = signatures; _i < signatures_9.length; _i++) {
- var sig = signatures_9[_i];
- var min_2 = getMinTypeArgumentCount(sig.typeParameters);
- var max = ts.length(sig.typeParameters);
- if (min_2 > argCount) {
- aboveArgCount = Math.min(aboveArgCount, min_2);
+ return undefined;
+ }
+ function isInParameterInitializerBeforeContainingFunction(node) {
+ var inBindingInitializer = false;
+ while (node.parent && !ts.isFunctionLike(node.parent)) {
+ if (ts.isParameter(node.parent) && (inBindingInitializer || node.parent.initializer === node)) {
+ return true;
}
- else if (max < argCount) {
- belowArgCount = Math.max(belowArgCount, max);
+ if (ts.isBindingElement(node.parent) && node.parent.initializer === node) {
+ inBindingInitializer = true;
}
+ node = node.parent;
}
- if (belowArgCount !== -Infinity && aboveArgCount !== Infinity) {
- return ts.createDiagnosticForNodeArray(ts.getSourceFileOfNode(node), typeArguments, ts.Diagnostics.No_overload_expects_0_type_arguments_but_overloads_do_exist_that_expect_either_1_or_2_type_arguments, argCount, belowArgCount, aboveArgCount);
+ return false;
+ }
+ function getContextualIterationType(kind, functionDecl) {
+ var isAsync = !!(ts.getFunctionFlags(functionDecl) & 2 /* Async */);
+ var contextualReturnType = getContextualReturnType(functionDecl);
+ if (contextualReturnType) {
+ return getIterationTypeOfGeneratorFunctionReturnType(kind, contextualReturnType, isAsync)
+ || undefined;
}
- return ts.createDiagnosticForNodeArray(ts.getSourceFileOfNode(node), typeArguments, ts.Diagnostics.Expected_0_type_arguments_but_got_1, belowArgCount === -Infinity ? aboveArgCount : belowArgCount, argCount);
+ return undefined;
}
- function resolveCall(node, signatures, candidatesOutArray, checkMode, callChainFlags, fallbackError) {
- var isTaggedTemplate = node.kind === 198 /* TaggedTemplateExpression */;
- var isDecorator = node.kind === 157 /* Decorator */;
- var isJsxOpeningOrSelfClosingElement = ts.isJsxOpeningLikeElement(node);
- var reportErrors = !candidatesOutArray;
- var typeArguments;
- if (!isDecorator) {
- typeArguments = node.typeArguments;
- // We already perform checking on the type arguments on the class declaration itself.
- if (isTaggedTemplate || isJsxOpeningOrSelfClosingElement || node.expression.kind !== 102 /* SuperKeyword */) {
- ts.forEach(typeArguments, checkSourceElement);
- }
+ function getContextualReturnType(functionDecl) {
+ // If the containing function has a return type annotation, is a constructor, or is a get accessor whose
+ // corresponding set accessor has a type annotation, return statements in the function are contextually typed
+ var returnType = getReturnTypeFromAnnotation(functionDecl);
+ if (returnType) {
+ return returnType;
}
- var candidates = candidatesOutArray || [];
- // reorderCandidates fills up the candidates array directly
- reorderCandidates(signatures, candidates, callChainFlags);
- if (!candidates.length) {
- if (reportErrors) {
- diagnostics.add(getDiagnosticForCallNode(node, ts.Diagnostics.Call_target_does_not_contain_any_signatures));
- }
- return resolveErrorCall(node);
+ // Otherwise, if the containing function is contextually typed by a function type with exactly one call signature
+ // and that call signature is non-generic, return statements are contextually typed by the return type of the signature
+ var signature = getContextualSignatureForFunctionLikeDeclaration(functionDecl);
+ if (signature && !isResolvingReturnTypeOfSignature(signature)) {
+ return getReturnTypeOfSignature(signature);
}
- var args = getEffectiveCallArguments(node);
- // The excludeArgument array contains true for each context sensitive argument (an argument
- // is context sensitive it is susceptible to a one-time permanent contextual typing).
- //
- // The idea is that we will perform type argument inference & assignability checking once
- // without using the susceptible parameters that are functions, and once more for those
- // parameters, contextually typing each as we go along.
- //
- // For a tagged template, then the first argument be 'undefined' if necessary because it
- // represents a TemplateStringsArray.
- //
- // For a decorator, no arguments are susceptible to contextual typing due to the fact
- // decorators are applied to a declaration by the emitter, and not to an expression.
- var isSingleNonGenericCandidate = candidates.length === 1 && !candidates[0].typeParameters;
- var argCheckMode = !isDecorator && !isSingleNonGenericCandidate && ts.some(args, isContextSensitive) ? 4 /* SkipContextSensitive */ : 0 /* Normal */;
- // The following variables are captured and modified by calls to chooseOverload.
- // If overload resolution or type argument inference fails, we want to report the
- // best error possible. The best error is one which says that an argument was not
- // assignable to a parameter. This implies that everything else about the overload
- // was fine. So if there is any overload that is only incorrect because of an
- // argument, we will report an error on that one.
- //
- // function foo(s: string): void;
- // function foo(n: number): void; // Report argument error on this overload
- // function foo(): void;
- // foo(true);
- //
- // If none of the overloads even made it that far, there are two possibilities.
- // There was a problem with type arguments for some overload, in which case
- // report an error on that. Or none of the overloads even had correct arity,
- // in which case give an arity error.
- //
- // function foo(x: T): void; // Report type argument error
- // function foo(): void;
- // foo(0);
- //
- var candidatesForArgumentError;
- var candidateForArgumentArityError;
- var candidateForTypeArgumentError;
- var result;
- // If we are in signature help, a trailing comma indicates that we intend to provide another argument,
- // so we will only accept overloads with arity at least 1 higher than the current number of provided arguments.
- var signatureHelpTrailingComma = !!(checkMode & 16 /* IsForSignatureHelp */) && node.kind === 196 /* CallExpression */ && node.arguments.hasTrailingComma;
- // Section 4.12.1:
- // if the candidate list contains one or more signatures for which the type of each argument
- // expression is a subtype of each corresponding parameter type, the return type of the first
- // of those signatures becomes the return type of the function call.
- // Otherwise, the return type of the first signature in the candidate list becomes the return
- // type of the function call.
- //
- // Whether the call is an error is determined by assignability of the arguments. The subtype pass
- // is just important for choosing the best signature. So in the case where there is only one
- // signature, the subtype pass is useless. So skipping it is an optimization.
- if (candidates.length > 1) {
- result = chooseOverload(candidates, subtypeRelation, signatureHelpTrailingComma);
+ return undefined;
+ }
+ // In a typed function call, an argument or substitution expression is contextually typed by the type of the corresponding parameter.
+ function getContextualTypeForArgument(callTarget, arg) {
+ var args = getEffectiveCallArguments(callTarget);
+ var argIndex = args.indexOf(arg); // -1 for e.g. the expression of a CallExpression, or the tag of a TaggedTemplateExpression
+ return argIndex === -1 ? undefined : getContextualTypeForArgumentAtIndex(callTarget, argIndex);
+ }
+ function getContextualTypeForArgumentAtIndex(callTarget, argIndex) {
+ // If we're already in the process of resolving the given signature, don't resolve again as
+ // that could cause infinite recursion. Instead, return anySignature.
+ var signature = getNodeLinks(callTarget).resolvedSignature === resolvingSignature ? resolvingSignature : getResolvedSignature(callTarget);
+ if (ts.isJsxOpeningLikeElement(callTarget) && argIndex === 0) {
+ return getEffectiveFirstArgumentForJsxSignature(signature, callTarget);
}
- if (!result) {
- result = chooseOverload(candidates, assignableRelation, signatureHelpTrailingComma);
+ return getTypeAtPosition(signature, argIndex);
+ }
+ function getContextualTypeForSubstitutionExpression(template, substitutionExpression) {
+ if (template.parent.kind === 205 /* TaggedTemplateExpression */) {
+ return getContextualTypeForArgument(template.parent, substitutionExpression);
}
- if (result) {
- return result;
+ return undefined;
+ }
+ function getContextualTypeForBinaryOperand(node, contextFlags) {
+ var binaryExpression = node.parent;
+ var left = binaryExpression.left, operatorToken = binaryExpression.operatorToken, right = binaryExpression.right;
+ switch (operatorToken.kind) {
+ case 62 /* EqualsToken */:
+ case 75 /* AmpersandAmpersandEqualsToken */:
+ case 74 /* BarBarEqualsToken */:
+ case 76 /* QuestionQuestionEqualsToken */:
+ return node === right ? getContextualTypeForAssignmentDeclaration(binaryExpression) : undefined;
+ case 56 /* BarBarToken */:
+ case 60 /* QuestionQuestionToken */:
+ // When an || expression has a contextual type, the operands are contextually typed by that type, except
+ // when that type originates in a binding pattern, the right operand is contextually typed by the type of
+ // the left operand. When an || expression has no contextual type, the right operand is contextually typed
+ // by the type of the left operand, except for the special case of Javascript declarations of the form
+ // `namespace.prop = namespace.prop || {}`.
+ var type = getContextualType(binaryExpression, contextFlags);
+ return node === right && (type && type.pattern || !type && !ts.isDefaultedExpandoInitializer(binaryExpression)) ?
+ getTypeOfExpression(left) : type;
+ case 55 /* AmpersandAmpersandToken */:
+ case 27 /* CommaToken */:
+ return node === right ? getContextualType(binaryExpression, contextFlags) : undefined;
+ default:
+ return undefined;
}
- // No signatures were applicable. Now report errors based on the last applicable signature with
- // no arguments excluded from assignability checks.
- // If candidate is undefined, it means that no candidates had a suitable arity. In that case,
- // skip the checkApplicableSignature check.
- if (reportErrors) {
- if (candidatesForArgumentError) {
- if (candidatesForArgumentError.length === 1 || candidatesForArgumentError.length > 3) {
- var last_2 = candidatesForArgumentError[candidatesForArgumentError.length - 1];
- var chain_1;
- if (candidatesForArgumentError.length > 3) {
- chain_1 = ts.chainDiagnosticMessages(chain_1, ts.Diagnostics.The_last_overload_gave_the_following_error);
- chain_1 = ts.chainDiagnosticMessages(chain_1, ts.Diagnostics.No_overload_matches_this_call);
- }
- var diags = getSignatureApplicabilityError(node, args, last_2, assignableRelation, 0 /* Normal */, /*reportErrors*/ true, function () { return chain_1; });
- if (diags) {
- for (var _i = 0, diags_1 = diags; _i < diags_1.length; _i++) {
- var d = diags_1[_i];
- if (last_2.declaration && candidatesForArgumentError.length > 3) {
- ts.addRelatedInfo(d, ts.createDiagnosticForNode(last_2.declaration, ts.Diagnostics.The_last_overload_is_declared_here));
- }
- diagnostics.add(d);
- }
- }
- else {
- ts.Debug.fail("No error for last overload signature");
- }
+ }
+ // In an assignment expression, the right operand is contextually typed by the type of the left operand.
+ // Don't do this for assignment declarations unless there is a type tag on the assignment, to avoid circularity from checking the right operand.
+ function getContextualTypeForAssignmentDeclaration(binaryExpression) {
+ var kind = ts.getAssignmentDeclarationKind(binaryExpression);
+ switch (kind) {
+ case 0 /* None */:
+ return getTypeOfExpression(binaryExpression.left);
+ case 5 /* Property */:
+ case 1 /* ExportsProperty */:
+ case 6 /* Prototype */:
+ case 3 /* PrototypeProperty */:
+ if (isPossiblyAliasedThisProperty(binaryExpression, kind)) {
+ return getContextualTypeForThisPropertyAssignment(binaryExpression, kind);
+ }
+ // If `binaryExpression.left` was assigned a symbol, then this is a new declaration; otherwise it is an assignment to an existing declaration.
+ // See `bindStaticPropertyAssignment` in `binder.ts`.
+ else if (!binaryExpression.left.symbol) {
+ return getTypeOfExpression(binaryExpression.left);
}
else {
- var allDiagnostics = [];
- var max = 0;
- var min_3 = Number.MAX_VALUE;
- var minIndex = 0;
- var i_1 = 0;
- var _loop_17 = function (c) {
- var chain_2 = function () { return ts.chainDiagnosticMessages(/*details*/ undefined, ts.Diagnostics.Overload_0_of_1_2_gave_the_following_error, i_1 + 1, candidates.length, signatureToString(c)); };
- var diags_2 = getSignatureApplicabilityError(node, args, c, assignableRelation, 0 /* Normal */, /*reportErrors*/ true, chain_2);
- if (diags_2) {
- if (diags_2.length <= min_3) {
- min_3 = diags_2.length;
- minIndex = i_1;
- }
- max = Math.max(max, diags_2.length);
- allDiagnostics.push(diags_2);
- }
- else {
- ts.Debug.fail("No error for 3 or fewer overload signatures");
- }
- i_1++;
- };
- for (var _a = 0, candidatesForArgumentError_1 = candidatesForArgumentError; _a < candidatesForArgumentError_1.length; _a++) {
- var c = candidatesForArgumentError_1[_a];
- _loop_17(c);
+ var decl = binaryExpression.left.symbol.valueDeclaration;
+ if (!decl) {
+ return undefined;
}
- var diags_3 = max > 1 ? allDiagnostics[minIndex] : ts.flatten(allDiagnostics);
- ts.Debug.assert(diags_3.length > 0, "No errors reported for 3 or fewer overload signatures");
- var chain = ts.chainDiagnosticMessages(ts.map(diags_3, function (d) { return typeof d.messageText === "string" ? d : d.messageText; }), ts.Diagnostics.No_overload_matches_this_call);
- var related = ts.flatMap(diags_3, function (d) { return d.relatedInformation; });
- if (ts.every(diags_3, function (d) { return d.start === diags_3[0].start && d.length === diags_3[0].length && d.file === diags_3[0].file; })) {
- var _b = diags_3[0], file = _b.file, start = _b.start, length_6 = _b.length;
- diagnostics.add({ file: file, start: start, length: length_6, code: chain.code, category: chain.category, messageText: chain, relatedInformation: related });
+ var lhs = ts.cast(binaryExpression.left, ts.isAccessExpression);
+ var overallAnnotation = ts.getEffectiveTypeAnnotationNode(decl);
+ if (overallAnnotation) {
+ return getTypeFromTypeNode(overallAnnotation);
}
- else {
- diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(node, chain, related));
+ else if (ts.isIdentifier(lhs.expression)) {
+ var id = lhs.expression;
+ var parentSymbol = resolveName(id, id.escapedText, 111551 /* Value */, undefined, id.escapedText, /*isUse*/ true);
+ if (parentSymbol) {
+ var annotated = parentSymbol.valueDeclaration && ts.getEffectiveTypeAnnotationNode(parentSymbol.valueDeclaration);
+ if (annotated) {
+ var nameStr = ts.getElementOrPropertyAccessName(lhs);
+ if (nameStr !== undefined) {
+ return getTypeOfPropertyOfContextualType(getTypeFromTypeNode(annotated), nameStr);
+ }
+ }
+ return undefined;
+ }
}
+ return ts.isInJSFile(decl) ? undefined : getTypeOfExpression(binaryExpression.left);
+ }
+ case 2 /* ModuleExports */:
+ case 4 /* ThisProperty */:
+ return getContextualTypeForThisPropertyAssignment(binaryExpression, kind);
+ case 7 /* ObjectDefinePropertyValue */:
+ case 8 /* ObjectDefinePropertyExports */:
+ case 9 /* ObjectDefinePrototypeProperty */:
+ return ts.Debug.fail("Does not apply");
+ default:
+ return ts.Debug.assertNever(kind);
+ }
+ }
+ function isPossiblyAliasedThisProperty(declaration, kind) {
+ if (kind === void 0) { kind = ts.getAssignmentDeclarationKind(declaration); }
+ if (kind === 4 /* ThisProperty */) {
+ return true;
+ }
+ if (!ts.isInJSFile(declaration) || kind !== 5 /* Property */ || !ts.isIdentifier(declaration.left.expression)) {
+ return false;
+ }
+ var name = declaration.left.expression.escapedText;
+ var symbol = resolveName(declaration.left, name, 111551 /* Value */, undefined, undefined, /*isUse*/ true, /*excludeGlobals*/ true);
+ return ts.isThisInitializedDeclaration(symbol === null || symbol === void 0 ? void 0 : symbol.valueDeclaration);
+ }
+ function getContextualTypeForThisPropertyAssignment(binaryExpression, kind) {
+ if (!binaryExpression.symbol)
+ return getTypeOfExpression(binaryExpression.left);
+ if (binaryExpression.symbol.valueDeclaration) {
+ var annotated = ts.getEffectiveTypeAnnotationNode(binaryExpression.symbol.valueDeclaration);
+ if (annotated) {
+ var type = getTypeFromTypeNode(annotated);
+ if (type) {
+ return type;
}
}
- else if (candidateForArgumentArityError) {
- diagnostics.add(getArgumentArityError(node, [candidateForArgumentArityError], args));
- }
- else if (candidateForTypeArgumentError) {
- checkTypeArguments(candidateForTypeArgumentError, node.typeArguments, /*reportErrors*/ true, fallbackError);
- }
- else {
- var signaturesWithCorrectTypeArgumentArity = ts.filter(signatures, function (s) { return hasCorrectTypeArgumentArity(s, typeArguments); });
- if (signaturesWithCorrectTypeArgumentArity.length === 0) {
- diagnostics.add(getTypeArgumentArityError(node, signatures, typeArguments));
+ }
+ if (kind === 2 /* ModuleExports */)
+ return undefined;
+ var thisAccess = ts.cast(binaryExpression.left, ts.isAccessExpression);
+ if (!ts.isObjectLiteralMethod(ts.getThisContainer(thisAccess.expression, /*includeArrowFunctions*/ false))) {
+ return undefined;
+ }
+ var thisType = checkThisExpression(thisAccess.expression);
+ var nameStr = ts.getElementOrPropertyAccessName(thisAccess);
+ return nameStr !== undefined && getTypeOfPropertyOfContextualType(thisType, nameStr) || undefined;
+ }
+ function isCircularMappedProperty(symbol) {
+ return !!(ts.getCheckFlags(symbol) & 262144 /* Mapped */ && !symbol.type && findResolutionCycleStartIndex(symbol, 0 /* Type */) >= 0);
+ }
+ function getTypeOfPropertyOfContextualType(type, name) {
+ return mapType(type, function (t) {
+ if (isGenericMappedType(t)) {
+ var constraint = getConstraintTypeFromMappedType(t);
+ var constraintOfConstraint = getBaseConstraintOfType(constraint) || constraint;
+ var propertyNameType = getLiteralType(ts.unescapeLeadingUnderscores(name));
+ if (isTypeAssignableTo(propertyNameType, constraintOfConstraint)) {
+ return substituteIndexedMappedType(t, propertyNameType);
}
- else if (!isDecorator) {
- diagnostics.add(getArgumentArityError(node, signaturesWithCorrectTypeArgumentArity, args));
+ }
+ else if (t.flags & 3670016 /* StructuredType */) {
+ var prop = getPropertyOfType(t, name);
+ if (prop) {
+ return isCircularMappedProperty(prop) ? undefined : getTypeOfSymbol(prop);
}
- else if (fallbackError) {
- diagnostics.add(getDiagnosticForCallNode(node, fallbackError));
+ if (isTupleType(t)) {
+ var restType = getRestTypeOfTupleType(t);
+ if (restType && isNumericLiteralName(name) && +name >= 0) {
+ return restType;
+ }
}
+ return isNumericLiteralName(name) && getIndexTypeOfContextualType(t, 1 /* Number */) ||
+ getIndexTypeOfContextualType(t, 0 /* String */);
}
+ return undefined;
+ }, /*noReductions*/ true);
+ }
+ function getIndexTypeOfContextualType(type, kind) {
+ return mapType(type, function (t) { return getIndexTypeOfStructuredType(t, kind); }, /*noReductions*/ true);
+ }
+ // In an object literal contextually typed by a type T, the contextual type of a property assignment is the type of
+ // the matching property in T, if one exists. Otherwise, it is the type of the numeric index signature in T, if one
+ // exists. Otherwise, it is the type of the string index signature in T, if one exists.
+ function getContextualTypeForObjectLiteralMethod(node, contextFlags) {
+ ts.Debug.assert(ts.isObjectLiteralMethod(node));
+ if (node.flags & 16777216 /* InWithStatement */) {
+ // We cannot answer semantic questions within a with block, do not proceed any further
+ return undefined;
}
- return getCandidateForOverloadFailure(node, candidates, args, !!candidatesOutArray);
- function chooseOverload(candidates, relation, signatureHelpTrailingComma) {
- if (signatureHelpTrailingComma === void 0) { signatureHelpTrailingComma = false; }
- candidatesForArgumentError = undefined;
- candidateForArgumentArityError = undefined;
- candidateForTypeArgumentError = undefined;
- if (isSingleNonGenericCandidate) {
- var candidate = candidates[0];
- if (ts.some(typeArguments) || !hasCorrectArity(node, args, candidate, signatureHelpTrailingComma)) {
- return undefined;
- }
- if (getSignatureApplicabilityError(node, args, candidate, relation, 0 /* Normal */, /*reportErrors*/ false, /*containingMessageChain*/ undefined)) {
- candidatesForArgumentError = [candidate];
- return undefined;
+ return getContextualTypeForObjectLiteralElement(node, contextFlags);
+ }
+ function getContextualTypeForObjectLiteralElement(element, contextFlags) {
+ var objectLiteral = element.parent;
+ var type = getApparentTypeOfContextualType(objectLiteral, contextFlags);
+ if (type) {
+ if (!hasNonBindableDynamicName(element)) {
+ // For a (non-symbol) computed property, there is no reason to look up the name
+ // in the type. It will just be "__computed", which does not appear in any
+ // SymbolTable.
+ var symbolName_3 = getSymbolOfNode(element).escapedName;
+ var propertyType = getTypeOfPropertyOfContextualType(type, symbolName_3);
+ if (propertyType) {
+ return propertyType;
}
- return candidate;
}
- for (var candidateIndex = 0; candidateIndex < candidates.length; candidateIndex++) {
- var candidate = candidates[candidateIndex];
- if (!hasCorrectTypeArgumentArity(candidate, typeArguments) || !hasCorrectArity(node, args, candidate, signatureHelpTrailingComma)) {
- continue;
- }
- var checkCandidate = void 0;
- var inferenceContext = void 0;
- if (candidate.typeParameters) {
- var typeArgumentTypes = void 0;
- if (ts.some(typeArguments)) {
- typeArgumentTypes = checkTypeArguments(candidate, typeArguments, /*reportErrors*/ false);
- if (!typeArgumentTypes) {
- candidateForTypeArgumentError = candidate;
- continue;
- }
- }
- else {
- inferenceContext = createInferenceContext(candidate.typeParameters, candidate, /*flags*/ ts.isInJSFile(node) ? 2 /* AnyDefault */ : 0 /* None */);
- typeArgumentTypes = inferTypeArguments(node, candidate, args, argCheckMode | 8 /* SkipGenericFunctions */, inferenceContext);
- argCheckMode |= inferenceContext.flags & 4 /* SkippedGenericFunction */ ? 8 /* SkipGenericFunctions */ : 0 /* Normal */;
- }
- checkCandidate = getSignatureInstantiation(candidate, typeArgumentTypes, ts.isInJSFile(candidate.declaration), inferenceContext && inferenceContext.inferredTypeParameters);
- // If the original signature has a generic rest type, instantiation may produce a
- // signature with different arity and we need to perform another arity check.
- if (getNonArrayRestType(candidate) && !hasCorrectArity(node, args, checkCandidate, signatureHelpTrailingComma)) {
- candidateForArgumentArityError = checkCandidate;
- continue;
- }
+ return isNumericName(element.name) && getIndexTypeOfContextualType(type, 1 /* Number */) ||
+ getIndexTypeOfContextualType(type, 0 /* String */);
+ }
+ return undefined;
+ }
+ // In an array literal contextually typed by a type T, the contextual type of an element expression at index N is
+ // the type of the property with the numeric name N in T, if one exists. Otherwise, if T has a numeric index signature,
+ // it is the type of the numeric index signature in T. Otherwise, in ES6 and higher, the contextual type is the iterated
+ // type of T.
+ function getContextualTypeForElementExpression(arrayContextualType, index) {
+ return arrayContextualType && (getTypeOfPropertyOfContextualType(arrayContextualType, "" + index)
+ || mapType(arrayContextualType, function (t) { return getIteratedTypeOrElementType(1 /* Element */, t, undefinedType, /*errorNode*/ undefined, /*checkAssignability*/ false); },
+ /*noReductions*/ true));
+ }
+ // In a contextually typed conditional expression, the true/false expressions are contextually typed by the same type.
+ function getContextualTypeForConditionalOperand(node, contextFlags) {
+ var conditional = node.parent;
+ return node === conditional.whenTrue || node === conditional.whenFalse ? getContextualType(conditional, contextFlags) : undefined;
+ }
+ function getContextualTypeForChildJsxExpression(node, child) {
+ var attributesType = getApparentTypeOfContextualType(node.openingElement.tagName);
+ // JSX expression is in children of JSX Element, we will look for an "children" atttribute (we get the name from JSX.ElementAttributesProperty)
+ var jsxChildrenPropertyName = getJsxElementChildrenPropertyName(getJsxNamespaceAt(node));
+ if (!(attributesType && !isTypeAny(attributesType) && jsxChildrenPropertyName && jsxChildrenPropertyName !== "")) {
+ return undefined;
+ }
+ var realChildren = ts.getSemanticJsxChildren(node.children);
+ var childIndex = realChildren.indexOf(child);
+ var childFieldType = getTypeOfPropertyOfContextualType(attributesType, jsxChildrenPropertyName);
+ return childFieldType && (realChildren.length === 1 ? childFieldType : mapType(childFieldType, function (t) {
+ if (isArrayLikeType(t)) {
+ return getIndexedAccessType(t, getLiteralType(childIndex));
+ }
+ else {
+ return t;
+ }
+ }, /*noReductions*/ true));
+ }
+ function getContextualTypeForJsxExpression(node) {
+ var exprParent = node.parent;
+ return ts.isJsxAttributeLike(exprParent)
+ ? getContextualType(node)
+ : ts.isJsxElement(exprParent)
+ ? getContextualTypeForChildJsxExpression(exprParent, node)
+ : undefined;
+ }
+ function getContextualTypeForJsxAttribute(attribute) {
+ // When we trying to resolve JsxOpeningLikeElement as a stateless function element, we will already give its attributes a contextual type
+ // which is a type of the parameter of the signature we are trying out.
+ // If there is no contextual type (e.g. we are trying to resolve stateful component), get attributes type from resolving element's tagName
+ if (ts.isJsxAttribute(attribute)) {
+ var attributesType = getApparentTypeOfContextualType(attribute.parent);
+ if (!attributesType || isTypeAny(attributesType)) {
+ return undefined;
+ }
+ return getTypeOfPropertyOfContextualType(attributesType, attribute.name.escapedText);
+ }
+ else {
+ return getContextualType(attribute.parent);
+ }
+ }
+ // Return true if the given expression is possibly a discriminant value. We limit the kinds of
+ // expressions we check to those that don't depend on their contextual type in order not to cause
+ // recursive (and possibly infinite) invocations of getContextualType.
+ function isPossiblyDiscriminantValue(node) {
+ switch (node.kind) {
+ case 10 /* StringLiteral */:
+ case 8 /* NumericLiteral */:
+ case 9 /* BigIntLiteral */:
+ case 14 /* NoSubstitutionTemplateLiteral */:
+ case 109 /* TrueKeyword */:
+ case 94 /* FalseKeyword */:
+ case 103 /* NullKeyword */:
+ case 78 /* Identifier */:
+ case 150 /* UndefinedKeyword */:
+ return true;
+ case 201 /* PropertyAccessExpression */:
+ case 207 /* ParenthesizedExpression */:
+ return isPossiblyDiscriminantValue(node.expression);
+ case 283 /* JsxExpression */:
+ return !node.expression || isPossiblyDiscriminantValue(node.expression);
+ }
+ return false;
+ }
+ function discriminateContextualTypeByObjectMembers(node, contextualType) {
+ return discriminateTypeByDiscriminableItems(contextualType, ts.map(ts.filter(node.properties, function (p) { return !!p.symbol && p.kind === 288 /* PropertyAssignment */ && isPossiblyDiscriminantValue(p.initializer) && isDiscriminantProperty(contextualType, p.symbol.escapedName); }), function (prop) { return [function () { return checkExpression(prop.initializer); }, prop.symbol.escapedName]; }), isTypeAssignableTo, contextualType);
+ }
+ function discriminateContextualTypeByJSXAttributes(node, contextualType) {
+ return discriminateTypeByDiscriminableItems(contextualType, ts.map(ts.filter(node.properties, function (p) { return !!p.symbol && p.kind === 280 /* JsxAttribute */ && isDiscriminantProperty(contextualType, p.symbol.escapedName) && (!p.initializer || isPossiblyDiscriminantValue(p.initializer)); }), function (prop) { return [!prop.initializer ? (function () { return trueType; }) : (function () { return checkExpression(prop.initializer); }), prop.symbol.escapedName]; }), isTypeAssignableTo, contextualType);
+ }
+ // Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily
+ // be "pushed" onto a node using the contextualType property.
+ function getApparentTypeOfContextualType(node, contextFlags) {
+ var contextualType = ts.isObjectLiteralMethod(node) ?
+ getContextualTypeForObjectLiteralMethod(node, contextFlags) :
+ getContextualType(node, contextFlags);
+ var instantiatedType = instantiateContextualType(contextualType, node, contextFlags);
+ if (instantiatedType && !(contextFlags && contextFlags & 2 /* NoConstraints */ && instantiatedType.flags & 8650752 /* TypeVariable */)) {
+ var apparentType = mapType(instantiatedType, getApparentType, /*noReductions*/ true);
+ if (apparentType.flags & 1048576 /* Union */) {
+ if (ts.isObjectLiteralExpression(node)) {
+ return discriminateContextualTypeByObjectMembers(node, apparentType);
}
- else {
- checkCandidate = candidate;
+ else if (ts.isJsxAttributes(node)) {
+ return discriminateContextualTypeByJSXAttributes(node, apparentType);
}
- if (getSignatureApplicabilityError(node, args, checkCandidate, relation, argCheckMode, /*reportErrors*/ false, /*containingMessageChain*/ undefined)) {
- // Give preference to error candidates that have no rest parameters (as they are more specific)
- (candidatesForArgumentError || (candidatesForArgumentError = [])).push(checkCandidate);
- continue;
+ }
+ return apparentType;
+ }
+ }
+ // If the given contextual type contains instantiable types and if a mapper representing
+ // return type inferences is available, instantiate those types using that mapper.
+ function instantiateContextualType(contextualType, node, contextFlags) {
+ if (contextualType && maybeTypeOfKind(contextualType, 465829888 /* Instantiable */)) {
+ var inferenceContext = getInferenceContext(node);
+ // If no inferences have been made, nothing is gained from instantiating as type parameters
+ // would just be replaced with their defaults similar to the apparent type.
+ if (inferenceContext && ts.some(inferenceContext.inferences, hasInferenceCandidates)) {
+ // For contextual signatures we incorporate all inferences made so far, e.g. from return
+ // types as well as arguments to the left in a function call.
+ if (contextFlags && contextFlags & 1 /* Signature */) {
+ return instantiateInstantiableTypes(contextualType, inferenceContext.nonFixingMapper);
}
- if (argCheckMode) {
- // If one or more context sensitive arguments were excluded, we start including
- // them now (and keeping do so for any subsequent candidates) and perform a second
- // round of type inference and applicability checking for this particular candidate.
- argCheckMode = 0 /* Normal */;
- if (inferenceContext) {
- var typeArgumentTypes = inferTypeArguments(node, candidate, args, argCheckMode, inferenceContext);
- checkCandidate = getSignatureInstantiation(candidate, typeArgumentTypes, ts.isInJSFile(candidate.declaration), inferenceContext && inferenceContext.inferredTypeParameters);
- // If the original signature has a generic rest type, instantiation may produce a
- // signature with different arity and we need to perform another arity check.
- if (getNonArrayRestType(candidate) && !hasCorrectArity(node, args, checkCandidate, signatureHelpTrailingComma)) {
- candidateForArgumentArityError = checkCandidate;
- continue;
- }
- }
- if (getSignatureApplicabilityError(node, args, checkCandidate, relation, argCheckMode, /*reportErrors*/ false, /*containingMessageChain*/ undefined)) {
- // Give preference to error candidates that have no rest parameters (as they are more specific)
- (candidatesForArgumentError || (candidatesForArgumentError = [])).push(checkCandidate);
- continue;
- }
+ // For other purposes (e.g. determining whether to produce literal types) we only
+ // incorporate inferences made from the return type in a function call.
+ if (inferenceContext.returnMapper) {
+ return instantiateInstantiableTypes(contextualType, inferenceContext.returnMapper);
}
- candidates[candidateIndex] = checkCandidate;
- return checkCandidate;
}
- return undefined;
}
+ return contextualType;
}
- // No signature was applicable. We have already reported the errors for the invalid signature.
- function getCandidateForOverloadFailure(node, candidates, args, hasCandidatesOutArray) {
- ts.Debug.assert(candidates.length > 0); // Else should not have called this.
- checkNodeDeferred(node);
- // Normally we will combine overloads. Skip this if they have type parameters since that's hard to combine.
- // Don't do this if there is a `candidatesOutArray`,
- // because then we want the chosen best candidate to be one of the overloads, not a combination.
- return hasCandidatesOutArray || candidates.length === 1 || candidates.some(function (c) { return !!c.typeParameters; })
- ? pickLongestCandidateSignature(node, candidates, args)
- : createUnionOfSignaturesForOverloadFailure(candidates);
+ // This function is similar to instantiateType, except that (a) it only instantiates types that
+ // are classified as instantiable (i.e. it doesn't instantiate object types), and (b) it performs
+ // no reductions on instantiated union types.
+ function instantiateInstantiableTypes(type, mapper) {
+ if (type.flags & 465829888 /* Instantiable */) {
+ return instantiateType(type, mapper);
+ }
+ if (type.flags & 1048576 /* Union */) {
+ return getUnionType(ts.map(type.types, function (t) { return instantiateInstantiableTypes(t, mapper); }), 0 /* None */);
+ }
+ if (type.flags & 2097152 /* Intersection */) {
+ return getIntersectionType(ts.map(type.types, function (t) { return instantiateInstantiableTypes(t, mapper); }));
+ }
+ return type;
}
- function createUnionOfSignaturesForOverloadFailure(candidates) {
- var thisParameters = ts.mapDefined(candidates, function (c) { return c.thisParameter; });
- var thisParameter;
- if (thisParameters.length) {
- thisParameter = createCombinedSymbolFromTypes(thisParameters, thisParameters.map(getTypeOfParameter));
+ /**
+ * Whoa! Do you really want to use this function?
+ *
+ * Unless you're trying to get the *non-apparent* type for a
+ * value-literal type or you're authoring relevant portions of this algorithm,
+ * you probably meant to use 'getApparentTypeOfContextualType'.
+ * Otherwise this may not be very useful.
+ *
+ * In cases where you *are* working on this function, you should understand
+ * when it is appropriate to use 'getContextualType' and 'getApparentTypeOfContextualType'.
+ *
+ * - Use 'getContextualType' when you are simply going to propagate the result to the expression.
+ * - Use 'getApparentTypeOfContextualType' when you're going to need the members of the type.
+ *
+ * @param node the expression whose contextual type will be returned.
+ * @returns the contextual type of an expression.
+ */
+ function getContextualType(node, contextFlags) {
+ if (node.flags & 16777216 /* InWithStatement */) {
+ // We cannot answer semantic questions within a with block, do not proceed any further
+ return undefined;
}
- var _a = ts.minAndMax(candidates, getNumNonRestParameters), minArgumentCount = _a.min, maxNonRestParam = _a.max;
- var parameters = [];
- var _loop_18 = function (i) {
- var symbols = ts.mapDefined(candidates, function (s) { return signatureHasRestParameter(s) ?
- i < s.parameters.length - 1 ? s.parameters[i] : ts.last(s.parameters) :
- i < s.parameters.length ? s.parameters[i] : undefined; });
- ts.Debug.assert(symbols.length !== 0);
- parameters.push(createCombinedSymbolFromTypes(symbols, ts.mapDefined(candidates, function (candidate) { return tryGetTypeAtPosition(candidate, i); })));
- };
- for (var i = 0; i < maxNonRestParam; i++) {
- _loop_18(i);
+ if (node.contextualType) {
+ return node.contextualType;
}
- var restParameterSymbols = ts.mapDefined(candidates, function (c) { return signatureHasRestParameter(c) ? ts.last(c.parameters) : undefined; });
- var flags = 0 /* None */;
- if (restParameterSymbols.length !== 0) {
- var type = createArrayType(getUnionType(ts.mapDefined(candidates, tryGetRestTypeOfSignature), 2 /* Subtype */));
- parameters.push(createCombinedSymbolForOverloadFailure(restParameterSymbols, type));
- flags |= 1 /* HasRestParameter */;
+ var parent = node.parent;
+ switch (parent.kind) {
+ case 249 /* VariableDeclaration */:
+ case 160 /* Parameter */:
+ case 163 /* PropertyDeclaration */:
+ case 162 /* PropertySignature */:
+ case 198 /* BindingElement */:
+ return getContextualTypeForInitializerExpression(node, contextFlags);
+ case 209 /* ArrowFunction */:
+ case 242 /* ReturnStatement */:
+ return getContextualTypeForReturnExpression(node);
+ case 219 /* YieldExpression */:
+ return getContextualTypeForYieldOperand(parent);
+ case 213 /* AwaitExpression */:
+ return getContextualTypeForAwaitOperand(parent, contextFlags);
+ case 203 /* CallExpression */:
+ if (parent.expression.kind === 99 /* ImportKeyword */) {
+ return stringType;
+ }
+ /* falls through */
+ case 204 /* NewExpression */:
+ return getContextualTypeForArgument(parent, node);
+ case 206 /* TypeAssertionExpression */:
+ case 224 /* AsExpression */:
+ return ts.isConstTypeReference(parent.type) ? tryFindWhenConstTypeReference(parent) : getTypeFromTypeNode(parent.type);
+ case 216 /* BinaryExpression */:
+ return getContextualTypeForBinaryOperand(node, contextFlags);
+ case 288 /* PropertyAssignment */:
+ case 289 /* ShorthandPropertyAssignment */:
+ return getContextualTypeForObjectLiteralElement(parent, contextFlags);
+ case 290 /* SpreadAssignment */:
+ return getApparentTypeOfContextualType(parent.parent, contextFlags);
+ case 199 /* ArrayLiteralExpression */: {
+ var arrayLiteral = parent;
+ var type = getApparentTypeOfContextualType(arrayLiteral, contextFlags);
+ return getContextualTypeForElementExpression(type, ts.indexOfNode(arrayLiteral.elements, node));
+ }
+ case 217 /* ConditionalExpression */:
+ return getContextualTypeForConditionalOperand(node, contextFlags);
+ case 228 /* TemplateSpan */:
+ ts.Debug.assert(parent.parent.kind === 218 /* TemplateExpression */);
+ return getContextualTypeForSubstitutionExpression(parent.parent, node);
+ case 207 /* ParenthesizedExpression */: {
+ // Like in `checkParenthesizedExpression`, an `/** @type {xyz} */` comment before a parenthesized expression acts as a type cast.
+ var tag = ts.isInJSFile(parent) ? ts.getJSDocTypeTag(parent) : undefined;
+ return tag ? getTypeFromTypeNode(tag.typeExpression.type) : getContextualType(parent, contextFlags);
+ }
+ case 283 /* JsxExpression */:
+ return getContextualTypeForJsxExpression(parent);
+ case 280 /* JsxAttribute */:
+ case 282 /* JsxSpreadAttribute */:
+ return getContextualTypeForJsxAttribute(parent);
+ case 275 /* JsxOpeningElement */:
+ case 274 /* JsxSelfClosingElement */:
+ return getContextualJsxElementAttributesType(parent, contextFlags);
}
- if (candidates.some(signatureHasLiteralTypes)) {
- flags |= 2 /* HasLiteralTypes */;
+ return undefined;
+ function tryFindWhenConstTypeReference(node) {
+ if (ts.isCallLikeExpression(node.parent)) {
+ return getContextualTypeForArgument(node.parent, node);
+ }
+ return undefined;
}
- return createSignature(candidates[0].declaration,
- /*typeParameters*/ undefined, // Before calling this we tested for `!candidates.some(c => !!c.typeParameters)`.
- thisParameter, parameters,
- /*resolvedReturnType*/ getIntersectionType(candidates.map(getReturnTypeOfSignature)),
- /*typePredicate*/ undefined, minArgumentCount, flags);
}
- function getNumNonRestParameters(signature) {
- var numParams = signature.parameters.length;
- return signatureHasRestParameter(signature) ? numParams - 1 : numParams;
+ function getInferenceContext(node) {
+ var ancestor = ts.findAncestor(node, function (n) { return !!n.inferenceContext; });
+ return ancestor && ancestor.inferenceContext;
}
- function createCombinedSymbolFromTypes(sources, types) {
- return createCombinedSymbolForOverloadFailure(sources, getUnionType(types, 2 /* Subtype */));
+ function getContextualJsxElementAttributesType(node, contextFlags) {
+ if (ts.isJsxOpeningElement(node) && node.parent.contextualType && contextFlags !== 4 /* Completions */) {
+ // Contextually applied type is moved from attributes up to the outer jsx attributes so when walking up from the children they get hit
+ // _However_ to hit them from the _attributes_ we must look for them here; otherwise we'll used the declared type
+ // (as below) instead!
+ return node.parent.contextualType;
+ }
+ return getContextualTypeForArgumentAtIndex(node, 0);
}
- function createCombinedSymbolForOverloadFailure(sources, type) {
- // This function is currently only used for erroneous overloads, so it's good enough to just use the first source.
- return createSymbolWithType(ts.first(sources), type);
+ function getEffectiveFirstArgumentForJsxSignature(signature, node) {
+ return getJsxReferenceKind(node) !== 0 /* Component */
+ ? getJsxPropsTypeFromCallSignature(signature, node)
+ : getJsxPropsTypeFromClassType(signature, node);
}
- function pickLongestCandidateSignature(node, candidates, args) {
- // Pick the longest signature. This way we can get a contextual type for cases like:
- // declare function f(a: { xa: number; xb: number; }, b: number);
- // f({ |
- // Also, use explicitly-supplied type arguments if they are provided, so we can get a contextual signature in cases like:
- // declare function f(k: keyof T);
- // f("
- var bestIndex = getLongestCandidateIndex(candidates, apparentArgumentCount === undefined ? args.length : apparentArgumentCount);
- var candidate = candidates[bestIndex];
- var typeParameters = candidate.typeParameters;
- if (!typeParameters) {
- return candidate;
+ function getJsxPropsTypeFromCallSignature(sig, context) {
+ var propsType = getTypeOfFirstParameterOfSignatureWithFallback(sig, unknownType);
+ propsType = getJsxManagedAttributesFromLocatedAttributes(context, getJsxNamespaceAt(context), propsType);
+ var intrinsicAttribs = getJsxType(JsxNames.IntrinsicAttributes, context);
+ if (intrinsicAttribs !== errorType) {
+ propsType = intersectTypes(intrinsicAttribs, propsType);
}
- var typeArgumentNodes = callLikeExpressionMayHaveTypeArguments(node) ? node.typeArguments : undefined;
- var instantiated = typeArgumentNodes
- ? createSignatureInstantiation(candidate, getTypeArgumentsFromNodes(typeArgumentNodes, typeParameters, ts.isInJSFile(node)))
- : inferSignatureInstantiationForOverloadFailure(node, typeParameters, candidate, args);
- candidates[bestIndex] = instantiated;
- return instantiated;
+ return propsType;
}
- function getTypeArgumentsFromNodes(typeArgumentNodes, typeParameters, isJs) {
- var typeArguments = typeArgumentNodes.map(getTypeOfNode);
- while (typeArguments.length > typeParameters.length) {
- typeArguments.pop();
+ function getJsxPropsTypeForSignatureFromMember(sig, forcedLookupLocation) {
+ if (sig.unionSignatures) {
+ // JSX Elements using the legacy `props`-field based lookup (eg, react class components) need to treat the `props` member as an input
+ // instead of an output position when resolving the signature. We need to go back to the input signatures of the composite signature,
+ // get the type of `props` on each return type individually, and then _intersect them_, rather than union them (as would normally occur
+ // for a union signature). It's an unfortunate quirk of looking in the output of the signature for the type we want to use for the input.
+ // The default behavior of `getTypeOfFirstParameterOfSignatureWithFallback` when no `props` member name is defined is much more sane.
+ var results = [];
+ for (var _i = 0, _a = sig.unionSignatures; _i < _a.length; _i++) {
+ var signature = _a[_i];
+ var instance = getReturnTypeOfSignature(signature);
+ if (isTypeAny(instance)) {
+ return instance;
+ }
+ var propType = getTypeOfPropertyOfType(instance, forcedLookupLocation);
+ if (!propType) {
+ return;
+ }
+ results.push(propType);
+ }
+ return getIntersectionType(results);
}
- while (typeArguments.length < typeParameters.length) {
- typeArguments.push(getConstraintOfTypeParameter(typeParameters[typeArguments.length]) || getDefaultTypeArgumentType(isJs));
+ var instanceType = getReturnTypeOfSignature(sig);
+ return isTypeAny(instanceType) ? instanceType : getTypeOfPropertyOfType(instanceType, forcedLookupLocation);
+ }
+ function getStaticTypeOfReferencedJsxConstructor(context) {
+ if (isJsxIntrinsicIdentifier(context.tagName)) {
+ var result = getIntrinsicAttributesTypeFromJsxOpeningLikeElement(context);
+ var fakeSignature = createSignatureForJSXIntrinsic(context, result);
+ return getOrCreateTypeFromSignature(fakeSignature);
}
- return typeArguments;
+ var tagType = checkExpressionCached(context.tagName);
+ if (tagType.flags & 128 /* StringLiteral */) {
+ var result = getIntrinsicAttributesTypeFromStringLiteralType(tagType, context);
+ if (!result) {
+ return errorType;
+ }
+ var fakeSignature = createSignatureForJSXIntrinsic(context, result);
+ return getOrCreateTypeFromSignature(fakeSignature);
+ }
+ return tagType;
}
- function inferSignatureInstantiationForOverloadFailure(node, typeParameters, candidate, args) {
- var inferenceContext = createInferenceContext(typeParameters, candidate, /*flags*/ ts.isInJSFile(node) ? 2 /* AnyDefault */ : 0 /* None */);
- var typeArgumentTypes = inferTypeArguments(node, candidate, args, 4 /* SkipContextSensitive */ | 8 /* SkipGenericFunctions */, inferenceContext);
- return createSignatureInstantiation(candidate, typeArgumentTypes);
+ function getJsxManagedAttributesFromLocatedAttributes(context, ns, attributesType) {
+ var managedSym = getJsxLibraryManagedAttributes(ns);
+ if (managedSym) {
+ var declaredManagedType = getDeclaredTypeOfSymbol(managedSym);
+ var ctorType = getStaticTypeOfReferencedJsxConstructor(context);
+ if (ts.length(declaredManagedType.typeParameters) >= 2) {
+ var args = fillMissingTypeArguments([ctorType, attributesType], declaredManagedType.typeParameters, 2, ts.isInJSFile(context));
+ return createTypeReference(declaredManagedType, args);
+ }
+ else if (ts.length(declaredManagedType.aliasTypeArguments) >= 2) {
+ var args = fillMissingTypeArguments([ctorType, attributesType], declaredManagedType.aliasTypeArguments, 2, ts.isInJSFile(context));
+ return getTypeAliasInstantiation(declaredManagedType.aliasSymbol, args);
+ }
+ }
+ return attributesType;
}
- function getLongestCandidateIndex(candidates, argsCount) {
- var maxParamsIndex = -1;
- var maxParams = -1;
- for (var i = 0; i < candidates.length; i++) {
- var candidate = candidates[i];
- var paramCount = getParameterCount(candidate);
- if (hasEffectiveRestParameter(candidate) || paramCount >= argsCount) {
- return i;
+ function getJsxPropsTypeFromClassType(sig, context) {
+ var ns = getJsxNamespaceAt(context);
+ var forcedLookupLocation = getJsxElementPropertiesName(ns);
+ var attributesType = forcedLookupLocation === undefined
+ // If there is no type ElementAttributesProperty, return the type of the first parameter of the signature, which should be the props type
+ ? getTypeOfFirstParameterOfSignatureWithFallback(sig, unknownType)
+ : forcedLookupLocation === ""
+ // If there is no e.g. 'props' member in ElementAttributesProperty, use the element class type instead
+ ? getReturnTypeOfSignature(sig)
+ // Otherwise get the type of the property on the signature return type
+ : getJsxPropsTypeForSignatureFromMember(sig, forcedLookupLocation);
+ if (!attributesType) {
+ // There is no property named 'props' on this instance type
+ if (!!forcedLookupLocation && !!ts.length(context.attributes.properties)) {
+ error(context, ts.Diagnostics.JSX_element_class_does_not_support_attributes_because_it_does_not_have_a_0_property, ts.unescapeLeadingUnderscores(forcedLookupLocation));
+ }
+ return unknownType;
+ }
+ attributesType = getJsxManagedAttributesFromLocatedAttributes(context, ns, attributesType);
+ if (isTypeAny(attributesType)) {
+ // Props is of type 'any' or unknown
+ return attributesType;
+ }
+ else {
+ // Normal case -- add in IntrinsicClassElements and IntrinsicElements
+ var apparentAttributesType = attributesType;
+ var intrinsicClassAttribs = getJsxType(JsxNames.IntrinsicClassAttributes, context);
+ if (intrinsicClassAttribs !== errorType) {
+ var typeParams = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(intrinsicClassAttribs.symbol);
+ var hostClassType = getReturnTypeOfSignature(sig);
+ apparentAttributesType = intersectTypes(typeParams
+ ? createTypeReference(intrinsicClassAttribs, fillMissingTypeArguments([hostClassType], typeParams, getMinTypeArgumentCount(typeParams), ts.isInJSFile(context)))
+ : intrinsicClassAttribs, apparentAttributesType);
}
- if (paramCount > maxParams) {
- maxParams = paramCount;
- maxParamsIndex = i;
+ var intrinsicAttribs = getJsxType(JsxNames.IntrinsicAttributes, context);
+ if (intrinsicAttribs !== errorType) {
+ apparentAttributesType = intersectTypes(intrinsicAttribs, apparentAttributesType);
}
+ return apparentAttributesType;
}
- return maxParamsIndex;
}
- function resolveCallExpression(node, candidatesOutArray, checkMode) {
- if (node.expression.kind === 102 /* SuperKeyword */) {
- var superType = checkSuperExpression(node.expression);
- if (isTypeAny(superType)) {
- for (var _i = 0, _a = node.arguments; _i < _a.length; _i++) {
- var arg = _a[_i];
- checkExpression(arg); // Still visit arguments so they get marked for visibility, etc
- }
- return anySignature;
- }
- if (superType !== errorType) {
- // In super call, the candidate signatures are the matching arity signatures of the base constructor function instantiated
- // with the type arguments specified in the extends clause.
- var baseTypeNode = ts.getEffectiveBaseTypeNode(ts.getContainingClass(node));
- if (baseTypeNode) {
- var baseConstructors = getInstantiatedConstructorsForTypeArguments(superType, baseTypeNode.typeArguments, baseTypeNode);
- return resolveCall(node, baseConstructors, candidatesOutArray, checkMode, 0 /* None */);
- }
+ // If the given type is an object or union type with a single signature, and if that signature has at
+ // least as many parameters as the given function, return the signature. Otherwise return undefined.
+ function getContextualCallSignature(type, node) {
+ var signatures = getSignaturesOfType(type, 0 /* Call */);
+ if (signatures.length === 1) {
+ var signature = signatures[0];
+ if (!isAritySmaller(signature, node)) {
+ return signature;
}
- return resolveUntypedCall(node);
}
- var callChainFlags;
- var funcType = checkExpression(node.expression);
- if (ts.isCallChain(node)) {
- var nonOptionalType = getOptionalExpressionType(funcType, node.expression);
- callChainFlags = nonOptionalType === funcType ? 0 /* None */ :
- ts.isOutermostOptionalChain(node) ? 8 /* IsOuterCallChain */ :
- 4 /* IsInnerCallChain */;
- funcType = nonOptionalType;
+ }
+ /** If the contextual signature has fewer parameters than the function expression, do not use it */
+ function isAritySmaller(signature, target) {
+ var targetParameterCount = 0;
+ for (; targetParameterCount < target.parameters.length; targetParameterCount++) {
+ var param = target.parameters[targetParameterCount];
+ if (param.initializer || param.questionToken || param.dotDotDotToken || isJSDocOptionalParameter(param)) {
+ break;
+ }
}
- else {
- callChainFlags = 0 /* None */;
+ if (target.parameters.length && ts.parameterIsThisKeyword(target.parameters[0])) {
+ targetParameterCount--;
}
- funcType = checkNonNullTypeWithReporter(funcType, node.expression, reportCannotInvokePossiblyNullOrUndefinedError);
- if (funcType === silentNeverType) {
- return silentNeverSignature;
+ return !hasEffectiveRestParameter(signature) && getParameterCount(signature) < targetParameterCount;
+ }
+ function isFunctionExpressionOrArrowFunction(node) {
+ return node.kind === 208 /* FunctionExpression */ || node.kind === 209 /* ArrowFunction */;
+ }
+ function getContextualSignatureForFunctionLikeDeclaration(node) {
+ // Only function expressions, arrow functions, and object literal methods are contextually typed.
+ return isFunctionExpressionOrArrowFunction(node) || ts.isObjectLiteralMethod(node)
+ ? getContextualSignature(node)
+ : undefined;
+ }
+ // Return the contextual signature for a given expression node. A contextual type provides a
+ // contextual signature if it has a single call signature and if that call signature is non-generic.
+ // If the contextual type is a union type, get the signature from each type possible and if they are
+ // all identical ignoring their return type, the result is same signature but with return type as
+ // union type of return types from these signatures
+ function getContextualSignature(node) {
+ ts.Debug.assert(node.kind !== 165 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node));
+ var typeTagSignature = getSignatureOfTypeTag(node);
+ if (typeTagSignature) {
+ return typeTagSignature;
}
- var apparentType = getApparentType(funcType);
- if (apparentType === errorType) {
- // Another error has already been reported
- return resolveErrorCall(node);
+ var type = getApparentTypeOfContextualType(node, 1 /* Signature */);
+ if (!type) {
+ return undefined;
}
- // Technically, this signatures list may be incomplete. We are taking the apparent type,
- // but we are not including call signatures that may have been added to the Object or
- // Function interface, since they have none by default. This is a bit of a leap of faith
- // that the user will not add any.
- var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */);
- var numConstructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */).length;
- // TS 1.0 Spec: 4.12
- // In an untyped function call no TypeArgs are permitted, Args can be any argument list, no contextual
- // types are provided for the argument expressions, and the result is always of type Any.
- if (isUntypedFunctionCall(funcType, apparentType, callSignatures.length, numConstructSignatures)) {
- // The unknownType indicates that an error already occurred (and was reported). No
- // need to report another error in this case.
- if (funcType !== errorType && node.typeArguments) {
- error(node, ts.Diagnostics.Untyped_function_calls_may_not_accept_type_arguments);
- }
- return resolveUntypedCall(node);
+ if (!(type.flags & 1048576 /* Union */)) {
+ return getContextualCallSignature(type, node);
}
- // If FuncExpr's apparent type(section 3.8.1) is a function type, the call is a typed function call.
- // TypeScript employs overload resolution in typed function calls in order to support functions
- // with multiple call signatures.
- if (!callSignatures.length) {
- if (numConstructSignatures) {
- error(node, ts.Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new, typeToString(funcType));
- }
- else {
- var relatedInformation = void 0;
- if (node.arguments.length === 1) {
- var text = ts.getSourceFileOfNode(node).text;
- if (ts.isLineBreak(text.charCodeAt(ts.skipTrivia(text, node.expression.end, /* stopAfterLineBreak */ true) - 1))) {
- relatedInformation = ts.createDiagnosticForNode(node.expression, ts.Diagnostics.Are_you_missing_a_semicolon);
- }
+ var signatureList;
+ var types = type.types;
+ for (var _i = 0, types_18 = types; _i < types_18.length; _i++) {
+ var current = types_18[_i];
+ var signature = getContextualCallSignature(current, node);
+ if (signature) {
+ if (!signatureList) {
+ // This signature will contribute to contextual union signature
+ signatureList = [signature];
+ }
+ else if (!compareSignaturesIdentical(signatureList[0], signature, /*partialMatch*/ false, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesIdentical)) {
+ // Signatures aren't identical, do not use
+ return undefined;
+ }
+ else {
+ // Use this signature for contextual union signature
+ signatureList.push(signature);
}
- invocationError(node.expression, apparentType, 0 /* Call */, relatedInformation);
}
- return resolveErrorCall(node);
}
- // When a call to a generic function is an argument to an outer call to a generic function for which
- // inference is in process, we have a choice to make. If the inner call relies on inferences made from
- // its contextual type to its return type, deferring the inner call processing allows the best possible
- // contextual type to accumulate. But if the outer call relies on inferences made from the return type of
- // the inner call, the inner call should be processed early. There's no sure way to know which choice is
- // right (only a full unification algorithm can determine that), so we resort to the following heuristic:
- // If no type arguments are specified in the inner call and at least one call signature is generic and
- // returns a function type, we choose to defer processing. This narrowly permits function composition
- // operators to flow inferences through return types, but otherwise processes calls right away. We
- // use the resolvingSignature singleton to indicate that we deferred processing. This result will be
- // propagated out and eventually turned into nonInferrableType (a type that is assignable to anything and
- // from which we never make inferences).
- if (checkMode & 8 /* SkipGenericFunctions */ && !node.typeArguments && callSignatures.some(isGenericFunctionReturningFunction)) {
- skippedGenericFunction(node, checkMode);
- return resolvingSignature;
+ // Result is union of signatures collected (return type is union of return types of this signature set)
+ if (signatureList) {
+ return signatureList.length === 1 ? signatureList[0] : createUnionSignature(signatureList[0], signatureList);
}
- // If the function is explicitly marked with `@class`, then it must be constructed.
- if (callSignatures.some(function (sig) { return ts.isInJSFile(sig.declaration) && !!ts.getJSDocClassTag(sig.declaration); })) {
- error(node, ts.Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new, typeToString(funcType));
- return resolveErrorCall(node);
+ }
+ function checkSpreadExpression(node, checkMode) {
+ if (languageVersion < 2 /* ES2015 */) {
+ checkExternalEmitHelpers(node, compilerOptions.downlevelIteration ? 1536 /* SpreadIncludes */ : 2048 /* SpreadArrays */);
}
- return resolveCall(node, callSignatures, candidatesOutArray, checkMode, callChainFlags);
+ var arrayOrIterableType = checkExpression(node.expression, checkMode);
+ return checkIteratedTypeOrElementType(33 /* Spread */, arrayOrIterableType, undefinedType, node.expression);
}
- function isGenericFunctionReturningFunction(signature) {
- return !!(signature.typeParameters && isFunctionType(getReturnTypeOfSignature(signature)));
+ function checkSyntheticExpression(node) {
+ return node.isSpread ? getIndexedAccessType(node.type, numberType) : node.type;
}
- /**
- * TS 1.0 spec: 4.12
- * If FuncExpr is of type Any, or of an object type that has no call or construct signatures
- * but is a subtype of the Function interface, the call is an untyped function call.
- */
- function isUntypedFunctionCall(funcType, apparentFuncType, numCallSignatures, numConstructSignatures) {
- // We exclude union types because we may have a union of function types that happen to have no common signatures.
- return isTypeAny(funcType) || isTypeAny(apparentFuncType) && !!(funcType.flags & 262144 /* TypeParameter */) ||
- !numCallSignatures && !numConstructSignatures && !(apparentFuncType.flags & (1048576 /* Union */ | 131072 /* Never */)) && isTypeAssignableTo(funcType, globalFunctionType);
+ function hasDefaultValue(node) {
+ return (node.kind === 198 /* BindingElement */ && !!node.initializer) ||
+ (node.kind === 216 /* BinaryExpression */ && node.operatorToken.kind === 62 /* EqualsToken */);
}
- function resolveNewExpression(node, candidatesOutArray, checkMode) {
- if (node.arguments && languageVersion < 1 /* ES5 */) {
- var spreadIndex = getSpreadArgumentIndex(node.arguments);
- if (spreadIndex >= 0) {
- error(node.arguments[spreadIndex], ts.Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher);
+ function checkArrayLiteral(node, checkMode, forceTuple) {
+ var elements = node.elements;
+ var elementCount = elements.length;
+ var elementTypes = [];
+ var elementFlags = [];
+ var contextualType = getApparentTypeOfContextualType(node);
+ var inDestructuringPattern = ts.isAssignmentTarget(node);
+ var inConstContext = isConstContext(node);
+ for (var i = 0; i < elementCount; i++) {
+ var e = elements[i];
+ if (e.kind === 220 /* SpreadElement */) {
+ if (languageVersion < 2 /* ES2015 */) {
+ checkExternalEmitHelpers(e, compilerOptions.downlevelIteration ? 1536 /* SpreadIncludes */ : 2048 /* SpreadArrays */);
+ }
+ var spreadType = checkExpression(e.expression, checkMode, forceTuple);
+ if (isArrayLikeType(spreadType)) {
+ elementTypes.push(spreadType);
+ elementFlags.push(8 /* Variadic */);
+ }
+ else if (inDestructuringPattern) {
+ // Given the following situation:
+ // var c: {};
+ // [...c] = ["", 0];
+ //
+ // c is represented in the tree as a spread element in an array literal.
+ // But c really functions as a rest element, and its purpose is to provide
+ // a contextual type for the right hand side of the assignment. Therefore,
+ // instead of calling checkExpression on "...c", which will give an error
+ // if c is not iterable/array-like, we need to act as if we are trying to
+ // get the contextual element type from it. So we do something similar to
+ // getContextualTypeForElementExpression, which will crucially not error
+ // if there is no index type / iterated type.
+ var restElementType = getIndexTypeOfType(spreadType, 1 /* Number */) ||
+ getIteratedTypeOrElementType(65 /* Destructuring */, spreadType, undefinedType, /*errorNode*/ undefined, /*checkAssignability*/ false) ||
+ unknownType;
+ elementTypes.push(restElementType);
+ elementFlags.push(4 /* Rest */);
+ }
+ else {
+ elementTypes.push(checkIteratedTypeOrElementType(33 /* Spread */, spreadType, undefinedType, e.expression));
+ elementFlags.push(4 /* Rest */);
+ }
+ }
+ else {
+ var elementContextualType = getContextualTypeForElementExpression(contextualType, elementTypes.length);
+ var type = checkExpressionForMutableLocation(e, checkMode, elementContextualType, forceTuple);
+ elementTypes.push(type);
+ elementFlags.push(1 /* Required */);
}
}
- var expressionType = checkNonNullExpression(node.expression);
- if (expressionType === silentNeverType) {
- return silentNeverSignature;
- }
- // If expressionType's apparent type(section 3.8.1) is an object type with one or
- // more construct signatures, the expression is processed in the same manner as a
- // function call, but using the construct signatures as the initial set of candidate
- // signatures for overload resolution. The result type of the function call becomes
- // the result type of the operation.
- expressionType = getApparentType(expressionType);
- if (expressionType === errorType) {
- // Another error has already been reported
- return resolveErrorCall(node);
+ if (inDestructuringPattern) {
+ return createTupleType(elementTypes, elementFlags);
}
- // TS 1.0 spec: 4.11
- // If expressionType is of type Any, Args can be any argument
- // list and the result of the operation is of type Any.
- if (isTypeAny(expressionType)) {
- if (node.typeArguments) {
- error(node, ts.Diagnostics.Untyped_function_calls_may_not_accept_type_arguments);
- }
- return resolveUntypedCall(node);
+ if (forceTuple || inConstContext || contextualType && forEachType(contextualType, isTupleLikeType)) {
+ return createArrayLiteralType(createTupleType(elementTypes, elementFlags, /*readonly*/ inConstContext));
}
- // Technically, this signatures list may be incomplete. We are taking the apparent type,
- // but we are not including construct signatures that may have been added to the Object or
- // Function interface, since they have none by default. This is a bit of a leap of faith
- // that the user will not add any.
- var constructSignatures = getSignaturesOfType(expressionType, 1 /* Construct */);
- if (constructSignatures.length) {
- if (!isConstructorAccessible(node, constructSignatures[0])) {
- return resolveErrorCall(node);
- }
- // If the expression is a class of abstract type, then it cannot be instantiated.
- // Note, only class declarations can be declared abstract.
- // In the case of a merged class-module or class-interface declaration,
- // only the class declaration node will have the Abstract flag set.
- var valueDecl = expressionType.symbol && ts.getClassLikeDeclarationOfSymbol(expressionType.symbol);
- if (valueDecl && ts.hasModifier(valueDecl, 128 /* Abstract */)) {
- error(node, ts.Diagnostics.Cannot_create_an_instance_of_an_abstract_class);
- return resolveErrorCall(node);
- }
- return resolveCall(node, constructSignatures, candidatesOutArray, checkMode, 0 /* None */);
+ return createArrayLiteralType(createArrayType(elementTypes.length ?
+ getUnionType(ts.sameMap(elementTypes, function (t, i) { return elementFlags[i] & 8 /* Variadic */ ? getIndexedAccessTypeOrUndefined(t, numberType) || anyType : t; }), 2 /* Subtype */) :
+ strictNullChecks ? implicitNeverType : undefinedWideningType, inConstContext));
+ }
+ function createArrayLiteralType(type) {
+ if (!(ts.getObjectFlags(type) & 4 /* Reference */)) {
+ return type;
}
- // If expressionType's apparent type is an object type with no construct signatures but
- // one or more call signatures, the expression is processed as a function call. A compile-time
- // error occurs if the result of the function call is not Void. The type of the result of the
- // operation is Any. It is an error to have a Void this type.
- var callSignatures = getSignaturesOfType(expressionType, 0 /* Call */);
- if (callSignatures.length) {
- var signature = resolveCall(node, callSignatures, candidatesOutArray, checkMode, 0 /* None */);
- if (!noImplicitAny) {
- if (signature.declaration && !isJSConstructor(signature.declaration) && getReturnTypeOfSignature(signature) !== voidType) {
- error(node, ts.Diagnostics.Only_a_void_function_can_be_called_with_the_new_keyword);
- }
- if (getThisTypeOfSignature(signature) === voidType) {
- error(node, ts.Diagnostics.A_function_that_is_called_with_the_new_keyword_cannot_have_a_this_type_that_is_void);
- }
- }
- return signature;
+ var literalType = type.literalType;
+ if (!literalType) {
+ literalType = type.literalType = cloneTypeReference(type);
+ literalType.objectFlags |= 65536 /* ArrayLiteral */ | 1048576 /* ContainsObjectOrArrayLiteral */;
}
- invocationError(node.expression, expressionType, 1 /* Construct */);
- return resolveErrorCall(node);
+ return literalType;
}
- function typeHasProtectedAccessibleBase(target, type) {
- var baseTypes = getBaseTypes(type);
- if (!ts.length(baseTypes)) {
- return false;
+ function isNumericName(name) {
+ switch (name.kind) {
+ case 158 /* ComputedPropertyName */:
+ return isNumericComputedName(name);
+ case 78 /* Identifier */:
+ return isNumericLiteralName(name.escapedText);
+ case 8 /* NumericLiteral */:
+ case 10 /* StringLiteral */:
+ return isNumericLiteralName(name.text);
+ default:
+ return false;
}
- var firstBase = baseTypes[0];
- if (firstBase.flags & 2097152 /* Intersection */) {
- var types = firstBase.types;
- var mixinFlags = findMixins(types);
- var i = 0;
- for (var _i = 0, _a = firstBase.types; _i < _a.length; _i++) {
- var intersectionMember = _a[_i];
- // We want to ignore mixin ctors
- if (!mixinFlags[i]) {
- if (ts.getObjectFlags(intersectionMember) & (1 /* Class */ | 2 /* Interface */)) {
- if (intersectionMember.symbol === target) {
- return true;
- }
- if (typeHasProtectedAccessibleBase(target, intersectionMember)) {
- return true;
- }
- }
- }
- i++;
+ }
+ function isNumericComputedName(name) {
+ // It seems odd to consider an expression of type Any to result in a numeric name,
+ // but this behavior is consistent with checkIndexedAccess
+ return isTypeAssignableToKind(checkComputedPropertyName(name), 296 /* NumberLike */);
+ }
+ function isInfinityOrNaNString(name) {
+ return name === "Infinity" || name === "-Infinity" || name === "NaN";
+ }
+ function isNumericLiteralName(name) {
+ // The intent of numeric names is that
+ // - they are names with text in a numeric form, and that
+ // - setting properties/indexing with them is always equivalent to doing so with the numeric literal 'numLit',
+ // acquired by applying the abstract 'ToNumber' operation on the name's text.
+ //
+ // The subtlety is in the latter portion, as we cannot reliably say that anything that looks like a numeric literal is a numeric name.
+ // In fact, it is the case that the text of the name must be equal to 'ToString(numLit)' for this to hold.
+ //
+ // Consider the property name '"0xF00D"'. When one indexes with '0xF00D', they are actually indexing with the value of 'ToString(0xF00D)'
+ // according to the ECMAScript specification, so it is actually as if the user indexed with the string '"61453"'.
+ // Thus, the text of all numeric literals equivalent to '61543' such as '0xF00D', '0xf00D', '0170015', etc. are not valid numeric names
+ // because their 'ToString' representation is not equal to their original text.
+ // This is motivated by ECMA-262 sections 9.3.1, 9.8.1, 11.1.5, and 11.2.1.
+ //
+ // Here, we test whether 'ToString(ToNumber(name))' is exactly equal to 'name'.
+ // The '+' prefix operator is equivalent here to applying the abstract ToNumber operation.
+ // Applying the 'toString()' method on a number gives us the abstract ToString operation on a number.
+ //
+ // Note that this accepts the values 'Infinity', '-Infinity', and 'NaN', and that this is intentional.
+ // This is desired behavior, because when indexing with them as numeric entities, you are indexing
+ // with the strings '"Infinity"', '"-Infinity"', and '"NaN"' respectively.
+ return (+name).toString() === name;
+ }
+ function checkComputedPropertyName(node) {
+ var links = getNodeLinks(node.expression);
+ if (!links.resolvedType) {
+ links.resolvedType = checkExpression(node.expression);
+ // This will allow types number, string, symbol or any. It will also allow enums, the unknown
+ // type, and any union of these types (like string | number).
+ if (links.resolvedType.flags & 98304 /* Nullable */ ||
+ !isTypeAssignableToKind(links.resolvedType, 402653316 /* StringLike */ | 296 /* NumberLike */ | 12288 /* ESSymbolLike */) &&
+ !isTypeAssignableTo(links.resolvedType, stringNumberSymbolType)) {
+ error(node, ts.Diagnostics.A_computed_property_name_must_be_of_type_string_number_symbol_or_any);
+ }
+ else {
+ checkThatExpressionIsProperSymbolReference(node.expression, links.resolvedType, /*reportError*/ true);
}
- return false;
}
- if (firstBase.symbol === target) {
- return true;
+ return links.resolvedType;
+ }
+ function isSymbolWithNumericName(symbol) {
+ var _a;
+ var firstDecl = (_a = symbol.declarations) === null || _a === void 0 ? void 0 : _a[0];
+ return isNumericLiteralName(symbol.escapedName) || (firstDecl && ts.isNamedDeclaration(firstDecl) && isNumericName(firstDecl.name));
+ }
+ function getObjectLiteralIndexInfo(node, offset, properties, kind) {
+ var propTypes = [];
+ for (var i = offset; i < properties.length; i++) {
+ if (kind === 0 /* String */ || isSymbolWithNumericName(properties[i])) {
+ propTypes.push(getTypeOfSymbol(properties[i]));
+ }
}
- return typeHasProtectedAccessibleBase(target, firstBase);
+ var unionType = propTypes.length ? getUnionType(propTypes, 2 /* Subtype */) : undefinedType;
+ return createIndexInfo(unionType, isConstContext(node));
}
- function isConstructorAccessible(node, signature) {
- if (!signature || !signature.declaration) {
- return true;
+ function getImmediateAliasedSymbol(symbol) {
+ ts.Debug.assert((symbol.flags & 2097152 /* Alias */) !== 0, "Should only get Alias here.");
+ var links = getSymbolLinks(symbol);
+ if (!links.immediateTarget) {
+ var node = getDeclarationOfAliasSymbol(symbol);
+ if (!node)
+ return ts.Debug.fail();
+ links.immediateTarget = getTargetOfAliasDeclaration(node, /*dontRecursivelyResolve*/ true);
}
- var declaration = signature.declaration;
- var modifiers = ts.getSelectedModifierFlags(declaration, 24 /* NonPublicAccessibilityModifier */);
- // (1) Public constructors and (2) constructor functions are always accessible.
- if (!modifiers || declaration.kind !== 162 /* Constructor */) {
- return true;
+ return links.immediateTarget;
+ }
+ function checkObjectLiteral(node, checkMode) {
+ var inDestructuringPattern = ts.isAssignmentTarget(node);
+ // Grammar checking
+ checkGrammarObjectLiteralExpression(node, inDestructuringPattern);
+ var allPropertiesTable = strictNullChecks ? ts.createSymbolTable() : undefined;
+ var propertiesTable = ts.createSymbolTable();
+ var propertiesArray = [];
+ var spread = emptyObjectType;
+ var contextualType = getApparentTypeOfContextualType(node);
+ var contextualTypeHasPattern = contextualType && contextualType.pattern &&
+ (contextualType.pattern.kind === 196 /* ObjectBindingPattern */ || contextualType.pattern.kind === 200 /* ObjectLiteralExpression */);
+ var inConstContext = isConstContext(node);
+ var checkFlags = inConstContext ? 8 /* Readonly */ : 0;
+ var isInJavascript = ts.isInJSFile(node) && !ts.isInJsonFile(node);
+ var enumTag = ts.getJSDocEnumTag(node);
+ var isJSObjectLiteral = !contextualType && isInJavascript && !enumTag;
+ var objectFlags = freshObjectLiteralFlag;
+ var patternWithComputedProperties = false;
+ var hasComputedStringProperty = false;
+ var hasComputedNumberProperty = false;
+ // Spreads may cause an early bail; ensure computed names are always checked (this is cached)
+ // As otherwise they may not be checked until exports for the type at this position are retrieved,
+ // which may never occur.
+ for (var _i = 0, _a = node.properties; _i < _a.length; _i++) {
+ var elem = _a[_i];
+ if (elem.name && ts.isComputedPropertyName(elem.name) && !ts.isWellKnownSymbolSyntactically(elem.name)) {
+ checkComputedPropertyName(elem.name);
+ }
}
- var declaringClassDeclaration = ts.getClassLikeDeclarationOfSymbol(declaration.parent.symbol);
- var declaringClass = getDeclaredTypeOfSymbol(declaration.parent.symbol);
- // A private or protected constructor can only be instantiated within its own class (or a subclass, for protected)
- if (!isNodeWithinClass(node, declaringClassDeclaration)) {
- var containingClass = ts.getContainingClass(node);
- if (containingClass && modifiers & 16 /* Protected */) {
- var containingType = getTypeOfNode(containingClass);
- if (typeHasProtectedAccessibleBase(declaration.parent.symbol, containingType)) {
- return true;
+ var offset = 0;
+ for (var _b = 0, _c = node.properties; _b < _c.length; _b++) {
+ var memberDecl = _c[_b];
+ var member = getSymbolOfNode(memberDecl);
+ var computedNameType = memberDecl.name && memberDecl.name.kind === 158 /* ComputedPropertyName */ && !ts.isWellKnownSymbolSyntactically(memberDecl.name.expression) ?
+ checkComputedPropertyName(memberDecl.name) : undefined;
+ if (memberDecl.kind === 288 /* PropertyAssignment */ ||
+ memberDecl.kind === 289 /* ShorthandPropertyAssignment */ ||
+ ts.isObjectLiteralMethod(memberDecl)) {
+ var type = memberDecl.kind === 288 /* PropertyAssignment */ ? checkPropertyAssignment(memberDecl, checkMode) :
+ // avoid resolving the left side of the ShorthandPropertyAssignment outside of the destructuring
+ // for error recovery purposes. For example, if a user wrote `{ a = 100 }` instead of `{ a: 100 }`.
+ // we don't want to say "could not find 'a'".
+ memberDecl.kind === 289 /* ShorthandPropertyAssignment */ ? checkExpressionForMutableLocation(!inDestructuringPattern && memberDecl.objectAssignmentInitializer ? memberDecl.objectAssignmentInitializer : memberDecl.name, checkMode) :
+ checkObjectLiteralMethod(memberDecl, checkMode);
+ if (isInJavascript) {
+ var jsDocType = getTypeForDeclarationFromJSDocComment(memberDecl);
+ if (jsDocType) {
+ checkTypeAssignableTo(type, jsDocType, memberDecl);
+ type = jsDocType;
+ }
+ else if (enumTag && enumTag.typeExpression) {
+ checkTypeAssignableTo(type, getTypeFromTypeNode(enumTag.typeExpression), memberDecl);
+ }
}
+ objectFlags |= ts.getObjectFlags(type) & 3670016 /* PropagatingFlags */;
+ var nameType = computedNameType && isTypeUsableAsPropertyName(computedNameType) ? computedNameType : undefined;
+ var prop = nameType ?
+ createSymbol(4 /* Property */ | member.flags, getPropertyNameFromType(nameType), checkFlags | 4096 /* Late */) :
+ createSymbol(4 /* Property */ | member.flags, member.escapedName, checkFlags);
+ if (nameType) {
+ prop.nameType = nameType;
+ }
+ if (inDestructuringPattern) {
+ // If object literal is an assignment pattern and if the assignment pattern specifies a default value
+ // for the property, make the property optional.
+ var isOptional = (memberDecl.kind === 288 /* PropertyAssignment */ && hasDefaultValue(memberDecl.initializer)) ||
+ (memberDecl.kind === 289 /* ShorthandPropertyAssignment */ && memberDecl.objectAssignmentInitializer);
+ if (isOptional) {
+ prop.flags |= 16777216 /* Optional */;
+ }
+ }
+ else if (contextualTypeHasPattern && !(ts.getObjectFlags(contextualType) & 512 /* ObjectLiteralPatternWithComputedProperties */)) {
+ // If object literal is contextually typed by the implied type of a binding pattern, and if the
+ // binding pattern specifies a default value for the property, make the property optional.
+ var impliedProp = getPropertyOfType(contextualType, member.escapedName);
+ if (impliedProp) {
+ prop.flags |= impliedProp.flags & 16777216 /* Optional */;
+ }
+ else if (!compilerOptions.suppressExcessPropertyErrors && !getIndexInfoOfType(contextualType, 0 /* String */)) {
+ error(memberDecl.name, ts.Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, symbolToString(member), typeToString(contextualType));
+ }
+ }
+ prop.declarations = member.declarations;
+ prop.parent = member.parent;
+ if (member.valueDeclaration) {
+ prop.valueDeclaration = member.valueDeclaration;
+ }
+ prop.type = type;
+ prop.target = member;
+ member = prop;
+ allPropertiesTable === null || allPropertiesTable === void 0 ? void 0 : allPropertiesTable.set(prop.escapedName, prop);
}
- if (modifiers & 8 /* Private */) {
- error(node, ts.Diagnostics.Constructor_of_class_0_is_private_and_only_accessible_within_the_class_declaration, typeToString(declaringClass));
+ else if (memberDecl.kind === 290 /* SpreadAssignment */) {
+ if (languageVersion < 2 /* ES2015 */) {
+ checkExternalEmitHelpers(memberDecl, 2 /* Assign */);
+ }
+ if (propertiesArray.length > 0) {
+ spread = getSpreadType(spread, createObjectLiteralType(), node.symbol, objectFlags, inConstContext);
+ propertiesArray = [];
+ propertiesTable = ts.createSymbolTable();
+ hasComputedStringProperty = false;
+ hasComputedNumberProperty = false;
+ }
+ var type = getReducedType(checkExpression(memberDecl.expression));
+ if (!isValidSpreadType(type)) {
+ error(memberDecl, ts.Diagnostics.Spread_types_may_only_be_created_from_object_types);
+ return errorType;
+ }
+ if (allPropertiesTable) {
+ checkSpreadPropOverrides(type, allPropertiesTable, memberDecl);
+ }
+ spread = getSpreadType(spread, type, node.symbol, objectFlags, inConstContext);
+ offset = propertiesArray.length;
+ continue;
}
- if (modifiers & 16 /* Protected */) {
- error(node, ts.Diagnostics.Constructor_of_class_0_is_protected_and_only_accessible_within_the_class_declaration, typeToString(declaringClass));
+ else {
+ // TypeScript 1.0 spec (April 2014)
+ // A get accessor declaration is processed in the same manner as
+ // an ordinary function declaration(section 6.1) with no parameters.
+ // A set accessor declaration is processed in the same manner
+ // as an ordinary function declaration with a single parameter and a Void return type.
+ ts.Debug.assert(memberDecl.kind === 167 /* GetAccessor */ || memberDecl.kind === 168 /* SetAccessor */);
+ checkNodeDeferred(memberDecl);
}
- return false;
- }
- return true;
- }
- function invocationErrorDetails(apparentType, kind) {
- var errorInfo;
- var isCall = kind === 0 /* Call */;
- var awaitedType = getAwaitedType(apparentType);
- var maybeMissingAwait = awaitedType && getSignaturesOfType(awaitedType, kind).length > 0;
- if (apparentType.flags & 1048576 /* Union */) {
- var types = apparentType.types;
- var hasSignatures = false;
- for (var _i = 0, types_18 = types; _i < types_18.length; _i++) {
- var constituent = types_18[_i];
- var signatures = getSignaturesOfType(constituent, kind);
- if (signatures.length !== 0) {
- hasSignatures = true;
- if (errorInfo) {
- // Bail early if we already have an error, no chance of "No constituent of type is callable"
- break;
+ if (computedNameType && !(computedNameType.flags & 8576 /* StringOrNumberLiteralOrUnique */)) {
+ if (isTypeAssignableTo(computedNameType, stringNumberSymbolType)) {
+ if (isTypeAssignableTo(computedNameType, numberType)) {
+ hasComputedNumberProperty = true;
}
- }
- else {
- // Error on the first non callable constituent only
- if (!errorInfo) {
- errorInfo = ts.chainDiagnosticMessages(errorInfo, isCall ?
- ts.Diagnostics.Type_0_has_no_call_signatures :
- ts.Diagnostics.Type_0_has_no_construct_signatures, typeToString(constituent));
- errorInfo = ts.chainDiagnosticMessages(errorInfo, isCall ?
- ts.Diagnostics.Not_all_constituents_of_type_0_are_callable :
- ts.Diagnostics.Not_all_constituents_of_type_0_are_constructable, typeToString(apparentType));
+ else {
+ hasComputedStringProperty = true;
}
- if (hasSignatures) {
- // Bail early if we already found a siganture, no chance of "No constituent of type is callable"
- break;
+ if (inDestructuringPattern) {
+ patternWithComputedProperties = true;
}
}
}
- if (!hasSignatures) {
- errorInfo = ts.chainDiagnosticMessages(
- /* detials */ undefined, isCall ?
- ts.Diagnostics.No_constituent_of_type_0_is_callable :
- ts.Diagnostics.No_constituent_of_type_0_is_constructable, typeToString(apparentType));
- }
- if (!errorInfo) {
- errorInfo = ts.chainDiagnosticMessages(errorInfo, isCall ?
- ts.Diagnostics.Each_member_of_the_union_type_0_has_signatures_but_none_of_those_signatures_are_compatible_with_each_other :
- ts.Diagnostics.Each_member_of_the_union_type_0_has_construct_signatures_but_none_of_those_signatures_are_compatible_with_each_other, typeToString(apparentType));
+ else {
+ propertiesTable.set(member.escapedName, member);
}
+ propertiesArray.push(member);
}
- else {
- errorInfo = ts.chainDiagnosticMessages(errorInfo, isCall ?
- ts.Diagnostics.Type_0_has_no_call_signatures :
- ts.Diagnostics.Type_0_has_no_construct_signatures, typeToString(apparentType));
+ // If object literal is contextually typed by the implied type of a binding pattern, augment the result
+ // type with those properties for which the binding pattern specifies a default value.
+ // If the object literal is spread into another object literal, skip this step and let the top-level object
+ // literal handle it instead.
+ if (contextualTypeHasPattern && node.parent.kind !== 290 /* SpreadAssignment */) {
+ for (var _d = 0, _e = getPropertiesOfType(contextualType); _d < _e.length; _d++) {
+ var prop = _e[_d];
+ if (!propertiesTable.get(prop.escapedName) && !getPropertyOfType(spread, prop.escapedName)) {
+ if (!(prop.flags & 16777216 /* Optional */)) {
+ error(prop.valueDeclaration || prop.bindingElement, ts.Diagnostics.Initializer_provides_no_value_for_this_binding_element_and_the_binding_element_has_no_default_value);
+ }
+ propertiesTable.set(prop.escapedName, prop);
+ propertiesArray.push(prop);
+ }
+ }
}
- return {
- messageChain: ts.chainDiagnosticMessages(errorInfo, isCall ? ts.Diagnostics.This_expression_is_not_callable : ts.Diagnostics.This_expression_is_not_constructable),
- relatedMessage: maybeMissingAwait ? ts.Diagnostics.Did_you_forget_to_use_await : undefined,
- };
- }
- function invocationError(errorTarget, apparentType, kind, relatedInformation) {
- var _a = invocationErrorDetails(apparentType, kind), messageChain = _a.messageChain, relatedInfo = _a.relatedMessage;
- var diagnostic = ts.createDiagnosticForNodeFromMessageChain(errorTarget, messageChain);
- if (relatedInfo) {
- ts.addRelatedInfo(diagnostic, ts.createDiagnosticForNode(errorTarget, relatedInfo));
+ if (spread !== emptyObjectType) {
+ if (propertiesArray.length > 0) {
+ spread = getSpreadType(spread, createObjectLiteralType(), node.symbol, objectFlags, inConstContext);
+ propertiesArray = [];
+ propertiesTable = ts.createSymbolTable();
+ hasComputedStringProperty = false;
+ hasComputedNumberProperty = false;
+ }
+ // remap the raw emptyObjectType fed in at the top into a fresh empty object literal type, unique to this use site
+ return mapType(spread, function (t) { return t === emptyObjectType ? createObjectLiteralType() : t; });
}
- if (ts.isCallExpression(errorTarget.parent)) {
- var _b = getDiagnosticSpanForCallNode(errorTarget.parent, /* doNotIncludeArguments */ true), start = _b.start, length_7 = _b.length;
- diagnostic.start = start;
- diagnostic.length = length_7;
+ return createObjectLiteralType();
+ function createObjectLiteralType() {
+ var stringIndexInfo = hasComputedStringProperty ? getObjectLiteralIndexInfo(node, offset, propertiesArray, 0 /* String */) : undefined;
+ var numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node, offset, propertiesArray, 1 /* Number */) : undefined;
+ var result = createAnonymousType(node.symbol, propertiesTable, ts.emptyArray, ts.emptyArray, stringIndexInfo, numberIndexInfo);
+ result.objectFlags |= objectFlags | 128 /* ObjectLiteral */ | 1048576 /* ContainsObjectOrArrayLiteral */;
+ if (isJSObjectLiteral) {
+ result.objectFlags |= 16384 /* JSLiteral */;
+ }
+ if (patternWithComputedProperties) {
+ result.objectFlags |= 512 /* ObjectLiteralPatternWithComputedProperties */;
+ }
+ if (inDestructuringPattern) {
+ result.pattern = node;
+ }
+ return result;
}
- diagnostics.add(diagnostic);
- invocationErrorRecovery(apparentType, kind, relatedInformation ? ts.addRelatedInfo(diagnostic, relatedInformation) : diagnostic);
}
- function invocationErrorRecovery(apparentType, kind, diagnostic) {
- if (!apparentType.symbol) {
- return;
- }
- var importNode = getSymbolLinks(apparentType.symbol).originatingImport;
- // Create a diagnostic on the originating import if possible onto which we can attach a quickfix
- // An import call expression cannot be rewritten into another form to correct the error - the only solution is to use `.default` at the use-site
- if (importNode && !ts.isImportCall(importNode)) {
- var sigs = getSignaturesOfType(getTypeOfSymbol(getSymbolLinks(apparentType.symbol).target), kind);
- if (!sigs || !sigs.length)
- return;
- ts.addRelatedInfo(diagnostic, ts.createDiagnosticForNode(importNode, ts.Diagnostics.Type_originates_at_this_import_A_namespace_style_import_cannot_be_called_or_constructed_and_will_cause_a_failure_at_runtime_Consider_using_a_default_import_or_import_require_here_instead));
+ function isValidSpreadType(type) {
+ if (type.flags & 465829888 /* Instantiable */) {
+ var constraint = getBaseConstraintOfType(type);
+ if (constraint !== undefined) {
+ return isValidSpreadType(constraint);
+ }
}
+ return !!(type.flags & (1 /* Any */ | 67108864 /* NonPrimitive */ | 524288 /* Object */ | 58982400 /* InstantiableNonPrimitive */) ||
+ getFalsyFlags(type) & 117632 /* DefinitelyFalsy */ && isValidSpreadType(removeDefinitelyFalsyTypes(type)) ||
+ type.flags & 3145728 /* UnionOrIntersection */ && ts.every(type.types, isValidSpreadType));
}
- function resolveTaggedTemplateExpression(node, candidatesOutArray, checkMode) {
- var tagType = checkExpression(node.tag);
- var apparentType = getApparentType(tagType);
- if (apparentType === errorType) {
- // Another error has already been reported
- return resolveErrorCall(node);
- }
- var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */);
- var numConstructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */).length;
- if (isUntypedFunctionCall(tagType, apparentType, callSignatures.length, numConstructSignatures)) {
- return resolveUntypedCall(node);
- }
- if (!callSignatures.length) {
- invocationError(node.tag, apparentType, 0 /* Call */);
- return resolveErrorCall(node);
- }
- return resolveCall(node, callSignatures, candidatesOutArray, checkMode, 0 /* None */);
+ function checkJsxSelfClosingElementDeferred(node) {
+ checkJsxOpeningLikeElementOrOpeningFragment(node);
}
- /**
- * Gets the localized diagnostic head message to use for errors when resolving a decorator as a call expression.
- */
- function getDiagnosticHeadMessageForDecoratorResolution(node) {
- switch (node.parent.kind) {
- case 245 /* ClassDeclaration */:
- case 214 /* ClassExpression */:
- return ts.Diagnostics.Unable_to_resolve_signature_of_class_decorator_when_called_as_an_expression;
- case 156 /* Parameter */:
- return ts.Diagnostics.Unable_to_resolve_signature_of_parameter_decorator_when_called_as_an_expression;
- case 159 /* PropertyDeclaration */:
- return ts.Diagnostics.Unable_to_resolve_signature_of_property_decorator_when_called_as_an_expression;
- case 161 /* MethodDeclaration */:
- case 163 /* GetAccessor */:
- case 164 /* SetAccessor */:
- return ts.Diagnostics.Unable_to_resolve_signature_of_method_decorator_when_called_as_an_expression;
- default:
- return ts.Debug.fail();
- }
+ function checkJsxSelfClosingElement(node, _checkMode) {
+ checkNodeDeferred(node);
+ return getJsxElementTypeAt(node) || anyType;
}
- /**
- * Resolves a decorator as if it were a call expression.
- */
- function resolveDecorator(node, candidatesOutArray, checkMode) {
- var funcType = checkExpression(node.expression);
- var apparentType = getApparentType(funcType);
- if (apparentType === errorType) {
- return resolveErrorCall(node);
- }
- var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */);
- var numConstructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */).length;
- if (isUntypedFunctionCall(funcType, apparentType, callSignatures.length, numConstructSignatures)) {
- return resolveUntypedCall(node);
- }
- if (isPotentiallyUncalledDecorator(node, callSignatures)) {
- var nodeStr = ts.getTextOfNode(node.expression, /*includeTrivia*/ false);
- error(node, ts.Diagnostics._0_accepts_too_few_arguments_to_be_used_as_a_decorator_here_Did_you_mean_to_call_it_first_and_write_0, nodeStr);
- return resolveErrorCall(node);
+ function checkJsxElementDeferred(node) {
+ // Check attributes
+ checkJsxOpeningLikeElementOrOpeningFragment(node.openingElement);
+ // Perform resolution on the closing tag so that rename/go to definition/etc work
+ if (isJsxIntrinsicIdentifier(node.closingElement.tagName)) {
+ getIntrinsicTagSymbol(node.closingElement);
}
- var headMessage = getDiagnosticHeadMessageForDecoratorResolution(node);
- if (!callSignatures.length) {
- var errorDetails = invocationErrorDetails(apparentType, 0 /* Call */);
- var messageChain = ts.chainDiagnosticMessages(errorDetails.messageChain, headMessage);
- var diag = ts.createDiagnosticForNodeFromMessageChain(node.expression, messageChain);
- if (errorDetails.relatedMessage) {
- ts.addRelatedInfo(diag, ts.createDiagnosticForNode(node.expression, errorDetails.relatedMessage));
- }
- diagnostics.add(diag);
- invocationErrorRecovery(apparentType, 0 /* Call */, diag);
- return resolveErrorCall(node);
+ else {
+ checkExpression(node.closingElement.tagName);
}
- return resolveCall(node, callSignatures, candidatesOutArray, checkMode, 0 /* None */, headMessage);
+ checkJsxChildren(node);
}
- function createSignatureForJSXIntrinsic(node, result) {
- var namespace = getJsxNamespaceAt(node);
- var exports = namespace && getExportsOfSymbol(namespace);
- // We fake up a SFC signature for each intrinsic, however a more specific per-element signature drawn from the JSX declaration
- // file would probably be preferable.
- var typeSymbol = exports && getSymbol(exports, JsxNames.Element, 788968 /* Type */);
- var returnNode = typeSymbol && nodeBuilder.symbolToEntityName(typeSymbol, 788968 /* Type */, node);
- var declaration = ts.createFunctionTypeNode(/*typeParameters*/ undefined, [ts.createParameter(/*decorators*/ undefined, /*modifiers*/ undefined, /*dotdotdot*/ undefined, "props", /*questionMark*/ undefined, nodeBuilder.typeToTypeNode(result, node))], returnNode ? ts.createTypeReferenceNode(returnNode, /*typeArguments*/ undefined) : ts.createKeywordTypeNode(125 /* AnyKeyword */));
- var parameterSymbol = createSymbol(1 /* FunctionScopedVariable */, "props");
- parameterSymbol.type = result;
- return createSignature(declaration,
- /*typeParameters*/ undefined,
- /*thisParameter*/ undefined, [parameterSymbol], typeSymbol ? getDeclaredTypeOfSymbol(typeSymbol) : errorType,
- /*returnTypePredicate*/ undefined, 1, 0 /* None */);
+ function checkJsxElement(node, _checkMode) {
+ checkNodeDeferred(node);
+ return getJsxElementTypeAt(node) || anyType;
}
- function resolveJsxOpeningLikeElement(node, candidatesOutArray, checkMode) {
- if (isJsxIntrinsicIdentifier(node.tagName)) {
- var result = getIntrinsicAttributesTypeFromJsxOpeningLikeElement(node);
- var fakeSignature = createSignatureForJSXIntrinsic(node, result);
- checkTypeAssignableToAndOptionallyElaborate(checkExpressionWithContextualType(node.attributes, getEffectiveFirstArgumentForJsxSignature(fakeSignature, node), /*mapper*/ undefined, 0 /* Normal */), result, node.tagName, node.attributes);
- return fakeSignature;
- }
- var exprTypes = checkExpression(node.tagName);
- var apparentType = getApparentType(exprTypes);
- if (apparentType === errorType) {
- return resolveErrorCall(node);
- }
- var signatures = getUninstantiatedJsxSignaturesOfType(exprTypes, node);
- if (isUntypedFunctionCall(exprTypes, apparentType, signatures.length, /*constructSignatures*/ 0)) {
- return resolveUntypedCall(node);
- }
- if (signatures.length === 0) {
- // We found no signatures at all, which is an error
- error(node.tagName, ts.Diagnostics.JSX_element_type_0_does_not_have_any_construct_or_call_signatures, ts.getTextOfNode(node.tagName));
- return resolveErrorCall(node);
+ function checkJsxFragment(node) {
+ checkJsxOpeningLikeElementOrOpeningFragment(node.openingFragment);
+ // by default, jsx:'react' will use jsxFactory = React.createElement and jsxFragmentFactory = React.Fragment
+ // if jsxFactory compiler option is provided, ensure jsxFragmentFactory compiler option or @jsxFrag pragma is provided too
+ var nodeSourceFile = ts.getSourceFileOfNode(node);
+ if (ts.getJSXTransformEnabled(compilerOptions) && (compilerOptions.jsxFactory || nodeSourceFile.pragmas.has("jsx"))
+ && !compilerOptions.jsxFragmentFactory && !nodeSourceFile.pragmas.has("jsxfrag")) {
+ error(node, compilerOptions.jsxFactory
+ ? ts.Diagnostics.The_jsxFragmentFactory_compiler_option_must_be_provided_to_use_JSX_fragments_with_the_jsxFactory_compiler_option
+ : ts.Diagnostics.An_jsxFrag_pragma_is_required_when_using_an_jsx_pragma_with_JSX_fragments);
}
- return resolveCall(node, signatures, candidatesOutArray, checkMode, 0 /* None */);
+ checkJsxChildren(node);
+ return getJsxElementTypeAt(node) || anyType;
}
/**
- * Sometimes, we have a decorator that could accept zero arguments,
- * but is receiving too many arguments as part of the decorator invocation.
- * In those cases, a user may have meant to *call* the expression before using it as a decorator.
+ * Returns true iff the JSX element name would be a valid JS identifier, ignoring restrictions about keywords not being identifiers
*/
- function isPotentiallyUncalledDecorator(decorator, signatures) {
- return signatures.length && ts.every(signatures, function (signature) {
- return signature.minArgumentCount === 0 &&
- !signatureHasRestParameter(signature) &&
- signature.parameters.length < getDecoratorArgumentCount(decorator, signature);
- });
- }
- function resolveSignature(node, candidatesOutArray, checkMode) {
- switch (node.kind) {
- case 196 /* CallExpression */:
- return resolveCallExpression(node, candidatesOutArray, checkMode);
- case 197 /* NewExpression */:
- return resolveNewExpression(node, candidatesOutArray, checkMode);
- case 198 /* TaggedTemplateExpression */:
- return resolveTaggedTemplateExpression(node, candidatesOutArray, checkMode);
- case 157 /* Decorator */:
- return resolveDecorator(node, candidatesOutArray, checkMode);
- case 268 /* JsxOpeningElement */:
- case 267 /* JsxSelfClosingElement */:
- return resolveJsxOpeningLikeElement(node, candidatesOutArray, checkMode);
- }
- throw ts.Debug.assertNever(node, "Branch in 'resolveSignature' should be unreachable.");
+ function isUnhyphenatedJsxName(name) {
+ // - is the only character supported in JSX attribute names that isn't valid in JavaScript identifiers
+ return !ts.stringContains(name, "-");
}
/**
- * Resolve a signature of a given call-like expression.
- * @param node a call-like expression to try resolve a signature for
- * @param candidatesOutArray an array of signature to be filled in by the function. It is passed by signature help in the language service;
- * the function will fill it up with appropriate candidate signatures
- * @return a signature of the call-like expression or undefined if one can't be found
+ * Returns true iff React would emit this tag name as a string rather than an identifier or qualified name
*/
- function getResolvedSignature(node, candidatesOutArray, checkMode) {
- var links = getNodeLinks(node);
- // If getResolvedSignature has already been called, we will have cached the resolvedSignature.
- // However, it is possible that either candidatesOutArray was not passed in the first time,
- // or that a different candidatesOutArray was passed in. Therefore, we need to redo the work
- // to correctly fill the candidatesOutArray.
- var cached = links.resolvedSignature;
- if (cached && cached !== resolvingSignature && !candidatesOutArray) {
- return cached;
- }
- links.resolvedSignature = resolvingSignature;
- var result = resolveSignature(node, candidatesOutArray, checkMode || 0 /* Normal */);
- // When CheckMode.SkipGenericFunctions is set we use resolvingSignature to indicate that call
- // resolution should be deferred.
- if (result !== resolvingSignature) {
- // If signature resolution originated in control flow type analysis (for example to compute the
- // assigned type in a flow assignment) we don't cache the result as it may be based on temporary
- // types from the control flow analysis.
- links.resolvedSignature = flowLoopStart === flowLoopCount ? result : cached;
- }
- return result;
+ function isJsxIntrinsicIdentifier(tagName) {
+ return tagName.kind === 78 /* Identifier */ && ts.isIntrinsicJsxName(tagName.escapedText);
+ }
+ function checkJsxAttribute(node, checkMode) {
+ return node.initializer
+ ? checkExpressionForMutableLocation(node.initializer, checkMode)
+ : trueType; // is sugar for
}
/**
- * Indicates whether a declaration can be treated as a constructor in a JavaScript
- * file.
+ * Get attributes type of the JSX opening-like element. The result is from resolving "attributes" property of the opening-like element.
+ *
+ * @param openingLikeElement a JSX opening-like element
+ * @param filter a function to remove attributes that will not participate in checking whether attributes are assignable
+ * @return an anonymous type (similar to the one returned by checkObjectLiteral) in which its properties are attributes property.
+ * @remarks Because this function calls getSpreadType, it needs to use the same checks as checkObjectLiteral,
+ * which also calls getSpreadType.
*/
- function isJSConstructor(node) {
- if (!node || !ts.isInJSFile(node)) {
- return false;
+ function createJsxAttributesTypeFromAttributesProperty(openingLikeElement, checkMode) {
+ var attributes = openingLikeElement.attributes;
+ var allAttributesTable = strictNullChecks ? ts.createSymbolTable() : undefined;
+ var attributesTable = ts.createSymbolTable();
+ var spread = emptyJsxObjectType;
+ var hasSpreadAnyType = false;
+ var typeToIntersect;
+ var explicitlySpecifyChildrenAttribute = false;
+ var objectFlags = 4096 /* JsxAttributes */;
+ var jsxChildrenPropertyName = getJsxElementChildrenPropertyName(getJsxNamespaceAt(openingLikeElement));
+ for (var _i = 0, _a = attributes.properties; _i < _a.length; _i++) {
+ var attributeDecl = _a[_i];
+ var member = attributeDecl.symbol;
+ if (ts.isJsxAttribute(attributeDecl)) {
+ var exprType = checkJsxAttribute(attributeDecl, checkMode);
+ objectFlags |= ts.getObjectFlags(exprType) & 3670016 /* PropagatingFlags */;
+ var attributeSymbol = createSymbol(4 /* Property */ | member.flags, member.escapedName);
+ attributeSymbol.declarations = member.declarations;
+ attributeSymbol.parent = member.parent;
+ if (member.valueDeclaration) {
+ attributeSymbol.valueDeclaration = member.valueDeclaration;
+ }
+ attributeSymbol.type = exprType;
+ attributeSymbol.target = member;
+ attributesTable.set(attributeSymbol.escapedName, attributeSymbol);
+ allAttributesTable === null || allAttributesTable === void 0 ? void 0 : allAttributesTable.set(attributeSymbol.escapedName, attributeSymbol);
+ if (attributeDecl.name.escapedText === jsxChildrenPropertyName) {
+ explicitlySpecifyChildrenAttribute = true;
+ }
+ }
+ else {
+ ts.Debug.assert(attributeDecl.kind === 282 /* JsxSpreadAttribute */);
+ if (attributesTable.size > 0) {
+ spread = getSpreadType(spread, createJsxAttributesType(), attributes.symbol, objectFlags, /*readonly*/ false);
+ attributesTable = ts.createSymbolTable();
+ }
+ var exprType = getReducedType(checkExpressionCached(attributeDecl.expression, checkMode));
+ if (isTypeAny(exprType)) {
+ hasSpreadAnyType = true;
+ }
+ if (isValidSpreadType(exprType)) {
+ spread = getSpreadType(spread, exprType, attributes.symbol, objectFlags, /*readonly*/ false);
+ if (allAttributesTable) {
+ checkSpreadPropOverrides(exprType, allAttributesTable, attributeDecl);
+ }
+ }
+ else {
+ typeToIntersect = typeToIntersect ? getIntersectionType([typeToIntersect, exprType]) : exprType;
+ }
+ }
}
- var func = ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) ? node :
- ts.isVariableDeclaration(node) && node.initializer && ts.isFunctionExpression(node.initializer) ? node.initializer :
- undefined;
- if (func) {
- // If the node has a @class tag, treat it like a constructor.
- if (ts.getJSDocClassTag(node))
- return true;
- // If the symbol of the node has members, treat it like a constructor.
- var symbol = getSymbolOfNode(func);
- return !!symbol && ts.hasEntries(symbol.members);
+ if (!hasSpreadAnyType) {
+ if (attributesTable.size > 0) {
+ spread = getSpreadType(spread, createJsxAttributesType(), attributes.symbol, objectFlags, /*readonly*/ false);
+ }
}
- return false;
- }
- function mergeJSSymbols(target, source) {
- if (source) {
- var links = getSymbolLinks(source);
- if (!links.inferredClassSymbol || !links.inferredClassSymbol.has("" + getSymbolId(target))) {
- var inferred = ts.isTransientSymbol(target) ? target : cloneSymbol(target);
- inferred.exports = inferred.exports || ts.createSymbolTable();
- inferred.members = inferred.members || ts.createSymbolTable();
- inferred.flags |= source.flags & 32 /* Class */;
- if (ts.hasEntries(source.exports)) {
- mergeSymbolTable(inferred.exports, source.exports);
- }
- if (ts.hasEntries(source.members)) {
- mergeSymbolTable(inferred.members, source.members);
+ // Handle children attribute
+ var parent = openingLikeElement.parent.kind === 273 /* JsxElement */ ? openingLikeElement.parent : undefined;
+ // We have to check that openingElement of the parent is the one we are visiting as this may not be true for selfClosingElement
+ if (parent && parent.openingElement === openingLikeElement && parent.children.length > 0) {
+ var childrenTypes = checkJsxChildren(parent, checkMode);
+ if (!hasSpreadAnyType && jsxChildrenPropertyName && jsxChildrenPropertyName !== "") {
+ // Error if there is a attribute named "children" explicitly specified and children element.
+ // This is because children element will overwrite the value from attributes.
+ // Note: we will not warn "children" attribute overwritten if "children" attribute is specified in object spread.
+ if (explicitlySpecifyChildrenAttribute) {
+ error(attributes, ts.Diagnostics._0_are_specified_twice_The_attribute_named_0_will_be_overwritten, ts.unescapeLeadingUnderscores(jsxChildrenPropertyName));
}
- (links.inferredClassSymbol || (links.inferredClassSymbol = ts.createMap())).set("" + getSymbolId(inferred), inferred);
- return inferred;
+ var contextualType = getApparentTypeOfContextualType(openingLikeElement.attributes);
+ var childrenContextualType = contextualType && getTypeOfPropertyOfContextualType(contextualType, jsxChildrenPropertyName);
+ // If there are children in the body of JSX element, create dummy attribute "children" with the union of children types so that it will pass the attribute checking process
+ var childrenPropSymbol = createSymbol(4 /* Property */, jsxChildrenPropertyName);
+ childrenPropSymbol.type = childrenTypes.length === 1 ? childrenTypes[0] :
+ childrenContextualType && forEachType(childrenContextualType, isTupleLikeType) ? createTupleType(childrenTypes) :
+ createArrayType(getUnionType(childrenTypes));
+ // Fake up a property declaration for the children
+ childrenPropSymbol.valueDeclaration = ts.factory.createPropertySignature(/*modifiers*/ undefined, ts.unescapeLeadingUnderscores(jsxChildrenPropertyName), /*questionToken*/ undefined, /*type*/ undefined);
+ ts.setParent(childrenPropSymbol.valueDeclaration, attributes);
+ childrenPropSymbol.valueDeclaration.symbol = childrenPropSymbol;
+ var childPropMap = ts.createSymbolTable();
+ childPropMap.set(jsxChildrenPropertyName, childrenPropSymbol);
+ spread = getSpreadType(spread, createAnonymousType(attributes.symbol, childPropMap, ts.emptyArray, ts.emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined), attributes.symbol, objectFlags, /*readonly*/ false);
}
- return links.inferredClassSymbol.get("" + getSymbolId(target));
}
- }
- function getAssignedClassSymbol(decl) {
- var assignmentSymbol = decl && decl.parent &&
- (ts.isFunctionDeclaration(decl) && getSymbolOfNode(decl) ||
- ts.isBinaryExpression(decl.parent) && getSymbolOfNode(decl.parent.left) ||
- ts.isVariableDeclaration(decl.parent) && getSymbolOfNode(decl.parent));
- var prototype = assignmentSymbol && assignmentSymbol.exports && assignmentSymbol.exports.get("prototype");
- var init = prototype && prototype.valueDeclaration && getAssignedJSPrototype(prototype.valueDeclaration);
- return init ? getSymbolOfNode(init) : undefined;
- }
- function getAssignedJSPrototype(node) {
- if (!node.parent) {
- return false;
+ if (hasSpreadAnyType) {
+ return anyType;
}
- var parent = node.parent;
- while (parent && parent.kind === 194 /* PropertyAccessExpression */) {
- parent = parent.parent;
+ if (typeToIntersect && spread !== emptyJsxObjectType) {
+ return getIntersectionType([typeToIntersect, spread]);
}
- if (parent && ts.isBinaryExpression(parent) && ts.isPrototypeAccess(parent.left) && parent.operatorToken.kind === 62 /* EqualsToken */) {
- var right = ts.getInitializerOfBinaryExpression(parent);
- return ts.isObjectLiteralExpression(right) && right;
+ return typeToIntersect || (spread === emptyJsxObjectType ? createJsxAttributesType() : spread);
+ /**
+ * Create anonymous type from given attributes symbol table.
+ * @param symbol a symbol of JsxAttributes containing attributes corresponding to attributesTable
+ * @param attributesTable a symbol table of attributes property
+ */
+ function createJsxAttributesType() {
+ objectFlags |= freshObjectLiteralFlag;
+ var result = createAnonymousType(attributes.symbol, attributesTable, ts.emptyArray, ts.emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined);
+ result.objectFlags |= objectFlags | 128 /* ObjectLiteral */ | 1048576 /* ContainsObjectOrArrayLiteral */;
+ return result;
}
}
- /**
- * Syntactically and semantically checks a call or new expression.
- * @param node The call/new expression to be checked.
- * @returns On success, the expression's signature's return type. On failure, anyType.
- */
- function checkCallExpression(node, checkMode) {
- if (!checkGrammarTypeArguments(node, node.typeArguments))
- checkGrammarArguments(node.arguments);
- var signature = getResolvedSignature(node, /*candidatesOutArray*/ undefined, checkMode);
- if (signature === resolvingSignature) {
- // CheckMode.SkipGenericFunctions is enabled and this is a call to a generic function that
- // returns a function type. We defer checking and return nonInferrableType.
- return nonInferrableType;
+ function checkJsxChildren(node, checkMode) {
+ var childrenTypes = [];
+ for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
+ var child = _a[_i];
+ // In React, JSX text that contains only whitespaces will be ignored so we don't want to type-check that
+ // because then type of children property will have constituent of string type.
+ if (child.kind === 11 /* JsxText */) {
+ if (!child.containsOnlyTriviaWhiteSpaces) {
+ childrenTypes.push(stringType);
+ }
+ }
+ else if (child.kind === 283 /* JsxExpression */ && !child.expression) {
+ continue; // empty jsx expressions don't *really* count as present children
+ }
+ else {
+ childrenTypes.push(checkExpressionForMutableLocation(child, checkMode));
+ }
}
- if (node.expression.kind === 102 /* SuperKeyword */) {
- return voidType;
+ return childrenTypes;
+ }
+ function checkSpreadPropOverrides(type, props, spread) {
+ for (var _i = 0, _a = getPropertiesOfType(type); _i < _a.length; _i++) {
+ var right = _a[_i];
+ var left = props.get(right.escapedName);
+ var rightType = getTypeOfSymbol(right);
+ if (left && !maybeTypeOfKind(rightType, 98304 /* Nullable */) && !(maybeTypeOfKind(rightType, 3 /* AnyOrUnknown */) && right.flags & 16777216 /* Optional */)) {
+ var diagnostic = error(left.valueDeclaration, ts.Diagnostics._0_is_specified_more_than_once_so_this_usage_will_be_overwritten, ts.unescapeLeadingUnderscores(left.escapedName));
+ ts.addRelatedInfo(diagnostic, ts.createDiagnosticForNode(spread, ts.Diagnostics.This_spread_always_overwrites_this_property));
+ }
}
- if (node.kind === 197 /* NewExpression */) {
- var declaration = signature.declaration;
- if (declaration &&
- declaration.kind !== 162 /* Constructor */ &&
- declaration.kind !== 166 /* ConstructSignature */ &&
- declaration.kind !== 171 /* ConstructorType */ &&
- !ts.isJSDocConstructSignature(declaration) &&
- !isJSConstructor(declaration)) {
- // When resolved signature is a call signature (and not a construct signature) the result type is any
+ }
+ /**
+ * Check attributes property of opening-like element. This function is called during chooseOverload to get call signature of a JSX opening-like element.
+ * (See "checkApplicableSignatureForJsxOpeningLikeElement" for how the function is used)
+ * @param node a JSXAttributes to be resolved of its type
+ */
+ function checkJsxAttributes(node, checkMode) {
+ return createJsxAttributesTypeFromAttributesProperty(node.parent, checkMode);
+ }
+ function getJsxType(name, location) {
+ var namespace = getJsxNamespaceAt(location);
+ var exports = namespace && getExportsOfSymbol(namespace);
+ var typeSymbol = exports && getSymbol(exports, name, 788968 /* Type */);
+ return typeSymbol ? getDeclaredTypeOfSymbol(typeSymbol) : errorType;
+ }
+ /**
+ * Looks up an intrinsic tag name and returns a symbol that either points to an intrinsic
+ * property (in which case nodeLinks.jsxFlags will be IntrinsicNamedElement) or an intrinsic
+ * string index signature (in which case nodeLinks.jsxFlags will be IntrinsicIndexedElement).
+ * May also return unknownSymbol if both of these lookups fail.
+ */
+ function getIntrinsicTagSymbol(node) {
+ var links = getNodeLinks(node);
+ if (!links.resolvedSymbol) {
+ var intrinsicElementsType = getJsxType(JsxNames.IntrinsicElements, node);
+ if (intrinsicElementsType !== errorType) {
+ // Property case
+ if (!ts.isIdentifier(node.tagName))
+ return ts.Debug.fail();
+ var intrinsicProp = getPropertyOfType(intrinsicElementsType, node.tagName.escapedText);
+ if (intrinsicProp) {
+ links.jsxFlags |= 1 /* IntrinsicNamedElement */;
+ return links.resolvedSymbol = intrinsicProp;
+ }
+ // Intrinsic string indexer case
+ var indexSignatureType = getIndexTypeOfType(intrinsicElementsType, 0 /* String */);
+ if (indexSignatureType) {
+ links.jsxFlags |= 2 /* IntrinsicIndexedElement */;
+ return links.resolvedSymbol = intrinsicElementsType.symbol;
+ }
+ // Wasn't found
+ error(node, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.idText(node.tagName), "JSX." + JsxNames.IntrinsicElements);
+ return links.resolvedSymbol = unknownSymbol;
+ }
+ else {
if (noImplicitAny) {
- error(node, ts.Diagnostics.new_expression_whose_target_lacks_a_construct_signature_implicitly_has_an_any_type);
+ error(node, ts.Diagnostics.JSX_element_implicitly_has_type_any_because_no_interface_JSX_0_exists, ts.unescapeLeadingUnderscores(JsxNames.IntrinsicElements));
}
- return anyType;
+ return links.resolvedSymbol = unknownSymbol;
}
}
- // In JavaScript files, calls to any identifier 'require' are treated as external module imports
- if (ts.isInJSFile(node) && isCommonJsRequire(node)) {
- return resolveExternalModuleTypeByLiteral(node.arguments[0]);
+ return links.resolvedSymbol;
+ }
+ function getJsxNamespaceContainerForImplicitImport(location) {
+ var file = location && ts.getSourceFileOfNode(location);
+ var links = file && getNodeLinks(file);
+ if (links && links.jsxImplicitImportContainer === false) {
+ return undefined;
}
- var returnType = getReturnTypeOfSignature(signature);
- // Treat any call to the global 'Symbol' function that is part of a const variable or readonly property
- // as a fresh unique symbol literal type.
- if (returnType.flags & 12288 /* ESSymbolLike */ && isSymbolOrSymbolForCall(node)) {
- return getESSymbolLikeTypeForNode(ts.walkUpParenthesizedExpressions(node.parent));
+ if (links && links.jsxImplicitImportContainer) {
+ return links.jsxImplicitImportContainer;
}
- if (node.kind === 196 /* CallExpression */ && node.parent.kind === 226 /* ExpressionStatement */ &&
- returnType.flags & 16384 /* Void */ && getTypePredicateOfSignature(signature)) {
- if (!ts.isDottedName(node.expression)) {
- error(node.expression, ts.Diagnostics.Assertions_require_the_call_target_to_be_an_identifier_or_qualified_name);
+ var runtimeImportSpecifier = ts.getJSXRuntimeImport(ts.getJSXImplicitImportBase(compilerOptions, file), compilerOptions);
+ if (!runtimeImportSpecifier) {
+ return undefined;
+ }
+ var isClassic = ts.getEmitModuleResolutionKind(compilerOptions) === ts.ModuleResolutionKind.Classic;
+ var errorMessage = isClassic
+ ? ts.Diagnostics.Cannot_find_module_0_Did_you_mean_to_set_the_moduleResolution_option_to_node_or_to_add_aliases_to_the_paths_option
+ : ts.Diagnostics.Cannot_find_module_0_or_its_corresponding_type_declarations;
+ var mod = resolveExternalModule(location, runtimeImportSpecifier, errorMessage, location);
+ var result = mod && mod !== unknownSymbol ? getMergedSymbol(resolveSymbol(mod)) : undefined;
+ if (links) {
+ links.jsxImplicitImportContainer = result || false;
+ }
+ return result;
+ }
+ function getJsxNamespaceAt(location) {
+ var links = location && getNodeLinks(location);
+ if (links && links.jsxNamespace) {
+ return links.jsxNamespace;
+ }
+ if (!links || links.jsxNamespace !== false) {
+ var resolvedNamespace = getJsxNamespaceContainerForImplicitImport(location);
+ if (!resolvedNamespace || resolvedNamespace === unknownSymbol) {
+ var namespaceName = getJsxNamespace(location);
+ resolvedNamespace = resolveName(location, namespaceName, 1920 /* Namespace */, /*diagnosticMessage*/ undefined, namespaceName, /*isUse*/ false);
}
- else if (!getEffectsSignature(node)) {
- var diagnostic = error(node.expression, ts.Diagnostics.Assertions_require_every_name_in_the_call_target_to_be_declared_with_an_explicit_type_annotation);
- getTypeOfDottedName(node.expression, diagnostic);
+ if (resolvedNamespace) {
+ var candidate = resolveSymbol(getSymbol(getExportsOfSymbol(resolveSymbol(resolvedNamespace)), JsxNames.JSX, 1920 /* Namespace */));
+ if (candidate && candidate !== unknownSymbol) {
+ if (links) {
+ links.jsxNamespace = candidate;
+ }
+ return candidate;
+ }
+ }
+ if (links) {
+ links.jsxNamespace = false;
}
}
- if (ts.isInJSFile(node)) {
- var decl = ts.getDeclarationOfExpando(node);
- if (decl) {
- var jsSymbol = getSymbolOfNode(decl);
- if (jsSymbol && ts.hasEntries(jsSymbol.exports)) {
- var jsAssignmentType = createAnonymousType(jsSymbol, jsSymbol.exports, ts.emptyArray, ts.emptyArray, undefined, undefined);
- jsAssignmentType.objectFlags |= 16384 /* JSLiteral */;
- return getIntersectionType([returnType, jsAssignmentType]);
- }
+ // JSX global fallback
+ var s = resolveSymbol(getGlobalSymbol(JsxNames.JSX, 1920 /* Namespace */, /*diagnosticMessage*/ undefined));
+ if (s === unknownSymbol) {
+ return undefined; // TODO: GH#18217
+ }
+ return s; // TODO: GH#18217
+ }
+ /**
+ * Look into JSX namespace and then look for container with matching name as nameOfAttribPropContainer.
+ * Get a single property from that container if existed. Report an error if there are more than one property.
+ *
+ * @param nameOfAttribPropContainer a string of value JsxNames.ElementAttributesPropertyNameContainer or JsxNames.ElementChildrenAttributeNameContainer
+ * if other string is given or the container doesn't exist, return undefined.
+ */
+ function getNameFromJsxElementAttributesContainer(nameOfAttribPropContainer, jsxNamespace) {
+ // JSX.ElementAttributesProperty | JSX.ElementChildrenAttribute [symbol]
+ var jsxElementAttribPropInterfaceSym = jsxNamespace && getSymbol(jsxNamespace.exports, nameOfAttribPropContainer, 788968 /* Type */);
+ // JSX.ElementAttributesProperty | JSX.ElementChildrenAttribute [type]
+ var jsxElementAttribPropInterfaceType = jsxElementAttribPropInterfaceSym && getDeclaredTypeOfSymbol(jsxElementAttribPropInterfaceSym);
+ // The properties of JSX.ElementAttributesProperty | JSX.ElementChildrenAttribute
+ var propertiesOfJsxElementAttribPropInterface = jsxElementAttribPropInterfaceType && getPropertiesOfType(jsxElementAttribPropInterfaceType);
+ if (propertiesOfJsxElementAttribPropInterface) {
+ // Element Attributes has zero properties, so the element attributes type will be the class instance type
+ if (propertiesOfJsxElementAttribPropInterface.length === 0) {
+ return "";
+ }
+ // Element Attributes has one property, so the element attributes type will be the type of the corresponding
+ // property of the class instance type
+ else if (propertiesOfJsxElementAttribPropInterface.length === 1) {
+ return propertiesOfJsxElementAttribPropInterface[0].escapedName;
+ }
+ else if (propertiesOfJsxElementAttribPropInterface.length > 1) {
+ // More than one property on ElementAttributesProperty is an error
+ error(jsxElementAttribPropInterfaceSym.declarations[0], ts.Diagnostics.The_global_type_JSX_0_may_not_have_more_than_one_property, ts.unescapeLeadingUnderscores(nameOfAttribPropContainer));
}
}
- return returnType;
+ return undefined;
}
- function isSymbolOrSymbolForCall(node) {
- if (!ts.isCallExpression(node))
- return false;
- var left = node.expression;
- if (ts.isPropertyAccessExpression(left) && left.name.escapedText === "for") {
- left = left.expression;
+ function getJsxLibraryManagedAttributes(jsxNamespace) {
+ // JSX.LibraryManagedAttributes [symbol]
+ return jsxNamespace && getSymbol(jsxNamespace.exports, JsxNames.LibraryManagedAttributes, 788968 /* Type */);
+ }
+ /// e.g. "props" for React.d.ts,
+ /// or 'undefined' if ElementAttributesProperty doesn't exist (which means all
+ /// non-intrinsic elements' attributes type is 'any'),
+ /// or '' if it has 0 properties (which means every
+ /// non-intrinsic elements' attributes type is the element instance type)
+ function getJsxElementPropertiesName(jsxNamespace) {
+ return getNameFromJsxElementAttributesContainer(JsxNames.ElementAttributesPropertyNameContainer, jsxNamespace);
+ }
+ function getJsxElementChildrenPropertyName(jsxNamespace) {
+ return getNameFromJsxElementAttributesContainer(JsxNames.ElementChildrenAttributeNameContainer, jsxNamespace);
+ }
+ function getUninstantiatedJsxSignaturesOfType(elementType, caller) {
+ if (elementType.flags & 4 /* String */) {
+ return [anySignature];
}
- if (!ts.isIdentifier(left) || left.escapedText !== "Symbol") {
- return false;
+ else if (elementType.flags & 128 /* StringLiteral */) {
+ var intrinsicType = getIntrinsicAttributesTypeFromStringLiteralType(elementType, caller);
+ if (!intrinsicType) {
+ error(caller, ts.Diagnostics.Property_0_does_not_exist_on_type_1, elementType.value, "JSX." + JsxNames.IntrinsicElements);
+ return ts.emptyArray;
+ }
+ else {
+ var fakeSignature = createSignatureForJSXIntrinsic(caller, intrinsicType);
+ return [fakeSignature];
+ }
}
- // make sure `Symbol` is the global symbol
- var globalESSymbol = getGlobalESSymbolConstructorSymbol(/*reportErrors*/ false);
- if (!globalESSymbol) {
- return false;
+ var apparentElemType = getApparentType(elementType);
+ // Resolve the signatures, preferring constructor
+ var signatures = getSignaturesOfType(apparentElemType, 1 /* Construct */);
+ if (signatures.length === 0) {
+ // No construct signatures, try call signatures
+ signatures = getSignaturesOfType(apparentElemType, 0 /* Call */);
}
- return globalESSymbol === resolveName(left, "Symbol", 111551 /* Value */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ false);
+ if (signatures.length === 0 && apparentElemType.flags & 1048576 /* Union */) {
+ // If each member has some combination of new/call signatures; make a union signature list for those
+ signatures = getUnionSignatures(ts.map(apparentElemType.types, function (t) { return getUninstantiatedJsxSignaturesOfType(t, caller); }));
+ }
+ return signatures;
}
- function checkImportCallExpression(node) {
- // Check grammar of dynamic import
- if (!checkGrammarArguments(node.arguments))
- checkGrammarImportCallExpression(node);
- if (node.arguments.length === 0) {
- return createPromiseReturnType(node, anyType);
+ function getIntrinsicAttributesTypeFromStringLiteralType(type, location) {
+ // If the elemType is a stringLiteral type, we can then provide a check to make sure that the string literal type is one of the Jsx intrinsic element type
+ // For example:
+ // var CustomTag: "h1" = "h1";
+ // Hello World
+ var intrinsicElementsType = getJsxType(JsxNames.IntrinsicElements, location);
+ if (intrinsicElementsType !== errorType) {
+ var stringLiteralTypeName = type.value;
+ var intrinsicProp = getPropertyOfType(intrinsicElementsType, ts.escapeLeadingUnderscores(stringLiteralTypeName));
+ if (intrinsicProp) {
+ return getTypeOfSymbol(intrinsicProp);
+ }
+ var indexSignatureType = getIndexTypeOfType(intrinsicElementsType, 0 /* String */);
+ if (indexSignatureType) {
+ return indexSignatureType;
+ }
+ return undefined;
}
- var specifier = node.arguments[0];
- var specifierType = checkExpressionCached(specifier);
- // Even though multiple arguments is grammatically incorrect, type-check extra arguments for completion
- for (var i = 1; i < node.arguments.length; ++i) {
- checkExpressionCached(node.arguments[i]);
+ // If we need to report an error, we already done so here. So just return any to prevent any more error downstream
+ return anyType;
+ }
+ function checkJsxReturnAssignableToAppropriateBound(refKind, elemInstanceType, openingLikeElement) {
+ if (refKind === 1 /* Function */) {
+ var sfcReturnConstraint = getJsxStatelessElementTypeAt(openingLikeElement);
+ if (sfcReturnConstraint) {
+ checkTypeRelatedTo(elemInstanceType, sfcReturnConstraint, assignableRelation, openingLikeElement.tagName, ts.Diagnostics.Its_return_type_0_is_not_a_valid_JSX_element, generateInitialErrorChain);
+ }
}
- if (specifierType.flags & 32768 /* Undefined */ || specifierType.flags & 65536 /* Null */ || !isTypeAssignableTo(specifierType, stringType)) {
- error(specifier, ts.Diagnostics.Dynamic_import_s_specifier_must_be_of_type_string_but_here_has_type_0, typeToString(specifierType));
+ else if (refKind === 0 /* Component */) {
+ var classConstraint = getJsxElementClassTypeAt(openingLikeElement);
+ if (classConstraint) {
+ // Issue an error if this return type isn't assignable to JSX.ElementClass, failing that
+ checkTypeRelatedTo(elemInstanceType, classConstraint, assignableRelation, openingLikeElement.tagName, ts.Diagnostics.Its_instance_type_0_is_not_a_valid_JSX_element, generateInitialErrorChain);
+ }
}
- // resolveExternalModuleName will return undefined if the moduleReferenceExpression is not a string literal
- var moduleSymbol = resolveExternalModuleName(node, specifier);
- if (moduleSymbol) {
- var esModuleSymbol = resolveESModuleSymbol(moduleSymbol, specifier, /*dontRecursivelyResolve*/ true, /*suppressUsageError*/ false);
- if (esModuleSymbol) {
- return createPromiseReturnType(node, getTypeWithSyntheticDefaultImportType(getTypeOfSymbol(esModuleSymbol), esModuleSymbol, moduleSymbol));
+ else { // Mixed
+ var sfcReturnConstraint = getJsxStatelessElementTypeAt(openingLikeElement);
+ var classConstraint = getJsxElementClassTypeAt(openingLikeElement);
+ if (!sfcReturnConstraint || !classConstraint) {
+ return;
}
+ var combined = getUnionType([sfcReturnConstraint, classConstraint]);
+ checkTypeRelatedTo(elemInstanceType, combined, assignableRelation, openingLikeElement.tagName, ts.Diagnostics.Its_element_type_0_is_not_a_valid_JSX_element, generateInitialErrorChain);
+ }
+ function generateInitialErrorChain() {
+ var componentName = ts.getTextOfNode(openingLikeElement.tagName);
+ return ts.chainDiagnosticMessages(/* details */ undefined, ts.Diagnostics._0_cannot_be_used_as_a_JSX_component, componentName);
}
- return createPromiseReturnType(node, anyType);
}
- function getTypeWithSyntheticDefaultImportType(type, symbol, originalSymbol) {
- if (allowSyntheticDefaultImports && type && type !== errorType) {
- var synthType = type;
- if (!synthType.syntheticType) {
- var file = ts.find(originalSymbol.declarations, ts.isSourceFile);
- var hasSyntheticDefault = canHaveSyntheticDefault(file, originalSymbol, /*dontResolveAlias*/ false);
- if (hasSyntheticDefault) {
- var memberTable = ts.createSymbolTable();
- var newSymbol = createSymbol(2097152 /* Alias */, "default" /* Default */);
- newSymbol.nameType = getLiteralType("default");
- newSymbol.target = resolveSymbol(symbol);
- memberTable.set("default" /* Default */, newSymbol);
- var anonymousSymbol = createSymbol(2048 /* TypeLiteral */, "__type" /* Type */);
- var defaultContainingObject = createAnonymousType(anonymousSymbol, memberTable, ts.emptyArray, ts.emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined);
- anonymousSymbol.type = defaultContainingObject;
- synthType.syntheticType = isValidSpreadType(type) ? getSpreadType(type, defaultContainingObject, anonymousSymbol, /*objectFlags*/ 0, /*readonly*/ false) : defaultContainingObject;
- }
- else {
- synthType.syntheticType = type;
- }
+ /**
+ * Get attributes type of the given intrinsic opening-like Jsx element by resolving the tag name.
+ * The function is intended to be called from a function which has checked that the opening element is an intrinsic element.
+ * @param node an intrinsic JSX opening-like element
+ */
+ function getIntrinsicAttributesTypeFromJsxOpeningLikeElement(node) {
+ ts.Debug.assert(isJsxIntrinsicIdentifier(node.tagName));
+ var links = getNodeLinks(node);
+ if (!links.resolvedJsxElementAttributesType) {
+ var symbol = getIntrinsicTagSymbol(node);
+ if (links.jsxFlags & 1 /* IntrinsicNamedElement */) {
+ return links.resolvedJsxElementAttributesType = getTypeOfSymbol(symbol);
+ }
+ else if (links.jsxFlags & 2 /* IntrinsicIndexedElement */) {
+ return links.resolvedJsxElementAttributesType =
+ getIndexTypeOfType(getDeclaredTypeOfSymbol(symbol), 0 /* String */);
+ }
+ else {
+ return links.resolvedJsxElementAttributesType = errorType;
}
- return synthType.syntheticType;
}
+ return links.resolvedJsxElementAttributesType;
+ }
+ function getJsxElementClassTypeAt(location) {
+ var type = getJsxType(JsxNames.ElementClass, location);
+ if (type === errorType)
+ return undefined;
return type;
}
- function isCommonJsRequire(node) {
- if (!ts.isRequireCall(node, /*checkArgumentIsStringLiteralLike*/ true)) {
- return false;
- }
- // Make sure require is not a local function
- if (!ts.isIdentifier(node.expression))
- return ts.Debug.fail();
- var resolvedRequire = resolveName(node.expression, node.expression.escapedText, 111551 /* Value */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ true); // TODO: GH#18217
- if (resolvedRequire === requireSymbol) {
- return true;
+ function getJsxElementTypeAt(location) {
+ return getJsxType(JsxNames.Element, location);
+ }
+ function getJsxStatelessElementTypeAt(location) {
+ var jsxElementType = getJsxElementTypeAt(location);
+ if (jsxElementType) {
+ return getUnionType([jsxElementType, nullType]);
}
- // project includes symbol named 'require' - make sure that it is ambient and local non-alias
- if (resolvedRequire.flags & 2097152 /* Alias */) {
- return false;
+ }
+ /**
+ * Returns all the properties of the Jsx.IntrinsicElements interface
+ */
+ function getJsxIntrinsicTagNamesAt(location) {
+ var intrinsics = getJsxType(JsxNames.IntrinsicElements, location);
+ return intrinsics ? getPropertiesOfType(intrinsics) : ts.emptyArray;
+ }
+ function checkJsxPreconditions(errorNode) {
+ // Preconditions for using JSX
+ if ((compilerOptions.jsx || 0 /* None */) === 0 /* None */) {
+ error(errorNode, ts.Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided);
}
- var targetDeclarationKind = resolvedRequire.flags & 16 /* Function */
- ? 244 /* FunctionDeclaration */
- : resolvedRequire.flags & 3 /* Variable */
- ? 242 /* VariableDeclaration */
- : 0 /* Unknown */;
- if (targetDeclarationKind !== 0 /* Unknown */) {
- var decl = ts.getDeclarationOfKind(resolvedRequire, targetDeclarationKind);
- // function/variable declaration should be ambient
- return !!decl && !!(decl.flags & 8388608 /* Ambient */);
+ if (getJsxElementTypeAt(errorNode) === undefined) {
+ if (noImplicitAny) {
+ error(errorNode, ts.Diagnostics.JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist);
+ }
}
- return false;
}
- function checkTaggedTemplateExpression(node) {
- if (!checkGrammarTaggedTemplateChain(node))
- checkGrammarTypeArguments(node, node.typeArguments);
- if (languageVersion < 2 /* ES2015 */) {
- checkExternalEmitHelpers(node, 131072 /* MakeTemplateObject */);
+ function checkJsxOpeningLikeElementOrOpeningFragment(node) {
+ var isNodeOpeningLikeElement = ts.isJsxOpeningLikeElement(node);
+ if (isNodeOpeningLikeElement) {
+ checkGrammarJsxElement(node);
+ }
+ checkJsxPreconditions(node);
+ if (!getJsxNamespaceContainerForImplicitImport(node)) {
+ // The reactNamespace/jsxFactory's root symbol should be marked as 'used' so we don't incorrectly elide its import.
+ // And if there is no reactNamespace/jsxFactory's symbol in scope when targeting React emit, we should issue an error.
+ var jsxFactoryRefErr = diagnostics && compilerOptions.jsx === 2 /* React */ ? ts.Diagnostics.Cannot_find_name_0 : undefined;
+ var jsxFactoryNamespace = getJsxNamespace(node);
+ var jsxFactoryLocation = isNodeOpeningLikeElement ? node.tagName : node;
+ // allow null as jsxFragmentFactory
+ var jsxFactorySym = void 0;
+ if (!(ts.isJsxOpeningFragment(node) && jsxFactoryNamespace === "null")) {
+ jsxFactorySym = resolveName(jsxFactoryLocation, jsxFactoryNamespace, 111551 /* Value */, jsxFactoryRefErr, jsxFactoryNamespace, /*isUse*/ true);
+ }
+ if (jsxFactorySym) {
+ // Mark local symbol as referenced here because it might not have been marked
+ // if jsx emit was not jsxFactory as there wont be error being emitted
+ jsxFactorySym.isReferenced = 67108863 /* All */;
+ // If react/jsxFactory symbol is alias, mark it as refereced
+ if (jsxFactorySym.flags & 2097152 /* Alias */ && !getTypeOnlyAliasDeclaration(jsxFactorySym)) {
+ markAliasSymbolAsReferenced(jsxFactorySym);
+ }
+ }
+ }
+ if (isNodeOpeningLikeElement) {
+ var jsxOpeningLikeNode = node;
+ var sig = getResolvedSignature(jsxOpeningLikeNode);
+ checkDeprecatedSignature(sig, node);
+ checkJsxReturnAssignableToAppropriateBound(getJsxReferenceKind(jsxOpeningLikeNode), getReturnTypeOfSignature(sig), jsxOpeningLikeNode);
}
- return getReturnTypeOfSignature(getResolvedSignature(node));
- }
- function checkAssertion(node) {
- return checkAssertionWorker(node, node.type, node.expression);
}
- function isValidConstAssertionArgument(node) {
- switch (node.kind) {
- case 10 /* StringLiteral */:
- case 14 /* NoSubstitutionTemplateLiteral */:
- case 8 /* NumericLiteral */:
- case 9 /* BigIntLiteral */:
- case 106 /* TrueKeyword */:
- case 91 /* FalseKeyword */:
- case 192 /* ArrayLiteralExpression */:
- case 193 /* ObjectLiteralExpression */:
+ /**
+ * Check if a property with the given name is known anywhere in the given type. In an object type, a property
+ * is considered known if
+ * 1. the object type is empty and the check is for assignability, or
+ * 2. if the object type has index signatures, or
+ * 3. if the property is actually declared in the object type
+ * (this means that 'toString', for example, is not usually a known property).
+ * 4. In a union or intersection type,
+ * a property is considered known if it is known in any constituent type.
+ * @param targetType a type to search a given name in
+ * @param name a property name to search
+ * @param isComparingJsxAttributes a boolean flag indicating whether we are searching in JsxAttributesType
+ */
+ function isKnownProperty(targetType, name, isComparingJsxAttributes) {
+ if (targetType.flags & 524288 /* Object */) {
+ var resolved = resolveStructuredTypeMembers(targetType);
+ if (resolved.stringIndexInfo ||
+ resolved.numberIndexInfo && isNumericLiteralName(name) ||
+ getPropertyOfObjectType(targetType, name) ||
+ isComparingJsxAttributes && !isUnhyphenatedJsxName(name)) {
+ // For JSXAttributes, if the attribute has a hyphenated name, consider that the attribute to be known.
return true;
- case 200 /* ParenthesizedExpression */:
- return isValidConstAssertionArgument(node.expression);
- case 207 /* PrefixUnaryExpression */:
- var op = node.operator;
- var arg = node.operand;
- return op === 40 /* MinusToken */ && (arg.kind === 8 /* NumericLiteral */ || arg.kind === 9 /* BigIntLiteral */) ||
- op === 39 /* PlusToken */ && arg.kind === 8 /* NumericLiteral */;
- case 194 /* PropertyAccessExpression */:
- case 195 /* ElementAccessExpression */:
- var expr = node.expression;
- if (ts.isIdentifier(expr)) {
- var symbol = getSymbolAtLocation(expr);
- if (symbol && symbol.flags & 2097152 /* Alias */) {
- symbol = resolveAlias(symbol);
- }
- return !!(symbol && (symbol.flags & 384 /* Enum */) && getEnumKind(symbol) === 1 /* Literal */);
+ }
+ }
+ else if (targetType.flags & 3145728 /* UnionOrIntersection */ && isExcessPropertyCheckTarget(targetType)) {
+ for (var _i = 0, _a = targetType.types; _i < _a.length; _i++) {
+ var t = _a[_i];
+ if (isKnownProperty(t, name, isComparingJsxAttributes)) {
+ return true;
}
+ }
}
return false;
}
- function checkAssertionWorker(errNode, type, expression, checkMode) {
- var exprType = checkExpression(expression, checkMode);
- if (ts.isConstTypeReference(type)) {
- if (!isValidConstAssertionArgument(expression)) {
- error(expression, ts.Diagnostics.A_const_assertions_can_only_be_applied_to_references_to_enum_members_or_string_number_boolean_array_or_object_literals);
+ function isExcessPropertyCheckTarget(type) {
+ return !!(type.flags & 524288 /* Object */ && !(ts.getObjectFlags(type) & 512 /* ObjectLiteralPatternWithComputedProperties */) ||
+ type.flags & 67108864 /* NonPrimitive */ ||
+ type.flags & 1048576 /* Union */ && ts.some(type.types, isExcessPropertyCheckTarget) ||
+ type.flags & 2097152 /* Intersection */ && ts.every(type.types, isExcessPropertyCheckTarget));
+ }
+ function checkJsxExpression(node, checkMode) {
+ checkGrammarJsxExpression(node);
+ if (node.expression) {
+ var type = checkExpression(node.expression, checkMode);
+ if (node.dotDotDotToken && type !== anyType && !isArrayType(type)) {
+ error(node, ts.Diagnostics.JSX_spread_child_must_be_an_array_type);
}
- return getRegularTypeOfLiteralType(exprType);
+ return type;
}
- checkSourceElement(type);
- exprType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(exprType));
- var targetType = getTypeFromTypeNode(type);
- if (produceDiagnostics && targetType !== errorType) {
- var widenedType = getWidenedType(exprType);
- if (!isTypeComparableTo(targetType, widenedType)) {
- checkTypeComparableTo(exprType, targetType, errNode, ts.Diagnostics.Conversion_of_type_0_to_type_1_may_be_a_mistake_because_neither_type_sufficiently_overlaps_with_the_other_If_this_was_intentional_convert_the_expression_to_unknown_first);
- }
+ else {
+ return errorType;
}
- return targetType;
- }
- function checkNonNullChain(node) {
- var leftType = checkExpression(node.expression);
- var nonOptionalType = getOptionalExpressionType(leftType, node.expression);
- return propagateOptionalTypeMarker(getNonNullableType(nonOptionalType), node, nonOptionalType !== leftType);
}
- function checkNonNullAssertion(node) {
- return node.flags & 32 /* OptionalChain */ ? checkNonNullChain(node) :
- getNonNullableType(checkExpression(node.expression));
+ function getDeclarationNodeFlagsFromSymbol(s) {
+ return s.valueDeclaration ? ts.getCombinedNodeFlags(s.valueDeclaration) : 0;
}
- function checkMetaProperty(node) {
- checkGrammarMetaProperty(node);
- if (node.keywordToken === 99 /* NewKeyword */) {
- return checkNewTargetMetaProperty(node);
+ /**
+ * Return whether this symbol is a member of a prototype somewhere
+ * Note that this is not tracked well within the compiler, so the answer may be incorrect.
+ */
+ function isPrototypeProperty(symbol) {
+ if (symbol.flags & 8192 /* Method */ || ts.getCheckFlags(symbol) & 4 /* SyntheticMethod */) {
+ return true;
}
- if (node.keywordToken === 96 /* ImportKeyword */) {
- return checkImportMetaProperty(node);
+ if (ts.isInJSFile(symbol.valueDeclaration)) {
+ var parent = symbol.valueDeclaration.parent;
+ return parent && ts.isBinaryExpression(parent) &&
+ ts.getAssignmentDeclarationKind(parent) === 3 /* PrototypeProperty */;
}
- return ts.Debug.assertNever(node.keywordToken);
}
- function checkNewTargetMetaProperty(node) {
- var container = ts.getNewTargetContainer(node);
- if (!container) {
- error(node, ts.Diagnostics.Meta_property_0_is_only_allowed_in_the_body_of_a_function_declaration_function_expression_or_constructor, "new.target");
- return errorType;
+ /**
+ * Check whether the requested property access is valid.
+ * Returns true if node is a valid property access, and false otherwise.
+ * @param node The node to be checked.
+ * @param isSuper True if the access is from `super.`.
+ * @param type The type of the object whose property is being accessed. (Not the type of the property.)
+ * @param prop The symbol for the property being accessed.
+ */
+ function checkPropertyAccessibility(node, isSuper, type, prop) {
+ var flags = ts.getDeclarationModifierFlagsFromSymbol(prop);
+ var errorNode = node.kind === 157 /* QualifiedName */ ? node.right : node.kind === 195 /* ImportType */ ? node : node.name;
+ if (isSuper) {
+ // TS 1.0 spec (April 2014): 4.8.2
+ // - In a constructor, instance member function, instance member accessor, or
+ // instance member variable initializer where this references a derived class instance,
+ // a super property access is permitted and must specify a public instance member function of the base class.
+ // - In a static member function or static member accessor
+ // where this references the constructor function object of a derived class,
+ // a super property access is permitted and must specify a public static member function of the base class.
+ if (languageVersion < 2 /* ES2015 */) {
+ if (symbolHasNonMethodDeclaration(prop)) {
+ error(errorNode, ts.Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword);
+ return false;
+ }
+ }
+ if (flags & 128 /* Abstract */) {
+ // A method cannot be accessed in a super property access if the method is abstract.
+ // This error could mask a private property access error. But, a member
+ // cannot simultaneously be private and abstract, so this will trigger an
+ // additional error elsewhere.
+ error(errorNode, ts.Diagnostics.Abstract_method_0_in_class_1_cannot_be_accessed_via_super_expression, symbolToString(prop), typeToString(getDeclaringClass(prop)));
+ return false;
+ }
}
- else if (container.kind === 162 /* Constructor */) {
- var symbol = getSymbolOfNode(container.parent);
- return getTypeOfSymbol(symbol);
+ // Referencing abstract properties within their own constructors is not allowed
+ if ((flags & 128 /* Abstract */) && ts.isThisProperty(node) && symbolHasNonMethodDeclaration(prop)) {
+ var declaringClassDeclaration = ts.getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop));
+ if (declaringClassDeclaration && isNodeUsedDuringClassInitialization(node)) {
+ error(errorNode, ts.Diagnostics.Abstract_property_0_in_class_1_cannot_be_accessed_in_the_constructor, symbolToString(prop), ts.getTextOfIdentifierOrLiteral(declaringClassDeclaration.name)); // TODO: GH#18217
+ return false;
+ }
}
- else {
- var symbol = getSymbolOfNode(container);
- return getTypeOfSymbol(symbol);
+ if (ts.isPropertyAccessExpression(node) && ts.isPrivateIdentifier(node.name)) {
+ if (!ts.getContainingClass(node)) {
+ error(errorNode, ts.Diagnostics.Private_identifiers_are_not_allowed_outside_class_bodies);
+ return false;
+ }
+ return true;
}
- }
- function checkImportMetaProperty(node) {
- if (moduleKind !== ts.ModuleKind.ESNext && moduleKind !== ts.ModuleKind.System) {
- error(node, ts.Diagnostics.The_import_meta_meta_property_is_only_allowed_when_the_module_option_is_esnext_or_system);
+ // Public properties are otherwise accessible.
+ if (!(flags & 24 /* NonPublicAccessibilityModifier */)) {
+ return true;
}
- var file = ts.getSourceFileOfNode(node);
- ts.Debug.assert(!!(file.flags & 2097152 /* PossiblyContainsImportMeta */), "Containing file is missing import meta node flag.");
- ts.Debug.assert(!!file.externalModuleIndicator, "Containing file should be a module.");
- return node.name.escapedText === "meta" ? getGlobalImportMetaType() : errorType;
- }
- function getTypeOfParameter(symbol) {
- var type = getTypeOfSymbol(symbol);
- if (strictNullChecks) {
- var declaration = symbol.valueDeclaration;
- if (declaration && ts.hasInitializer(declaration)) {
- return getOptionalType(type);
+ // Property is known to be private or protected at this point
+ // Private property is accessible if the property is within the declaring class
+ if (flags & 8 /* Private */) {
+ var declaringClassDeclaration = ts.getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop));
+ if (!isNodeWithinClass(node, declaringClassDeclaration)) {
+ error(errorNode, ts.Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(getDeclaringClass(prop)));
+ return false;
+ }
+ return true;
+ }
+ // Property is known to be protected at this point
+ // All protected properties of a supertype are accessible in a super access
+ if (isSuper) {
+ return true;
+ }
+ // Find the first enclosing class that has the declaring classes of the protected constituents
+ // of the property as base classes
+ var enclosingClass = forEachEnclosingClass(node, function (enclosingDeclaration) {
+ var enclosingClass = getDeclaredTypeOfSymbol(getSymbolOfNode(enclosingDeclaration));
+ return isClassDerivedFromDeclaringClasses(enclosingClass, prop) ? enclosingClass : undefined;
+ });
+ // A protected property is accessible if the property is within the declaring class or classes derived from it
+ if (!enclosingClass) {
+ // allow PropertyAccessibility if context is in function with this parameter
+ // static member access is disallow
+ var thisParameter = void 0;
+ if (flags & 32 /* Static */ || !(thisParameter = getThisParameterFromNodeContext(node)) || !thisParameter.type) {
+ error(errorNode, ts.Diagnostics.Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses, symbolToString(prop), typeToString(getDeclaringClass(prop) || type));
+ return false;
}
+ var thisType = getTypeFromTypeNode(thisParameter.type);
+ enclosingClass = ((thisType.flags & 262144 /* TypeParameter */) ? getConstraintOfTypeParameter(thisType) : thisType).target;
}
- return type;
- }
- function getParameterNameAtPosition(signature, pos) {
- var paramCount = signature.parameters.length - (signatureHasRestParameter(signature) ? 1 : 0);
- if (pos < paramCount) {
- return signature.parameters[pos].escapedName;
+ // No further restrictions for static properties
+ if (flags & 32 /* Static */) {
+ return true;
}
- var restParameter = signature.parameters[paramCount] || unknownSymbol;
- var restType = getTypeOfSymbol(restParameter);
- if (isTupleType(restType)) {
- var associatedNames = restType.target.associatedNames;
- var index = pos - paramCount;
- return associatedNames && associatedNames[index] || restParameter.escapedName + "_" + index;
+ if (type.flags & 262144 /* TypeParameter */) {
+ // get the original type -- represented as the type constraint of the 'this' type
+ type = type.isThisType ? getConstraintOfTypeParameter(type) : getBaseConstraintOfType(type); // TODO: GH#18217 Use a different variable that's allowed to be undefined
}
- return restParameter.escapedName;
+ if (!type || !hasBaseType(type, enclosingClass)) {
+ error(errorNode, ts.Diagnostics.Property_0_is_protected_and_only_accessible_through_an_instance_of_class_1, symbolToString(prop), typeToString(enclosingClass));
+ return false;
+ }
+ return true;
}
- function getTypeAtPosition(signature, pos) {
- return tryGetTypeAtPosition(signature, pos) || anyType;
+ function getThisParameterFromNodeContext(node) {
+ var thisContainer = ts.getThisContainer(node, /* includeArrowFunctions */ false);
+ return thisContainer && ts.isFunctionLike(thisContainer) ? ts.getThisParameter(thisContainer) : undefined;
}
- function tryGetTypeAtPosition(signature, pos) {
- var paramCount = signature.parameters.length - (signatureHasRestParameter(signature) ? 1 : 0);
- if (pos < paramCount) {
- return getTypeOfParameter(signature.parameters[pos]);
- }
- if (signatureHasRestParameter(signature)) {
- // We want to return the value undefined for an out of bounds parameter position,
- // so we need to check bounds here before calling getIndexedAccessType (which
- // otherwise would return the type 'undefined').
- var restType = getTypeOfSymbol(signature.parameters[paramCount]);
- var index = pos - paramCount;
- if (!isTupleType(restType) || restType.target.hasRestElement || index < getTypeArguments(restType).length) {
- return getIndexedAccessType(restType, getLiteralType(index));
- }
- }
- return undefined;
+ function symbolHasNonMethodDeclaration(symbol) {
+ return !!forEachProperty(symbol, function (prop) { return !(prop.flags & 8192 /* Method */); });
}
- function getRestTypeAtPosition(source, pos) {
- var paramCount = getParameterCount(source);
- var restType = getEffectiveRestType(source);
- var nonRestCount = paramCount - (restType ? 1 : 0);
- if (restType && pos === nonRestCount) {
- return restType;
- }
- var types = [];
- var names = [];
- for (var i = pos; i < nonRestCount; i++) {
- types.push(getTypeAtPosition(source, i));
- names.push(getParameterNameAtPosition(source, i));
- }
- if (restType) {
- types.push(getIndexedAccessType(restType, numberType));
- names.push(getParameterNameAtPosition(source, nonRestCount));
- }
- var minArgumentCount = getMinArgumentCount(source);
- var minLength = minArgumentCount < pos ? 0 : minArgumentCount - pos;
- return createTupleType(types, minLength, !!restType, /*readonly*/ false, names);
+ function checkNonNullExpression(node) {
+ return checkNonNullType(checkExpression(node), node);
}
- function getParameterCount(signature) {
- var length = signature.parameters.length;
- if (signatureHasRestParameter(signature)) {
- var restType = getTypeOfSymbol(signature.parameters[length - 1]);
- if (isTupleType(restType)) {
- return length + getTypeArguments(restType).length - 1;
- }
- }
- return length;
+ function isNullableType(type) {
+ return !!((strictNullChecks ? getFalsyFlags(type) : type.flags) & 98304 /* Nullable */);
}
- function getMinArgumentCount(signature, strongArityForUntypedJS) {
- if (signatureHasRestParameter(signature)) {
- var restType = getTypeOfSymbol(signature.parameters[signature.parameters.length - 1]);
- if (isTupleType(restType)) {
- var minLength = restType.target.minLength;
- if (minLength > 0) {
- return signature.parameters.length - 1 + minLength;
- }
- }
+ function getNonNullableTypeIfNeeded(type) {
+ return isNullableType(type) ? getNonNullableType(type) : type;
+ }
+ function reportObjectPossiblyNullOrUndefinedError(node, flags) {
+ error(node, flags & 32768 /* Undefined */ ? flags & 65536 /* Null */ ?
+ ts.Diagnostics.Object_is_possibly_null_or_undefined :
+ ts.Diagnostics.Object_is_possibly_undefined :
+ ts.Diagnostics.Object_is_possibly_null);
+ }
+ function reportCannotInvokePossiblyNullOrUndefinedError(node, flags) {
+ error(node, flags & 32768 /* Undefined */ ? flags & 65536 /* Null */ ?
+ ts.Diagnostics.Cannot_invoke_an_object_which_is_possibly_null_or_undefined :
+ ts.Diagnostics.Cannot_invoke_an_object_which_is_possibly_undefined :
+ ts.Diagnostics.Cannot_invoke_an_object_which_is_possibly_null);
+ }
+ function checkNonNullTypeWithReporter(type, node, reportError) {
+ if (strictNullChecks && type.flags & 2 /* Unknown */) {
+ error(node, ts.Diagnostics.Object_is_of_type_unknown);
+ return errorType;
}
- if (!strongArityForUntypedJS && signature.flags & 16 /* IsUntypedSignatureInJSFile */) {
- return 0;
+ var kind = (strictNullChecks ? getFalsyFlags(type) : type.flags) & 98304 /* Nullable */;
+ if (kind) {
+ reportError(node, kind);
+ var t = getNonNullableType(type);
+ return t.flags & (98304 /* Nullable */ | 131072 /* Never */) ? errorType : t;
}
- return signature.minArgumentCount;
+ return type;
}
- function hasEffectiveRestParameter(signature) {
- if (signatureHasRestParameter(signature)) {
- var restType = getTypeOfSymbol(signature.parameters[signature.parameters.length - 1]);
- return !isTupleType(restType) || restType.target.hasRestElement;
- }
- return false;
+ function checkNonNullType(type, node) {
+ return checkNonNullTypeWithReporter(type, node, reportObjectPossiblyNullOrUndefinedError);
}
- function getEffectiveRestType(signature) {
- if (signatureHasRestParameter(signature)) {
- var restType = getTypeOfSymbol(signature.parameters[signature.parameters.length - 1]);
- return isTupleType(restType) ? getRestArrayTypeOfTupleType(restType) : restType;
+ function checkNonNullNonVoidType(type, node) {
+ var nonNullType = checkNonNullType(type, node);
+ if (nonNullType !== errorType && nonNullType.flags & 16384 /* Void */) {
+ error(node, ts.Diagnostics.Object_is_possibly_undefined);
}
- return undefined;
+ return nonNullType;
}
- function getNonArrayRestType(signature) {
- var restType = getEffectiveRestType(signature);
- return restType && !isArrayType(restType) && !isTypeAny(restType) && (getReducedType(restType).flags & 131072 /* Never */) === 0 ? restType : undefined;
+ function checkPropertyAccessExpression(node) {
+ return node.flags & 32 /* OptionalChain */ ? checkPropertyAccessChain(node) :
+ checkPropertyAccessExpressionOrQualifiedName(node, node.expression, checkNonNullExpression(node.expression), node.name);
}
- function getTypeOfFirstParameterOfSignature(signature) {
- return getTypeOfFirstParameterOfSignatureWithFallback(signature, neverType);
+ function checkPropertyAccessChain(node) {
+ var leftType = checkExpression(node.expression);
+ var nonOptionalType = getOptionalExpressionType(leftType, node.expression);
+ return propagateOptionalTypeMarker(checkPropertyAccessExpressionOrQualifiedName(node, node.expression, checkNonNullType(nonOptionalType, node.expression), node.name), node, nonOptionalType !== leftType);
}
- function getTypeOfFirstParameterOfSignatureWithFallback(signature, fallbackType) {
- return signature.parameters.length > 0 ? getTypeAtPosition(signature, 0) : fallbackType;
+ function checkQualifiedName(node) {
+ return checkPropertyAccessExpressionOrQualifiedName(node, node.left, checkNonNullExpression(node.left), node.right);
}
- function inferFromAnnotatedParameters(signature, context, inferenceContext) {
- var len = signature.parameters.length - (signatureHasRestParameter(signature) ? 1 : 0);
- for (var i = 0; i < len; i++) {
- var declaration = signature.parameters[i].valueDeclaration;
- if (declaration.type) {
- var typeNode = ts.getEffectiveTypeAnnotationNode(declaration);
- if (typeNode) {
- inferTypes(inferenceContext.inferences, getTypeFromTypeNode(typeNode), getTypeAtPosition(context, i));
- }
- }
- }
- var restType = getEffectiveRestType(context);
- if (restType && restType.flags & 262144 /* TypeParameter */) {
- // The contextual signature has a generic rest parameter. We first instantiate the contextual
- // signature (without fixing type parameters) and assign types to contextually typed parameters.
- var instantiatedContext = instantiateSignature(context, inferenceContext.nonFixingMapper);
- assignContextualParameterTypes(signature, instantiatedContext);
- // We then infer from a tuple type representing the parameters that correspond to the contextual
- // rest parameter.
- var restPos = getParameterCount(context) - 1;
- inferTypes(inferenceContext.inferences, getRestTypeAtPosition(signature, restPos), restType);
+ function isMethodAccessForCall(node) {
+ while (node.parent.kind === 207 /* ParenthesizedExpression */) {
+ node = node.parent;
}
+ return ts.isCallOrNewExpression(node.parent) && node.parent.expression === node;
}
- function assignContextualParameterTypes(signature, context) {
- signature.typeParameters = context.typeParameters;
- if (context.thisParameter) {
- var parameter = signature.thisParameter;
- if (!parameter || parameter.valueDeclaration && !parameter.valueDeclaration.type) {
- if (!parameter) {
- signature.thisParameter = createSymbolWithType(context.thisParameter, /*type*/ undefined);
- }
- assignParameterType(signature.thisParameter, getTypeOfSymbol(context.thisParameter));
- }
- }
- var len = signature.parameters.length - (signatureHasRestParameter(signature) ? 1 : 0);
- for (var i = 0; i < len; i++) {
- var parameter = signature.parameters[i];
- if (!ts.getEffectiveTypeAnnotationNode(parameter.valueDeclaration)) {
- var contextualParameterType = tryGetTypeAtPosition(context, i);
- assignParameterType(parameter, contextualParameterType);
- }
- }
- if (signatureHasRestParameter(signature)) {
- // parameter might be a transient symbol generated by use of `arguments` in the function body.
- var parameter = ts.last(signature.parameters);
- if (ts.isTransientSymbol(parameter) || !ts.getEffectiveTypeAnnotationNode(parameter.valueDeclaration)) {
- var contextualParameterType = getRestTypeAtPosition(context, len);
- assignParameterType(parameter, contextualParameterType);
+ // Lookup the private identifier lexically.
+ function lookupSymbolForPrivateIdentifierDeclaration(propName, location) {
+ for (var containingClass = ts.getContainingClass(location); !!containingClass; containingClass = ts.getContainingClass(containingClass)) {
+ var symbol = containingClass.symbol;
+ var name = ts.getSymbolNameForPrivateIdentifier(symbol, propName);
+ var prop = (symbol.members && symbol.members.get(name)) || (symbol.exports && symbol.exports.get(name));
+ if (prop) {
+ return prop;
}
}
}
- function assignNonContextualParameterTypes(signature) {
- if (signature.thisParameter) {
- assignParameterType(signature.thisParameter);
- }
- for (var _i = 0, _a = signature.parameters; _i < _a.length; _i++) {
- var parameter = _a[_i];
- assignParameterType(parameter);
- }
+ function getPrivateIdentifierPropertyOfType(leftType, lexicallyScopedIdentifier) {
+ return getPropertyOfType(leftType, lexicallyScopedIdentifier.escapedName);
}
- function assignParameterType(parameter, type) {
- var links = getSymbolLinks(parameter);
- if (!links.type) {
- var declaration = parameter.valueDeclaration;
- links.type = type || getWidenedTypeForVariableLikeDeclaration(declaration, /*includeOptionality*/ true);
- if (declaration.name.kind !== 75 /* Identifier */) {
- // if inference didn't come up with anything but unknown, fall back to the binding pattern if present.
- if (links.type === unknownType) {
- links.type = getTypeFromBindingPattern(declaration.name);
+ function checkPrivateIdentifierPropertyAccess(leftType, right, lexicallyScopedIdentifier) {
+ // Either the identifier could not be looked up in the lexical scope OR the lexically scoped identifier did not exist on the type.
+ // Find a private identifier with the same description on the type.
+ var propertyOnType;
+ var properties = getPropertiesOfType(leftType);
+ if (properties) {
+ ts.forEach(properties, function (symbol) {
+ var decl = symbol.valueDeclaration;
+ if (decl && ts.isNamedDeclaration(decl) && ts.isPrivateIdentifier(decl.name) && decl.name.escapedText === right.escapedText) {
+ propertyOnType = symbol;
+ return true;
}
- assignBindingElementTypes(declaration.name);
- }
+ });
}
- }
- // When contextual typing assigns a type to a parameter that contains a binding pattern, we also need to push
- // the destructured type into the contained binding elements.
- function assignBindingElementTypes(pattern) {
- for (var _i = 0, _a = pattern.elements; _i < _a.length; _i++) {
- var element = _a[_i];
- if (!ts.isOmittedExpression(element)) {
- if (element.name.kind === 75 /* Identifier */) {
- getSymbolLinks(getSymbolOfNode(element)).type = getTypeForBindingElement(element);
- }
- else {
- assignBindingElementTypes(element.name);
+ var diagName = diagnosticName(right);
+ if (propertyOnType) {
+ var typeValueDecl = propertyOnType.valueDeclaration;
+ var typeClass_1 = ts.getContainingClass(typeValueDecl);
+ ts.Debug.assert(!!typeClass_1);
+ // We found a private identifier property with the same description.
+ // Either:
+ // - There is a lexically scoped private identifier AND it shadows the one we found on the type.
+ // - It is an attempt to access the private identifier outside of the class.
+ if (lexicallyScopedIdentifier) {
+ var lexicalValueDecl = lexicallyScopedIdentifier.valueDeclaration;
+ var lexicalClass = ts.getContainingClass(lexicalValueDecl);
+ ts.Debug.assert(!!lexicalClass);
+ if (ts.findAncestor(lexicalClass, function (n) { return typeClass_1 === n; })) {
+ var diagnostic = error(right, ts.Diagnostics.The_property_0_cannot_be_accessed_on_type_1_within_this_class_because_it_is_shadowed_by_another_private_identifier_with_the_same_spelling, diagName, typeToString(leftType));
+ ts.addRelatedInfo(diagnostic, ts.createDiagnosticForNode(lexicalValueDecl, ts.Diagnostics.The_shadowing_declaration_of_0_is_defined_here, diagName), ts.createDiagnosticForNode(typeValueDecl, ts.Diagnostics.The_declaration_of_0_that_you_probably_intended_to_use_is_defined_here, diagName));
+ return true;
}
}
+ error(right, ts.Diagnostics.Property_0_is_not_accessible_outside_class_1_because_it_has_a_private_identifier, diagName, diagnosticName(typeClass_1.name || anon));
+ return true;
}
+ return false;
}
- function createPromiseType(promisedType) {
- // creates a `Promise` type where `T` is the promisedType argument
- var globalPromiseType = getGlobalPromiseType(/*reportErrors*/ true);
- if (globalPromiseType !== emptyGenericType) {
- // if the promised type is itself a promise, get the underlying type; otherwise, fallback to the promised type
- promisedType = getAwaitedType(promisedType) || unknownType;
- return createTypeReference(globalPromiseType, [promisedType]);
- }
- return unknownType;
- }
- function createPromiseLikeType(promisedType) {
- // creates a `PromiseLike` type where `T` is the promisedType argument
- var globalPromiseLikeType = getGlobalPromiseLikeType(/*reportErrors*/ true);
- if (globalPromiseLikeType !== emptyGenericType) {
- // if the promised type is itself a promise, get the underlying type; otherwise, fallback to the promised type
- promisedType = getAwaitedType(promisedType) || unknownType;
- return createTypeReference(globalPromiseLikeType, [promisedType]);
- }
- return unknownType;
- }
- function createPromiseReturnType(func, promisedType) {
- var promiseType = createPromiseType(promisedType);
- if (promiseType === unknownType) {
- error(func, ts.isImportCall(func) ?
- ts.Diagnostics.A_dynamic_import_call_returns_a_Promise_Make_sure_you_have_a_declaration_for_Promise_or_include_ES2015_in_your_lib_option :
- ts.Diagnostics.An_async_function_or_method_must_return_a_Promise_Make_sure_you_have_a_declaration_for_Promise_or_include_ES2015_in_your_lib_option);
- return errorType;
- }
- else if (!getGlobalPromiseConstructorSymbol(/*reportErrors*/ true)) {
- error(func, ts.isImportCall(func) ?
- ts.Diagnostics.A_dynamic_import_call_in_ES5_SlashES3_requires_the_Promise_constructor_Make_sure_you_have_a_declaration_for_the_Promise_constructor_or_include_ES2015_in_your_lib_option :
- ts.Diagnostics.An_async_function_or_method_in_ES5_SlashES3_requires_the_Promise_constructor_Make_sure_you_have_a_declaration_for_the_Promise_constructor_or_include_ES2015_in_your_lib_option);
- }
- return promiseType;
+ function isThisPropertyAccessInConstructor(node, prop) {
+ return (isConstructorDeclaredProperty(prop) || ts.isThisProperty(node) && isAutoTypedProperty(prop))
+ && ts.getThisContainer(node, /*includeArrowFunctions*/ true) === getDeclaringConstructor(prop);
}
- function getReturnTypeFromBody(func, checkMode) {
- if (!func.body) {
- return errorType;
- }
- var functionFlags = ts.getFunctionFlags(func);
- var isAsync = (functionFlags & 2 /* Async */) !== 0;
- var isGenerator = (functionFlags & 1 /* Generator */) !== 0;
- var returnType;
- var yieldType;
- var nextType;
- var fallbackReturnType = voidType;
- if (func.body.kind !== 223 /* Block */) { // Async or normal arrow function
- returnType = checkExpressionCached(func.body, checkMode && checkMode & ~8 /* SkipGenericFunctions */);
- if (isAsync) {
- // From within an async function you can return either a non-promise value or a promise. Any
- // Promise/A+ compatible implementation will always assimilate any foreign promise, so the
- // return type of the body should be unwrapped to its awaited type, which we will wrap in
- // the native Promise type later in this function.
- returnType = checkAwaitedType(returnType, /*errorNode*/ func, ts.Diagnostics.The_return_type_of_an_async_function_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member);
- }
+ function checkPropertyAccessExpressionOrQualifiedName(node, left, leftType, right) {
+ var parentSymbol = getNodeLinks(left).resolvedSymbol;
+ var assignmentKind = ts.getAssignmentTargetKind(node);
+ var apparentType = getApparentType(assignmentKind !== 0 /* None */ || isMethodAccessForCall(node) ? getWidenedType(leftType) : leftType);
+ if (ts.isPrivateIdentifier(right)) {
+ checkExternalEmitHelpers(node, 1048576 /* ClassPrivateFieldGet */);
}
- else if (isGenerator) { // Generator or AsyncGenerator function
- var returnTypes = checkAndAggregateReturnExpressionTypes(func, checkMode);
- if (!returnTypes) {
- fallbackReturnType = neverType;
+ var isAnyLike = isTypeAny(apparentType) || apparentType === silentNeverType;
+ var prop;
+ if (ts.isPrivateIdentifier(right)) {
+ var lexicallyScopedSymbol = lookupSymbolForPrivateIdentifierDeclaration(right.escapedText, right);
+ if (isAnyLike) {
+ if (lexicallyScopedSymbol) {
+ return apparentType;
+ }
+ if (!ts.getContainingClass(right)) {
+ grammarErrorOnNode(right, ts.Diagnostics.Private_identifiers_are_not_allowed_outside_class_bodies);
+ return anyType;
+ }
}
- else if (returnTypes.length > 0) {
- returnType = getUnionType(returnTypes, 2 /* Subtype */);
+ prop = lexicallyScopedSymbol ? getPrivateIdentifierPropertyOfType(leftType, lexicallyScopedSymbol) : undefined;
+ // Check for private-identifier-specific shadowing and lexical-scoping errors.
+ if (!prop && checkPrivateIdentifierPropertyAccess(leftType, right, lexicallyScopedSymbol)) {
+ return errorType;
}
- var _a = checkAndAggregateYieldOperandTypes(func, checkMode), yieldTypes = _a.yieldTypes, nextTypes = _a.nextTypes;
- yieldType = ts.some(yieldTypes) ? getUnionType(yieldTypes, 2 /* Subtype */) : undefined;
- nextType = ts.some(nextTypes) ? getIntersectionType(nextTypes) : undefined;
}
- else { // Async or normal function
- var types = checkAndAggregateReturnExpressionTypes(func, checkMode);
- if (!types) {
- // For an async function, the return type will not be never, but rather a Promise for never.
- return functionFlags & 2 /* Async */
- ? createPromiseReturnType(func, neverType) // Async function
- : neverType; // Normal function
- }
- if (types.length === 0) {
- // For an async function, the return type will not be void, but rather a Promise for void.
- return functionFlags & 2 /* Async */
- ? createPromiseReturnType(func, voidType) // Async function
- : voidType; // Normal function
+ else {
+ if (isAnyLike) {
+ if (ts.isIdentifier(left) && parentSymbol) {
+ markAliasReferenced(parentSymbol, node);
+ }
+ return apparentType;
}
- // Return a union of the return expression types.
- returnType = getUnionType(types, 2 /* Subtype */);
+ prop = getPropertyOfType(apparentType, right.escapedText);
}
- if (returnType || yieldType || nextType) {
- if (yieldType)
- reportErrorsFromWidening(func, yieldType, 3 /* GeneratorYield */);
- if (returnType)
- reportErrorsFromWidening(func, returnType, 1 /* FunctionReturn */);
- if (nextType)
- reportErrorsFromWidening(func, nextType, 2 /* GeneratorNext */);
- if (returnType && isUnitType(returnType) ||
- yieldType && isUnitType(yieldType) ||
- nextType && isUnitType(nextType)) {
- var contextualSignature = getContextualSignatureForFunctionLikeDeclaration(func);
- var contextualType = !contextualSignature ? undefined :
- contextualSignature === getSignatureFromDeclaration(func) ? isGenerator ? undefined : returnType :
- instantiateContextualType(getReturnTypeOfSignature(contextualSignature), func);
- if (isGenerator) {
- yieldType = getWidenedLiteralLikeTypeForContextualIterationTypeIfNeeded(yieldType, contextualType, 0 /* Yield */, isAsync);
- returnType = getWidenedLiteralLikeTypeForContextualIterationTypeIfNeeded(returnType, contextualType, 1 /* Return */, isAsync);
- nextType = getWidenedLiteralLikeTypeForContextualIterationTypeIfNeeded(nextType, contextualType, 2 /* Next */, isAsync);
+ if (ts.isIdentifier(left) && parentSymbol && !(prop && isConstEnumOrConstEnumOnlyModule(prop))) {
+ markAliasReferenced(parentSymbol, node);
+ }
+ var propType;
+ if (!prop) {
+ var indexInfo = !ts.isPrivateIdentifier(right) && (assignmentKind === 0 /* None */ || !isGenericObjectType(leftType) || isThisTypeParameter(leftType)) ? getIndexInfoOfType(apparentType, 0 /* String */) : undefined;
+ if (!(indexInfo && indexInfo.type)) {
+ if (isJSLiteralType(leftType)) {
+ return anyType;
}
- else {
- returnType = getWidenedLiteralLikeTypeForContextualReturnTypeIfNeeded(returnType, contextualType, isAsync);
+ if (leftType.symbol === globalThisSymbol) {
+ if (globalThisSymbol.exports.has(right.escapedText) && (globalThisSymbol.exports.get(right.escapedText).flags & 418 /* BlockScoped */)) {
+ error(right, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.unescapeLeadingUnderscores(right.escapedText), typeToString(leftType));
+ }
+ else if (noImplicitAny) {
+ error(right, ts.Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature, typeToString(leftType));
+ }
+ return anyType;
}
+ if (right.escapedText && !checkAndReportErrorForExtendingInterface(node)) {
+ reportNonexistentProperty(right, isThisTypeParameter(leftType) ? apparentType : leftType);
+ }
+ return errorType;
}
- if (yieldType)
- yieldType = getWidenedType(yieldType);
- if (returnType)
- returnType = getWidenedType(returnType);
- if (nextType)
- nextType = getWidenedType(nextType);
- }
- if (isGenerator) {
- return createGeneratorReturnType(yieldType || neverType, returnType || fallbackReturnType, nextType || getContextualIterationType(2 /* Next */, func) || unknownType, isAsync);
+ if (indexInfo.isReadonly && (ts.isAssignmentTarget(node) || ts.isDeleteTarget(node))) {
+ error(node, ts.Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(apparentType));
+ }
+ propType = (compilerOptions.noUncheckedIndexedAccess && !ts.isAssignmentTarget(node)) ? getUnionType([indexInfo.type, undefinedType]) : indexInfo.type;
}
else {
- // From within an async function you can return either a non-promise value or a promise. Any
- // Promise/A+ compatible implementation will always assimilate any foreign promise, so the
- // return type of the body is awaited type of the body, wrapped in a native Promise type.
- return isAsync
- ? createPromiseType(returnType || fallbackReturnType)
- : returnType || fallbackReturnType;
+ if (getDeclarationNodeFlagsFromSymbol(prop) & 134217728 /* Deprecated */ && isUncalledFunctionReference(node, prop)) {
+ errorOrSuggestion(/* isError */ false, right, ts.Diagnostics._0_is_deprecated, right.escapedText);
+ }
+ checkPropertyNotUsedBeforeDeclaration(prop, node, right);
+ markPropertyAsReferenced(prop, node, left.kind === 107 /* ThisKeyword */);
+ getNodeLinks(node).resolvedSymbol = prop;
+ checkPropertyAccessibility(node, left.kind === 105 /* SuperKeyword */, apparentType, prop);
+ if (isAssignmentToReadonlyEntity(node, prop, assignmentKind)) {
+ error(right, ts.Diagnostics.Cannot_assign_to_0_because_it_is_a_read_only_property, ts.idText(right));
+ return errorType;
+ }
+ propType = isThisPropertyAccessInConstructor(node, prop) ? autoType : getConstraintForLocation(getTypeOfSymbol(prop), node);
}
+ return getFlowTypeOfAccessExpression(node, prop, propType, right);
}
- function createGeneratorReturnType(yieldType, returnType, nextType, isAsyncGenerator) {
- var resolver = isAsyncGenerator ? asyncIterationTypesResolver : syncIterationTypesResolver;
- var globalGeneratorType = resolver.getGlobalGeneratorType(/*reportErrors*/ false);
- yieldType = resolver.resolveIterationType(yieldType, /*errorNode*/ undefined) || unknownType;
- returnType = resolver.resolveIterationType(returnType, /*errorNode*/ undefined) || unknownType;
- nextType = resolver.resolveIterationType(nextType, /*errorNode*/ undefined) || unknownType;
- if (globalGeneratorType === emptyGenericType) {
- // Fall back to the global IterableIterator if returnType is assignable to the expected return iteration
- // type of IterableIterator, and the expected next iteration type of IterableIterator is assignable to
- // nextType.
- var globalType = resolver.getGlobalIterableIteratorType(/*reportErrors*/ false);
- var iterationTypes = globalType !== emptyGenericType ? getIterationTypesOfGlobalIterableType(globalType, resolver) : undefined;
- var iterableIteratorReturnType = iterationTypes ? iterationTypes.returnType : anyType;
- var iterableIteratorNextType = iterationTypes ? iterationTypes.nextType : undefinedType;
- if (isTypeAssignableTo(returnType, iterableIteratorReturnType) &&
- isTypeAssignableTo(iterableIteratorNextType, nextType)) {
- if (globalType !== emptyGenericType) {
- return createTypeFromGenericGlobalType(globalType, [yieldType]);
+ function getFlowTypeOfAccessExpression(node, prop, propType, errorNode) {
+ // Only compute control flow type if this is a property access expression that isn't an
+ // assignment target, and the referenced property was declared as a variable, property,
+ // accessor, or optional method.
+ var assignmentKind = ts.getAssignmentTargetKind(node);
+ if (!ts.isAccessExpression(node) ||
+ assignmentKind === 1 /* Definite */ ||
+ prop && !(prop.flags & (3 /* Variable */ | 4 /* Property */ | 98304 /* Accessor */)) && !(prop.flags & 8192 /* Method */ && propType.flags & 1048576 /* Union */)) {
+ return propType;
+ }
+ if (propType === autoType) {
+ return getFlowTypeOfProperty(node, prop);
+ }
+ // If strict null checks and strict property initialization checks are enabled, if we have
+ // a this.xxx property access, if the property is an instance property without an initializer,
+ // and if we are in a constructor of the same class as the property declaration, assume that
+ // the property is uninitialized at the top of the control flow.
+ var assumeUninitialized = false;
+ if (strictNullChecks && strictPropertyInitialization && node.expression.kind === 107 /* ThisKeyword */) {
+ var declaration = prop && prop.valueDeclaration;
+ if (declaration && isInstancePropertyWithoutInitializer(declaration)) {
+ var flowContainer = getControlFlowContainer(node);
+ if (flowContainer.kind === 166 /* Constructor */ && flowContainer.parent === declaration.parent && !(declaration.flags & 8388608 /* Ambient */)) {
+ assumeUninitialized = true;
}
- // The global IterableIterator type doesn't exist, so report an error
- resolver.getGlobalIterableIteratorType(/*reportErrors*/ true);
- return emptyObjectType;
}
- // The global Generator type doesn't exist, so report an error
- resolver.getGlobalGeneratorType(/*reportErrors*/ true);
- return emptyObjectType;
}
- return createTypeFromGenericGlobalType(globalGeneratorType, [yieldType, returnType, nextType]);
+ else if (strictNullChecks && prop && prop.valueDeclaration &&
+ ts.isPropertyAccessExpression(prop.valueDeclaration) &&
+ ts.getAssignmentDeclarationPropertyAccessKind(prop.valueDeclaration) &&
+ getControlFlowContainer(node) === getControlFlowContainer(prop.valueDeclaration)) {
+ assumeUninitialized = true;
+ }
+ var flowType = getFlowTypeOfReference(node, propType, assumeUninitialized ? getOptionalType(propType) : propType);
+ if (assumeUninitialized && !(getFalsyFlags(propType) & 32768 /* Undefined */) && getFalsyFlags(flowType) & 32768 /* Undefined */) {
+ error(errorNode, ts.Diagnostics.Property_0_is_used_before_being_assigned, symbolToString(prop)); // TODO: GH#18217
+ // Return the declared type to reduce follow-on errors
+ return propType;
+ }
+ return assignmentKind ? getBaseTypeOfLiteralType(flowType) : flowType;
}
- function checkAndAggregateYieldOperandTypes(func, checkMode) {
- var yieldTypes = [];
- var nextTypes = [];
- var isAsync = (ts.getFunctionFlags(func) & 2 /* Async */) !== 0;
- ts.forEachYieldExpression(func.body, function (yieldExpression) {
- var yieldExpressionType = yieldExpression.expression ? checkExpression(yieldExpression.expression, checkMode) : undefinedWideningType;
- ts.pushIfUnique(yieldTypes, getYieldedTypeOfYieldExpression(yieldExpression, yieldExpressionType, anyType, isAsync));
- var nextType;
- if (yieldExpression.asteriskToken) {
- var iterationTypes = getIterationTypesOfIterable(yieldExpressionType, isAsync ? 19 /* AsyncYieldStar */ : 17 /* YieldStar */, yieldExpression.expression);
- nextType = iterationTypes && iterationTypes.nextType;
- }
- else {
- nextType = getContextualType(yieldExpression);
+ function checkPropertyNotUsedBeforeDeclaration(prop, node, right) {
+ var valueDeclaration = prop.valueDeclaration;
+ if (!valueDeclaration || ts.getSourceFileOfNode(node).isDeclarationFile) {
+ return;
+ }
+ var diagnosticMessage;
+ var declarationName = ts.idText(right);
+ if (isInPropertyInitializer(node)
+ && !(ts.isAccessExpression(node) && ts.isAccessExpression(node.expression))
+ && !isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right)
+ && !isPropertyDeclaredInAncestorClass(prop)) {
+ diagnosticMessage = error(right, ts.Diagnostics.Property_0_is_used_before_its_initialization, declarationName);
+ }
+ else if (valueDeclaration.kind === 252 /* ClassDeclaration */ &&
+ node.parent.kind !== 173 /* TypeReference */ &&
+ !(valueDeclaration.flags & 8388608 /* Ambient */) &&
+ !isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right)) {
+ diagnosticMessage = error(right, ts.Diagnostics.Class_0_used_before_its_declaration, declarationName);
+ }
+ if (diagnosticMessage) {
+ ts.addRelatedInfo(diagnosticMessage, ts.createDiagnosticForNode(valueDeclaration, ts.Diagnostics._0_is_declared_here, declarationName));
+ }
+ }
+ function isInPropertyInitializer(node) {
+ return !!ts.findAncestor(node, function (node) {
+ switch (node.kind) {
+ case 163 /* PropertyDeclaration */:
+ return true;
+ case 288 /* PropertyAssignment */:
+ case 165 /* MethodDeclaration */:
+ case 167 /* GetAccessor */:
+ case 168 /* SetAccessor */:
+ case 290 /* SpreadAssignment */:
+ case 158 /* ComputedPropertyName */:
+ case 228 /* TemplateSpan */:
+ case 283 /* JsxExpression */:
+ case 280 /* JsxAttribute */:
+ case 281 /* JsxAttributes */:
+ case 282 /* JsxSpreadAttribute */:
+ case 275 /* JsxOpeningElement */:
+ case 223 /* ExpressionWithTypeArguments */:
+ case 286 /* HeritageClause */:
+ return false;
+ default:
+ return ts.isExpressionNode(node) ? false : "quit";
}
- if (nextType)
- ts.pushIfUnique(nextTypes, nextType);
});
- return { yieldTypes: yieldTypes, nextTypes: nextTypes };
- }
- function getYieldedTypeOfYieldExpression(node, expressionType, sentType, isAsync) {
- var errorNode = node.expression || node;
- // A `yield*` expression effectively yields everything that its operand yields
- var yieldedType = node.asteriskToken ? checkIteratedTypeOrElementType(isAsync ? 19 /* AsyncYieldStar */ : 17 /* YieldStar */, expressionType, sentType, errorNode) : expressionType;
- return !isAsync ? yieldedType : getAwaitedType(yieldedType, errorNode, node.asteriskToken
- ? ts.Diagnostics.Type_of_iterated_elements_of_a_yield_Asterisk_operand_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member
- : ts.Diagnostics.Type_of_yield_operand_in_an_async_generator_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member);
}
/**
- * Collect the TypeFacts learned from a typeof switch with
- * total clauses `witnesses`, and the active clause ranging
- * from `start` to `end`. Parameter `hasDefault` denotes
- * whether the active clause contains a default clause.
+ * It's possible that "prop.valueDeclaration" is a local declaration, but the property was also declared in a superclass.
+ * In that case we won't consider it used before its declaration, because it gets its value from the superclass' declaration.
*/
- function getFactsFromTypeofSwitch(start, end, witnesses, hasDefault) {
- var facts = 0 /* None */;
- // When in the default we only collect inequality facts
- // because default is 'in theory' a set of infinite
- // equalities.
- if (hasDefault) {
- // Value is not equal to any types after the active clause.
- for (var i = end; i < witnesses.length; i++) {
- facts |= typeofNEFacts.get(witnesses[i]) || 32768 /* TypeofNEHostObject */;
+ function isPropertyDeclaredInAncestorClass(prop) {
+ if (!(prop.parent.flags & 32 /* Class */)) {
+ return false;
+ }
+ var classType = getTypeOfSymbol(prop.parent);
+ while (true) {
+ classType = classType.symbol && getSuperClass(classType);
+ if (!classType) {
+ return false;
}
- // Remove inequalities for types that appear in the
- // active clause because they appear before other
- // types collected so far.
- for (var i = start; i < end; i++) {
- facts &= ~(typeofNEFacts.get(witnesses[i]) || 0);
+ var superProperty = getPropertyOfType(classType, prop.escapedName);
+ if (superProperty && superProperty.valueDeclaration) {
+ return true;
}
- // Add inequalities for types before the active clause unconditionally.
- for (var i = 0; i < start; i++) {
- facts |= typeofNEFacts.get(witnesses[i]) || 32768 /* TypeofNEHostObject */;
+ }
+ }
+ function getSuperClass(classType) {
+ var x = getBaseTypes(classType);
+ if (x.length === 0) {
+ return undefined;
+ }
+ return getIntersectionType(x);
+ }
+ function reportNonexistentProperty(propNode, containingType) {
+ var errorInfo;
+ var relatedInfo;
+ if (!ts.isPrivateIdentifier(propNode) && containingType.flags & 1048576 /* Union */ && !(containingType.flags & 131068 /* Primitive */)) {
+ for (var _i = 0, _a = containingType.types; _i < _a.length; _i++) {
+ var subtype = _a[_i];
+ if (!getPropertyOfType(subtype, propNode.escapedText) && !getIndexInfoOfType(subtype, 0 /* String */)) {
+ errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.declarationNameToString(propNode), typeToString(subtype));
+ break;
+ }
}
}
- // When in an active clause without default the set of
- // equalities is finite.
+ if (typeHasStaticProperty(propNode.escapedText, containingType)) {
+ errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_is_a_static_member_of_type_1, ts.declarationNameToString(propNode), typeToString(containingType));
+ }
else {
- // Add equalities for all types in the active clause.
- for (var i = start; i < end; i++) {
- facts |= typeofEQFacts.get(witnesses[i]) || 128 /* TypeofEQHostObject */;
+ var promisedType = getPromisedTypeOfPromise(containingType);
+ if (promisedType && getPropertyOfType(promisedType, propNode.escapedText)) {
+ errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.declarationNameToString(propNode), typeToString(containingType));
+ relatedInfo = ts.createDiagnosticForNode(propNode, ts.Diagnostics.Did_you_forget_to_use_await);
}
- // Remove equalities for types that appear before the
- // active clause.
- for (var i = 0; i < start; i++) {
- facts &= ~(typeofEQFacts.get(witnesses[i]) || 0);
+ else {
+ var missingProperty = ts.declarationNameToString(propNode);
+ var container = typeToString(containingType);
+ var libSuggestion = getSuggestedLibForNonExistentProperty(missingProperty, containingType);
+ if (libSuggestion !== undefined) {
+ errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_does_not_exist_on_type_1_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_2_or_later, missingProperty, container, libSuggestion);
+ }
+ else {
+ var suggestion = getSuggestedSymbolForNonexistentProperty(propNode, containingType);
+ if (suggestion !== undefined) {
+ var suggestedName = ts.symbolName(suggestion);
+ errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2, missingProperty, container, suggestedName);
+ relatedInfo = suggestion.valueDeclaration && ts.createDiagnosticForNode(suggestion.valueDeclaration, ts.Diagnostics._0_is_declared_here, suggestedName);
+ }
+ else {
+ errorInfo = ts.chainDiagnosticMessages(elaborateNeverIntersection(errorInfo, containingType), ts.Diagnostics.Property_0_does_not_exist_on_type_1, missingProperty, container);
+ }
+ }
}
}
- return facts;
+ var resultDiagnostic = ts.createDiagnosticForNodeFromMessageChain(propNode, errorInfo);
+ if (relatedInfo) {
+ ts.addRelatedInfo(resultDiagnostic, relatedInfo);
+ }
+ diagnostics.add(resultDiagnostic);
}
- function isExhaustiveSwitchStatement(node) {
- var links = getNodeLinks(node);
- return links.isExhaustive !== undefined ? links.isExhaustive : (links.isExhaustive = computeExhaustiveSwitchStatement(node));
+ function typeHasStaticProperty(propName, containingType) {
+ var prop = containingType.symbol && getPropertyOfType(getTypeOfSymbol(containingType.symbol), propName);
+ return prop !== undefined && prop.valueDeclaration && ts.hasSyntacticModifier(prop.valueDeclaration, 32 /* Static */);
}
- function computeExhaustiveSwitchStatement(node) {
- if (node.expression.kind === 204 /* TypeOfExpression */) {
- var operandType = getTypeOfExpression(node.expression.expression);
- var witnesses = getSwitchClauseTypeOfWitnesses(node, /*retainDefault*/ false);
- // notEqualFacts states that the type of the switched value is not equal to every type in the switch.
- var notEqualFacts_1 = getFactsFromTypeofSwitch(0, 0, witnesses, /*hasDefault*/ true);
- var type_3 = getBaseConstraintOfType(operandType) || operandType;
- return !!(filterType(type_3, function (t) { return (getTypeFacts(t) & notEqualFacts_1) === notEqualFacts_1; }).flags & 131072 /* Never */);
+ function getSuggestedLibForNonExistentName(name) {
+ var missingName = diagnosticName(name);
+ var allFeatures = ts.getScriptTargetFeatures();
+ var libTargets = ts.getOwnKeys(allFeatures);
+ for (var _i = 0, libTargets_1 = libTargets; _i < libTargets_1.length; _i++) {
+ var libTarget = libTargets_1[_i];
+ var containingTypes = ts.getOwnKeys(allFeatures[libTarget]);
+ if (containingTypes !== undefined && ts.contains(containingTypes, missingName)) {
+ return libTarget;
+ }
}
- var type = getTypeOfExpression(node.expression);
- if (!isLiteralType(type)) {
- return false;
+ }
+ function getSuggestedLibForNonExistentProperty(missingProperty, containingType) {
+ var container = getApparentType(containingType).symbol;
+ if (!container) {
+ return undefined;
}
- var switchTypes = getSwitchClauseTypes(node);
- if (!switchTypes.length || ts.some(switchTypes, isNeitherUnitTypeNorNever)) {
- return false;
+ var allFeatures = ts.getScriptTargetFeatures();
+ var libTargets = ts.getOwnKeys(allFeatures);
+ for (var _i = 0, libTargets_2 = libTargets; _i < libTargets_2.length; _i++) {
+ var libTarget = libTargets_2[_i];
+ var featuresOfLib = allFeatures[libTarget];
+ var featuresOfContainingType = featuresOfLib[ts.symbolName(container)];
+ if (featuresOfContainingType !== undefined && ts.contains(featuresOfContainingType, missingProperty)) {
+ return libTarget;
+ }
}
- return eachTypeContainedIn(mapType(type, getRegularTypeOfLiteralType), switchTypes);
}
- function functionHasImplicitReturn(func) {
- return func.endFlowNode && isReachableFlowNode(func.endFlowNode);
+ function getSuggestedSymbolForNonexistentProperty(name, containingType) {
+ return getSpellingSuggestionForName(ts.isString(name) ? name : ts.idText(name), getPropertiesOfType(containingType), 111551 /* Value */);
}
- /** NOTE: Return value of `[]` means a different thing than `undefined`. `[]` means func returns `void`, `undefined` means it returns `never`. */
- function checkAndAggregateReturnExpressionTypes(func, checkMode) {
- var functionFlags = ts.getFunctionFlags(func);
- var aggregatedTypes = [];
- var hasReturnWithNoExpression = functionHasImplicitReturn(func);
- var hasReturnOfTypeNever = false;
- ts.forEachReturnStatement(func.body, function (returnStatement) {
- var expr = returnStatement.expression;
- if (expr) {
- var type = checkExpressionCached(expr, checkMode && checkMode & ~8 /* SkipGenericFunctions */);
- if (functionFlags & 2 /* Async */) {
- // From within an async function you can return either a non-promise value or a promise. Any
- // Promise/A+ compatible implementation will always assimilate any foreign promise, so the
- // return type of the body should be unwrapped to its awaited type, which should be wrapped in
- // the native Promise type by the caller.
- type = checkAwaitedType(type, func, ts.Diagnostics.The_return_type_of_an_async_function_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member);
- }
- if (type.flags & 131072 /* Never */) {
- hasReturnOfTypeNever = true;
- }
- ts.pushIfUnique(aggregatedTypes, type);
- }
- else {
- hasReturnWithNoExpression = true;
- }
+ function getSuggestedSymbolForNonexistentJSXAttribute(name, containingType) {
+ var strName = ts.isString(name) ? name : ts.idText(name);
+ var properties = getPropertiesOfType(containingType);
+ var jsxSpecific = strName === "for" ? ts.find(properties, function (x) { return ts.symbolName(x) === "htmlFor"; })
+ : strName === "class" ? ts.find(properties, function (x) { return ts.symbolName(x) === "className"; })
+ : undefined;
+ return jsxSpecific !== null && jsxSpecific !== void 0 ? jsxSpecific : getSpellingSuggestionForName(strName, properties, 111551 /* Value */);
+ }
+ function getSuggestionForNonexistentProperty(name, containingType) {
+ var suggestion = getSuggestedSymbolForNonexistentProperty(name, containingType);
+ return suggestion && ts.symbolName(suggestion);
+ }
+ function getSuggestedSymbolForNonexistentSymbol(location, outerName, meaning) {
+ ts.Debug.assert(outerName !== undefined, "outername should always be defined");
+ var result = resolveNameHelper(location, outerName, meaning, /*nameNotFoundMessage*/ undefined, outerName, /*isUse*/ false, /*excludeGlobals*/ false, function (symbols, name, meaning) {
+ ts.Debug.assertEqual(outerName, name, "name should equal outerName");
+ var symbol = getSymbol(symbols, name, meaning);
+ // Sometimes the symbol is found when location is a return type of a function: `typeof x` and `x` is declared in the body of the function
+ // So the table *contains* `x` but `x` isn't actually in scope.
+ // However, resolveNameHelper will continue and call this callback again, so we'll eventually get a correct suggestion.
+ return symbol || getSpellingSuggestionForName(ts.unescapeLeadingUnderscores(name), ts.arrayFrom(symbols.values()), meaning);
});
- if (aggregatedTypes.length === 0 && !hasReturnWithNoExpression && (hasReturnOfTypeNever || mayReturnNever(func))) {
+ return result;
+ }
+ function getSuggestionForNonexistentSymbol(location, outerName, meaning) {
+ var symbolResult = getSuggestedSymbolForNonexistentSymbol(location, outerName, meaning);
+ return symbolResult && ts.symbolName(symbolResult);
+ }
+ function getSuggestedSymbolForNonexistentModule(name, targetModule) {
+ return targetModule.exports && getSpellingSuggestionForName(ts.idText(name), getExportsOfModuleAsArray(targetModule), 2623475 /* ModuleMember */);
+ }
+ function getSuggestionForNonexistentExport(name, targetModule) {
+ var suggestion = getSuggestedSymbolForNonexistentModule(name, targetModule);
+ return suggestion && ts.symbolName(suggestion);
+ }
+ function getSuggestionForNonexistentIndexSignature(objectType, expr, keyedType) {
+ // check if object type has setter or getter
+ function hasProp(name) {
+ var prop = getPropertyOfObjectType(objectType, name);
+ if (prop) {
+ var s = getSingleCallSignature(getTypeOfSymbol(prop));
+ return !!s && getMinArgumentCount(s) >= 1 && isTypeAssignableTo(keyedType, getTypeAtPosition(s, 0));
+ }
+ return false;
+ }
+ ;
+ var suggestedMethod = ts.isAssignmentTarget(expr) ? "set" : "get";
+ if (!hasProp(suggestedMethod)) {
return undefined;
}
- if (strictNullChecks && aggregatedTypes.length && hasReturnWithNoExpression &&
- !(isJSConstructor(func) && aggregatedTypes.some(function (t) { return t.symbol === func.symbol; }))) {
- // Javascript "callable constructors", containing eg `if (!(this instanceof A)) return new A()` should not add undefined
- ts.pushIfUnique(aggregatedTypes, undefinedType);
+ var suggestion = ts.tryGetPropertyAccessOrIdentifierToString(expr.expression);
+ if (suggestion === undefined) {
+ suggestion = suggestedMethod;
}
- return aggregatedTypes;
- }
- function mayReturnNever(func) {
- switch (func.kind) {
- case 201 /* FunctionExpression */:
- case 202 /* ArrowFunction */:
- return true;
- case 161 /* MethodDeclaration */:
- return func.parent.kind === 193 /* ObjectLiteralExpression */;
- default:
- return false;
+ else {
+ suggestion += "." + suggestedMethod;
}
+ return suggestion;
}
/**
- * TypeScript Specification 1.0 (6.3) - July 2014
- * An explicitly typed function whose return type isn't the Void type,
- * the Any type, or a union type containing the Void or Any type as a constituent
- * must have at least one return statement somewhere in its body.
- * An exception to this rule is if the function implementation consists of a single 'throw' statement.
+ * Given a name and a list of symbols whose names are *not* equal to the name, return a spelling suggestion if there is one that is close enough.
+ * Names less than length 3 only check for case-insensitive equality, not levenshtein distance.
*
- * @param returnType - return type of the function, can be undefined if return type is not explicitly specified
+ * If there is a candidate that's the same except for case, return that.
+ * If there is a candidate that's within one edit of the name, return that.
+ * Otherwise, return the candidate with the smallest Levenshtein distance,
+ * except for candidates:
+ * * With no name
+ * * Whose meaning doesn't match the `meaning` parameter.
+ * * Whose length differs from the target name by more than 0.34 of the length of the name.
+ * * Whose levenshtein distance is more than 0.4 of the length of the name
+ * (0.4 allows 1 substitution/transposition for every 5 characters,
+ * and 1 insertion/deletion at 3 characters)
*/
- function checkAllCodePathsInNonVoidFunctionReturnOrThrow(func, returnType) {
- if (!produceDiagnostics) {
+ function getSpellingSuggestionForName(name, symbols, meaning) {
+ return ts.getSpellingSuggestion(name, symbols, getCandidateName);
+ function getCandidateName(candidate) {
+ var candidateName = ts.symbolName(candidate);
+ if (ts.startsWith(candidateName, "\"")) {
+ return undefined;
+ }
+ if (candidate.flags & meaning) {
+ return candidateName;
+ }
+ if (candidate.flags & 2097152 /* Alias */) {
+ var alias = tryResolveAlias(candidate);
+ if (alias && alias.flags & meaning) {
+ return candidateName;
+ }
+ }
+ return undefined;
+ }
+ }
+ function markPropertyAsReferenced(prop, nodeForCheckWriteOnly, isThisAccess) {
+ var valueDeclaration = prop && (prop.flags & 106500 /* ClassMember */) && prop.valueDeclaration;
+ if (!valueDeclaration) {
return;
}
- var functionFlags = ts.getFunctionFlags(func);
- var type = returnType && unwrapReturnType(returnType, functionFlags);
- // Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions.
- if (type && maybeTypeOfKind(type, 1 /* Any */ | 16384 /* Void */)) {
+ var hasPrivateModifier = ts.hasEffectiveModifier(valueDeclaration, 8 /* Private */);
+ var hasPrivateIdentifier = ts.isNamedDeclaration(prop.valueDeclaration) && ts.isPrivateIdentifier(prop.valueDeclaration.name);
+ if (!hasPrivateModifier && !hasPrivateIdentifier) {
return;
}
- // If all we have is a function signature, or an arrow function with an expression body, then there is nothing to check.
- // also if HasImplicitReturn flag is not set this means that all codepaths in function body end with return or throw
- if (func.kind === 160 /* MethodSignature */ || ts.nodeIsMissing(func.body) || func.body.kind !== 223 /* Block */ || !functionHasImplicitReturn(func)) {
+ if (nodeForCheckWriteOnly && ts.isWriteOnlyAccess(nodeForCheckWriteOnly) && !(prop.flags & 65536 /* SetAccessor */)) {
return;
}
- var hasExplicitReturn = func.flags & 512 /* HasExplicitReturn */;
- if (type && type.flags & 131072 /* Never */) {
- error(ts.getEffectiveReturnTypeNode(func), ts.Diagnostics.A_function_returning_never_cannot_have_a_reachable_end_point);
+ if (isThisAccess) {
+ // Find any FunctionLikeDeclaration because those create a new 'this' binding. But this should only matter for methods (or getters/setters).
+ var containingMethod = ts.findAncestor(nodeForCheckWriteOnly, ts.isFunctionLikeDeclaration);
+ if (containingMethod && containingMethod.symbol === prop) {
+ return;
+ }
}
- else if (type && !hasExplicitReturn) {
- // minimal check: function has syntactic return type annotation and no explicit return statements in the body
- // this function does not conform to the specification.
- // NOTE: having returnType !== undefined is a precondition for entering this branch so func.type will always be present
- error(ts.getEffectiveReturnTypeNode(func), ts.Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value);
+ (ts.getCheckFlags(prop) & 1 /* Instantiated */ ? getSymbolLinks(prop).target : prop).isReferenced = 67108863 /* All */;
+ }
+ function isValidPropertyAccess(node, propertyName) {
+ switch (node.kind) {
+ case 201 /* PropertyAccessExpression */:
+ return isValidPropertyAccessWithType(node, node.expression.kind === 105 /* SuperKeyword */, propertyName, getWidenedType(checkExpression(node.expression)));
+ case 157 /* QualifiedName */:
+ return isValidPropertyAccessWithType(node, /*isSuper*/ false, propertyName, getWidenedType(checkExpression(node.left)));
+ case 195 /* ImportType */:
+ return isValidPropertyAccessWithType(node, /*isSuper*/ false, propertyName, getTypeFromTypeNode(node));
}
- else if (type && strictNullChecks && !isTypeAssignableTo(undefinedType, type)) {
- error(ts.getEffectiveReturnTypeNode(func) || func, ts.Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined);
+ }
+ function isValidPropertyAccessForCompletions(node, type, property) {
+ return isValidPropertyAccessWithType(node, node.kind === 201 /* PropertyAccessExpression */ && node.expression.kind === 105 /* SuperKeyword */, property.escapedName, type);
+ // Previously we validated the 'this' type of methods but this adversely affected performance. See #31377 for more context.
+ }
+ function isValidPropertyAccessWithType(node, isSuper, propertyName, type) {
+ if (type === errorType || isTypeAny(type)) {
+ return true;
}
- else if (compilerOptions.noImplicitReturns) {
- if (!type) {
- // If return type annotation is omitted check if function has any explicit return statements.
- // If it does not have any - its inferred return type is void - don't do any checks.
- // Otherwise get inferred return type from function body and report error only if it is not void / anytype
- if (!hasExplicitReturn) {
- return;
- }
- var inferredReturnType = getReturnTypeOfSignature(getSignatureFromDeclaration(func));
- if (isUnwrappedReturnTypeVoidOrAny(func, inferredReturnType)) {
- return;
- }
+ var prop = getPropertyOfType(type, propertyName);
+ if (prop) {
+ if (ts.isPropertyAccessExpression(node) && prop.valueDeclaration && ts.isPrivateIdentifierPropertyDeclaration(prop.valueDeclaration)) {
+ var declClass_1 = ts.getContainingClass(prop.valueDeclaration);
+ return !ts.isOptionalChain(node) && !!ts.findAncestor(node, function (parent) { return parent === declClass_1; });
}
- error(ts.getEffectiveReturnTypeNode(func) || func, ts.Diagnostics.Not_all_code_paths_return_a_value);
+ return checkPropertyAccessibility(node, isSuper, type, prop);
}
+ // In js files properties of unions are allowed in completion
+ return ts.isInJSFile(node) && (type.flags & 1048576 /* Union */) !== 0 && type.types.some(function (elementType) { return isValidPropertyAccessWithType(node, isSuper, propertyName, elementType); });
}
- function checkFunctionExpressionOrObjectLiteralMethod(node, checkMode) {
- ts.Debug.assert(node.kind !== 161 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node));
- checkNodeDeferred(node);
- // The identityMapper object is used to indicate that function expressions are wildcards
- if (checkMode && checkMode & 4 /* SkipContextSensitive */ && isContextSensitive(node)) {
- // Skip parameters, return signature with return type that retains noncontextual parts so inferences can still be drawn in an early stage
- if (!ts.getEffectiveReturnTypeNode(node) && !hasContextSensitiveParameters(node)) {
- // Return plain anyFunctionType if there is no possibility we'll make inferences from the return type
- var contextualSignature = getContextualSignature(node);
- if (contextualSignature && couldContainTypeVariables(getReturnTypeOfSignature(contextualSignature))) {
- var links = getNodeLinks(node);
- if (links.contextFreeType) {
- return links.contextFreeType;
- }
- var returnType = getReturnTypeFromBody(node, checkMode);
- var returnOnlySignature = createSignature(undefined, undefined, undefined, ts.emptyArray, returnType, /*resolvedTypePredicate*/ undefined, 0, 0 /* None */);
- var returnOnlyType = createAnonymousType(node.symbol, emptySymbols, [returnOnlySignature], ts.emptyArray, undefined, undefined);
- returnOnlyType.objectFlags |= 2097152 /* NonInferrableType */;
- return links.contextFreeType = returnOnlyType;
- }
+ /**
+ * Return the symbol of the for-in variable declared or referenced by the given for-in statement.
+ */
+ function getForInVariableSymbol(node) {
+ var initializer = node.initializer;
+ if (initializer.kind === 250 /* VariableDeclarationList */) {
+ var variable = initializer.declarations[0];
+ if (variable && !ts.isBindingPattern(variable.name)) {
+ return getSymbolOfNode(variable);
}
- return anyFunctionType;
}
- // Grammar checking
- var hasGrammarError = checkGrammarFunctionLikeDeclaration(node);
- if (!hasGrammarError && node.kind === 201 /* FunctionExpression */) {
- checkGrammarForGenerator(node);
+ else if (initializer.kind === 78 /* Identifier */) {
+ return getResolvedSymbol(initializer);
}
- contextuallyCheckFunctionExpressionOrObjectLiteralMethod(node, checkMode);
- return getTypeOfSymbol(getSymbolOfNode(node));
+ return undefined;
}
- function contextuallyCheckFunctionExpressionOrObjectLiteralMethod(node, checkMode) {
- var links = getNodeLinks(node);
- // Check if function expression is contextually typed and assign parameter types if so.
- if (!(links.flags & 1024 /* ContextChecked */)) {
- var contextualSignature = getContextualSignature(node);
- // If a type check is started at a function expression that is an argument of a function call, obtaining the
- // contextual type may recursively get back to here during overload resolution of the call. If so, we will have
- // already assigned contextual types.
- if (!(links.flags & 1024 /* ContextChecked */)) {
- links.flags |= 1024 /* ContextChecked */;
- var signature = ts.firstOrUndefined(getSignaturesOfType(getTypeOfSymbol(getSymbolOfNode(node)), 0 /* Call */));
- if (!signature) {
- return;
- }
- if (isContextSensitive(node)) {
- if (contextualSignature) {
- var inferenceContext = getInferenceContext(node);
- if (checkMode && checkMode & 2 /* Inferential */) {
- inferFromAnnotatedParameters(signature, contextualSignature, inferenceContext);
- }
- var instantiatedContextualSignature = inferenceContext ?
- instantiateSignature(contextualSignature, inferenceContext.mapper) : contextualSignature;
- assignContextualParameterTypes(signature, instantiatedContextualSignature);
- }
- else {
- // Force resolution of all parameter types such that the absence of a contextual type is consistently reflected.
- assignNonContextualParameterTypes(signature);
- }
- }
- if (contextualSignature && !getReturnTypeFromAnnotation(node) && !signature.resolvedReturnType) {
- var returnType = getReturnTypeFromBody(node, checkMode);
- if (!signature.resolvedReturnType) {
- signature.resolvedReturnType = returnType;
- }
- }
- checkSignatureDeclaration(node);
- }
- }
+ /**
+ * Return true if the given type is considered to have numeric property names.
+ */
+ function hasNumericPropertyNames(type) {
+ return getIndexTypeOfType(type, 1 /* Number */) && !getIndexTypeOfType(type, 0 /* String */);
}
- function checkFunctionExpressionOrObjectLiteralMethodDeferred(node) {
- ts.Debug.assert(node.kind !== 161 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node));
- var functionFlags = ts.getFunctionFlags(node);
- var returnType = getReturnTypeFromAnnotation(node);
- checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnType);
- if (node.body) {
- if (!ts.getEffectiveReturnTypeNode(node)) {
- // There are some checks that are only performed in getReturnTypeFromBody, that may produce errors
- // we need. An example is the noImplicitAny errors resulting from widening the return expression
- // of a function. Because checking of function expression bodies is deferred, there was never an
- // appropriate time to do this during the main walk of the file (see the comment at the top of
- // checkFunctionExpressionBodies). So it must be done now.
- getReturnTypeOfSignature(getSignatureFromDeclaration(node));
- }
- if (node.body.kind === 223 /* Block */) {
- checkSourceElement(node.body);
- }
- else {
- // From within an async function you can return either a non-promise value or a promise. Any
- // Promise/A+ compatible implementation will always assimilate any foreign promise, so we
- // should not be checking assignability of a promise to the return type. Instead, we need to
- // check assignability of the awaited type of the expression body against the promised type of
- // its return type annotation.
- var exprType = checkExpression(node.body);
- var returnOrPromisedType = returnType && unwrapReturnType(returnType, functionFlags);
- if (returnOrPromisedType) {
- if ((functionFlags & 3 /* AsyncGenerator */) === 2 /* Async */) { // Async function
- var awaitedType = checkAwaitedType(exprType, node.body, ts.Diagnostics.The_return_type_of_an_async_function_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member);
- checkTypeAssignableToAndOptionallyElaborate(awaitedType, returnOrPromisedType, node.body, node.body);
- }
- else { // Normal function
- checkTypeAssignableToAndOptionallyElaborate(exprType, returnOrPromisedType, node.body, node.body);
+ /**
+ * Return true if given node is an expression consisting of an identifier (possibly parenthesized)
+ * that references a for-in variable for an object with numeric property names.
+ */
+ function isForInVariableForNumericPropertyNames(expr) {
+ var e = ts.skipParentheses(expr);
+ if (e.kind === 78 /* Identifier */) {
+ var symbol = getResolvedSymbol(e);
+ if (symbol.flags & 3 /* Variable */) {
+ var child = expr;
+ var node = expr.parent;
+ while (node) {
+ if (node.kind === 238 /* ForInStatement */ &&
+ child === node.statement &&
+ getForInVariableSymbol(node) === symbol &&
+ hasNumericPropertyNames(getTypeOfExpression(node.expression))) {
+ return true;
}
+ child = node;
+ node = node.parent;
}
}
}
+ return false;
}
- function checkArithmeticOperandType(operand, type, diagnostic, isAwaitValid) {
- if (isAwaitValid === void 0) { isAwaitValid = false; }
- if (!isTypeAssignableTo(type, numberOrBigIntType)) {
- var awaitedType = isAwaitValid && getAwaitedTypeOfPromise(type);
- errorAndMaybeSuggestAwait(operand, !!awaitedType && isTypeAssignableTo(awaitedType, numberOrBigIntType), diagnostic);
- return false;
- }
- return true;
+ function checkIndexedAccess(node) {
+ return node.flags & 32 /* OptionalChain */ ? checkElementAccessChain(node) :
+ checkElementAccessExpression(node, checkNonNullExpression(node.expression));
}
- function isReadonlyAssignmentDeclaration(d) {
- if (!ts.isCallExpression(d)) {
- return false;
- }
- if (!ts.isBindableObjectDefinePropertyCall(d)) {
- return false;
+ function checkElementAccessChain(node) {
+ var exprType = checkExpression(node.expression);
+ var nonOptionalType = getOptionalExpressionType(exprType, node.expression);
+ return propagateOptionalTypeMarker(checkElementAccessExpression(node, checkNonNullType(nonOptionalType, node.expression)), node, nonOptionalType !== exprType);
+ }
+ function checkElementAccessExpression(node, exprType) {
+ var objectType = ts.getAssignmentTargetKind(node) !== 0 /* None */ || isMethodAccessForCall(node) ? getWidenedType(exprType) : exprType;
+ var indexExpression = node.argumentExpression;
+ var indexType = checkExpression(indexExpression);
+ if (objectType === errorType || objectType === silentNeverType) {
+ return objectType;
}
- var objectLitType = checkExpressionCached(d.arguments[2]);
- var valueType = getTypeOfPropertyOfType(objectLitType, "value");
- if (valueType) {
- var writableProp = getPropertyOfType(objectLitType, "writable");
- var writableType = writableProp && getTypeOfSymbol(writableProp);
- if (!writableType || writableType === falseType || writableType === regularFalseType) {
- return true;
- }
- // We include this definition whereupon we walk back and check the type at the declaration because
- // The usual definition of `Object.defineProperty` will _not_ cause literal types to be preserved in the
- // argument types, should the type be contextualized by the call itself.
- if (writableProp && writableProp.valueDeclaration && ts.isPropertyAssignment(writableProp.valueDeclaration)) {
- var initializer = writableProp.valueDeclaration.initializer;
- var rawOriginalType = checkExpression(initializer);
- if (rawOriginalType === falseType || rawOriginalType === regularFalseType) {
- return true;
- }
- }
- return false;
+ if (isConstEnumObjectType(objectType) && !ts.isStringLiteralLike(indexExpression)) {
+ error(indexExpression, ts.Diagnostics.A_const_enum_member_can_only_be_accessed_using_a_string_literal);
+ return errorType;
}
- var setProp = getPropertyOfType(objectLitType, "set");
- return !setProp;
- }
- function isReadonlySymbol(symbol) {
- // The following symbols are considered read-only:
- // Properties with a 'readonly' modifier
- // Variables declared with 'const'
- // Get accessors without matching set accessors
- // Enum members
- // Object.defineProperty assignments with writable false or no setter
- // Unions and intersections of the above (unions and intersections eagerly set isReadonly on creation)
- return !!(ts.getCheckFlags(symbol) & 8 /* Readonly */ ||
- symbol.flags & 4 /* Property */ && ts.getDeclarationModifierFlagsFromSymbol(symbol) & 64 /* Readonly */ ||
- symbol.flags & 3 /* Variable */ && getDeclarationNodeFlagsFromSymbol(symbol) & 2 /* Const */ ||
- symbol.flags & 98304 /* Accessor */ && !(symbol.flags & 65536 /* SetAccessor */) ||
- symbol.flags & 8 /* EnumMember */ ||
- ts.some(symbol.declarations, isReadonlyAssignmentDeclaration));
+ var effectiveIndexType = isForInVariableForNumericPropertyNames(indexExpression) ? numberType : indexType;
+ var accessFlags = ts.isAssignmentTarget(node) ?
+ 2 /* Writing */ | (isGenericObjectType(objectType) && !isThisTypeParameter(objectType) ? 1 /* NoIndexSignatures */ : 0) :
+ 0 /* None */;
+ var indexedAccessType = getIndexedAccessTypeOrUndefined(objectType, effectiveIndexType, /*noUncheckedIndexedAccessCandidate*/ undefined, node, accessFlags | 16 /* ExpressionPosition */) || errorType;
+ return checkIndexedAccessIndexType(getFlowTypeOfAccessExpression(node, indexedAccessType.symbol, indexedAccessType, indexExpression), node);
}
- function isAssignmentToReadonlyEntity(expr, symbol, assignmentKind) {
- var _a, _b;
- if (assignmentKind === 0 /* None */) {
- // no assigment means it doesn't matter whether the entity is readonly
+ function checkThatExpressionIsProperSymbolReference(expression, expressionType, reportError) {
+ if (expressionType === errorType) {
+ // There is already an error, so no need to report one.
return false;
}
- if (isReadonlySymbol(symbol)) {
- // Allow assignments to readonly properties within constructors of the same class declaration.
- if (symbol.flags & 4 /* Property */ &&
- ts.isAccessExpression(expr) &&
- expr.expression.kind === 104 /* ThisKeyword */) {
- // Look for if this is the constructor for the class that `symbol` is a property of.
- var ctor = ts.getContainingFunction(expr);
- if (!(ctor && ctor.kind === 162 /* Constructor */)) {
- return true;
- }
- if (symbol.valueDeclaration) {
- var isAssignmentDeclaration_1 = ts.isBinaryExpression(symbol.valueDeclaration);
- var isLocalPropertyDeclaration = ctor.parent === symbol.valueDeclaration.parent;
- var isLocalParameterProperty = ctor === symbol.valueDeclaration.parent;
- var isLocalThisPropertyAssignment = isAssignmentDeclaration_1 && ((_a = symbol.parent) === null || _a === void 0 ? void 0 : _a.valueDeclaration) === ctor.parent;
- var isLocalThisPropertyAssignmentConstructorFunction = isAssignmentDeclaration_1 && ((_b = symbol.parent) === null || _b === void 0 ? void 0 : _b.valueDeclaration) === ctor;
- var isWriteableSymbol = isLocalPropertyDeclaration
- || isLocalParameterProperty
- || isLocalThisPropertyAssignment
- || isLocalThisPropertyAssignmentConstructorFunction;
- return !isWriteableSymbol;
- }
- }
- return true;
+ if (!ts.isWellKnownSymbolSyntactically(expression)) {
+ return false;
}
- if (ts.isAccessExpression(expr)) {
- // references through namespace import should be readonly
- var node = ts.skipParentheses(expr.expression);
- if (node.kind === 75 /* Identifier */) {
- var symbol_2 = getNodeLinks(node).resolvedSymbol;
- if (symbol_2.flags & 2097152 /* Alias */) {
- var declaration = getDeclarationOfAliasSymbol(symbol_2);
- return !!declaration && declaration.kind === 256 /* NamespaceImport */;
- }
+ // Make sure the property type is the primitive symbol type
+ if ((expressionType.flags & 12288 /* ESSymbolLike */) === 0) {
+ if (reportError) {
+ error(expression, ts.Diagnostics.A_computed_property_name_of_the_form_0_must_be_of_type_symbol, ts.getTextOfNode(expression));
}
+ return false;
}
- return false;
- }
- function checkReferenceExpression(expr, invalidReferenceMessage, invalidOptionalChainMessage) {
- // References are combinations of identifiers, parentheses, and property accesses.
- var node = ts.skipOuterExpressions(expr, 6 /* Assertions */ | 1 /* Parentheses */);
- if (node.kind !== 75 /* Identifier */ && !ts.isAccessExpression(node)) {
- error(expr, invalidReferenceMessage);
+ // The name is Symbol., so make sure Symbol actually resolves to the
+ // global Symbol object
+ var leftHandSide = expression.expression;
+ var leftHandSideSymbol = getResolvedSymbol(leftHandSide);
+ if (!leftHandSideSymbol) {
return false;
}
- if (node.flags & 32 /* OptionalChain */) {
- error(expr, invalidOptionalChainMessage);
+ var globalESSymbol = getGlobalESSymbolConstructorSymbol(/*reportErrors*/ true);
+ if (!globalESSymbol) {
+ // Already errored when we tried to look up the symbol
+ return false;
+ }
+ if (leftHandSideSymbol !== globalESSymbol) {
+ if (reportError) {
+ error(leftHandSide, ts.Diagnostics.Symbol_reference_does_not_refer_to_the_global_Symbol_constructor_object);
+ }
return false;
}
return true;
}
- function checkDeleteExpression(node) {
- checkExpression(node.expression);
- var expr = ts.skipParentheses(node.expression);
- if (!ts.isAccessExpression(expr)) {
- error(expr, ts.Diagnostics.The_operand_of_a_delete_operator_must_be_a_property_reference);
- return booleanType;
+ function callLikeExpressionMayHaveTypeArguments(node) {
+ return ts.isCallOrNewExpression(node) || ts.isTaggedTemplateExpression(node) || ts.isJsxOpeningLikeElement(node);
+ }
+ function resolveUntypedCall(node) {
+ if (callLikeExpressionMayHaveTypeArguments(node)) {
+ // Check type arguments even though we will give an error that untyped calls may not accept type arguments.
+ // This gets us diagnostics for the type arguments and marks them as referenced.
+ ts.forEach(node.typeArguments, checkSourceElement);
}
- // eslint-disable-next-line
- if (expr.kind === 194 /* PropertyAccessExpression */ && ts.isPrivateIdentifier(expr.name)) {
- error(expr, ts.Diagnostics.The_operand_of_a_delete_operator_cannot_be_a_private_identifier);
+ if (node.kind === 205 /* TaggedTemplateExpression */) {
+ checkExpression(node.template);
}
- var links = getNodeLinks(expr);
- var symbol = getExportSymbolOfValueSymbolIfExported(links.resolvedSymbol);
- if (symbol && isReadonlySymbol(symbol)) {
- error(expr, ts.Diagnostics.The_operand_of_a_delete_operator_cannot_be_a_read_only_property);
+ else if (ts.isJsxOpeningLikeElement(node)) {
+ checkExpression(node.attributes);
}
- return booleanType;
- }
- function checkTypeOfExpression(node) {
- checkExpression(node.expression);
- return typeofType;
- }
- function checkVoidExpression(node) {
- checkExpression(node.expression);
- return undefinedWideningType;
+ else if (node.kind !== 161 /* Decorator */) {
+ ts.forEach(node.arguments, function (argument) {
+ checkExpression(argument);
+ });
+ }
+ return anySignature;
}
- function isTopLevelAwait(node) {
- var container = ts.getThisContainer(node, /*includeArrowFunctions*/ true);
- return ts.isSourceFile(container);
+ function resolveErrorCall(node) {
+ resolveUntypedCall(node);
+ return unknownSignature;
}
- function checkAwaitExpression(node) {
- // Grammar checking
- if (produceDiagnostics) {
- if (!(node.flags & 32768 /* AwaitContext */)) {
- if (isTopLevelAwait(node)) {
- var sourceFile = ts.getSourceFileOfNode(node);
- if (!hasParseDiagnostics(sourceFile)) {
- var span = void 0;
- if (!ts.isEffectiveExternalModule(sourceFile, compilerOptions)) {
- if (!span)
- span = ts.getSpanOfTokenAtPosition(sourceFile, node.pos);
- var diagnostic = ts.createFileDiagnostic(sourceFile, span.start, span.length, ts.Diagnostics.await_expressions_are_only_allowed_at_the_top_level_of_a_file_when_that_file_is_a_module_but_this_file_has_no_imports_or_exports_Consider_adding_an_empty_export_to_make_this_file_a_module);
- diagnostics.add(diagnostic);
- }
- if ((moduleKind !== ts.ModuleKind.ESNext && moduleKind !== ts.ModuleKind.System) || languageVersion < 4 /* ES2017 */) {
- span = ts.getSpanOfTokenAtPosition(sourceFile, node.pos);
- var diagnostic = ts.createFileDiagnostic(sourceFile, span.start, span.length, ts.Diagnostics.Top_level_await_expressions_are_only_allowed_when_the_module_option_is_set_to_esnext_or_system_and_the_target_option_is_set_to_es2017_or_higher);
- diagnostics.add(diagnostic);
- }
- }
+ // Re-order candidate signatures into the result array. Assumes the result array to be empty.
+ // The candidate list orders groups in reverse, but within a group signatures are kept in declaration order
+ // A nit here is that we reorder only signatures that belong to the same symbol,
+ // so order how inherited signatures are processed is still preserved.
+ // interface A { (x: string): void }
+ // interface B extends A { (x: 'foo'): string }
+ // const b: B;
+ // b('foo') // <- here overloads should be processed as [(x:'foo'): string, (x: string): void]
+ function reorderCandidates(signatures, result, callChainFlags) {
+ var lastParent;
+ var lastSymbol;
+ var cutoffIndex = 0;
+ var index;
+ var specializedIndex = -1;
+ var spliceIndex;
+ ts.Debug.assert(!result.length);
+ for (var _i = 0, signatures_7 = signatures; _i < signatures_7.length; _i++) {
+ var signature = signatures_7[_i];
+ var symbol = signature.declaration && getSymbolOfNode(signature.declaration);
+ var parent = signature.declaration && signature.declaration.parent;
+ if (!lastSymbol || symbol === lastSymbol) {
+ if (lastParent && parent === lastParent) {
+ index = index + 1;
}
else {
- // use of 'await' in non-async function
- var sourceFile = ts.getSourceFileOfNode(node);
- if (!hasParseDiagnostics(sourceFile)) {
- var span = ts.getSpanOfTokenAtPosition(sourceFile, node.pos);
- var diagnostic = ts.createFileDiagnostic(sourceFile, span.start, span.length, ts.Diagnostics.await_expressions_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules);
- var func = ts.getContainingFunction(node);
- if (func && func.kind !== 162 /* Constructor */ && (ts.getFunctionFlags(func) & 2 /* Async */) === 0) {
- var relatedInfo = ts.createDiagnosticForNode(func, ts.Diagnostics.Did_you_mean_to_mark_this_function_as_async);
- ts.addRelatedInfo(diagnostic, relatedInfo);
- }
- diagnostics.add(diagnostic);
- }
+ lastParent = parent;
+ index = cutoffIndex;
}
}
- if (isInParameterInitializerBeforeContainingFunction(node)) {
- error(node, ts.Diagnostics.await_expressions_cannot_be_used_in_a_parameter_initializer);
+ else {
+ // current declaration belongs to a different symbol
+ // set cutoffIndex so re-orderings in the future won't change result set from 0 to cutoffIndex
+ index = cutoffIndex = result.length;
+ lastParent = parent;
}
+ lastSymbol = symbol;
+ // specialized signatures always need to be placed before non-specialized signatures regardless
+ // of the cutoff position; see GH#1133
+ if (signatureHasLiteralTypes(signature)) {
+ specializedIndex++;
+ spliceIndex = specializedIndex;
+ // The cutoff index always needs to be greater than or equal to the specialized signature index
+ // in order to prevent non-specialized signatures from being added before a specialized
+ // signature.
+ cutoffIndex++;
+ }
+ else {
+ spliceIndex = index;
+ }
+ result.splice(spliceIndex, 0, callChainFlags ? getOptionalCallSignature(signature, callChainFlags) : signature);
}
- var operandType = checkExpression(node.expression);
- var awaitedType = checkAwaitedType(operandType, node, ts.Diagnostics.Type_of_await_operand_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member);
- if (awaitedType === operandType && awaitedType !== errorType && !(operandType.flags & 3 /* AnyOrUnknown */)) {
- addErrorOrSuggestion(/*isError*/ false, ts.createDiagnosticForNode(node, ts.Diagnostics.await_has_no_effect_on_the_type_of_this_expression));
- }
- return awaitedType;
}
- function checkPrefixUnaryExpression(node) {
- var operandType = checkExpression(node.operand);
- if (operandType === silentNeverType) {
- return silentNeverType;
+ function isSpreadArgument(arg) {
+ return !!arg && (arg.kind === 220 /* SpreadElement */ || arg.kind === 227 /* SyntheticExpression */ && arg.isSpread);
+ }
+ function getSpreadArgumentIndex(args) {
+ return ts.findIndex(args, isSpreadArgument);
+ }
+ function acceptsVoid(t) {
+ return !!(t.flags & 16384 /* Void */);
+ }
+ function acceptsVoidUndefinedUnknownOrAny(t) {
+ return !!(t.flags & (16384 /* Void */ | 32768 /* Undefined */ | 2 /* Unknown */ | 1 /* Any */));
+ }
+ function hasCorrectArity(node, args, signature, signatureHelpTrailingComma) {
+ if (signatureHelpTrailingComma === void 0) { signatureHelpTrailingComma = false; }
+ var argCount;
+ var callIsIncomplete = false; // In incomplete call we want to be lenient when we have too few arguments
+ var effectiveParameterCount = getParameterCount(signature);
+ var effectiveMinimumArguments = getMinArgumentCount(signature);
+ if (node.kind === 205 /* TaggedTemplateExpression */) {
+ argCount = args.length;
+ if (node.template.kind === 218 /* TemplateExpression */) {
+ // If a tagged template expression lacks a tail literal, the call is incomplete.
+ // Specifically, a template only can end in a TemplateTail or a Missing literal.
+ var lastSpan = ts.last(node.template.templateSpans); // we should always have at least one span.
+ callIsIncomplete = ts.nodeIsMissing(lastSpan.literal) || !!lastSpan.literal.isUnterminated;
+ }
+ else {
+ // If the template didn't end in a backtick, or its beginning occurred right prior to EOF,
+ // then this might actually turn out to be a TemplateHead in the future;
+ // so we consider the call to be incomplete.
+ var templateLiteral = node.template;
+ ts.Debug.assert(templateLiteral.kind === 14 /* NoSubstitutionTemplateLiteral */);
+ callIsIncomplete = !!templateLiteral.isUnterminated;
+ }
}
- switch (node.operand.kind) {
- case 8 /* NumericLiteral */:
- switch (node.operator) {
- case 40 /* MinusToken */:
- return getFreshTypeOfLiteralType(getLiteralType(-node.operand.text));
- case 39 /* PlusToken */:
- return getFreshTypeOfLiteralType(getLiteralType(+node.operand.text));
- }
- break;
- case 9 /* BigIntLiteral */:
- if (node.operator === 40 /* MinusToken */) {
- return getFreshTypeOfLiteralType(getLiteralType({
- negative: true,
- base10Value: ts.parsePseudoBigInt(node.operand.text)
- }));
- }
+ else if (node.kind === 161 /* Decorator */) {
+ argCount = getDecoratorArgumentCount(node, signature);
}
- switch (node.operator) {
- case 39 /* PlusToken */:
- case 40 /* MinusToken */:
- case 54 /* TildeToken */:
- checkNonNullType(operandType, node.operand);
- if (maybeTypeOfKind(operandType, 12288 /* ESSymbolLike */)) {
- error(node.operand, ts.Diagnostics.The_0_operator_cannot_be_applied_to_type_symbol, ts.tokenToString(node.operator));
- }
- if (node.operator === 39 /* PlusToken */) {
- if (maybeTypeOfKind(operandType, 2112 /* BigIntLike */)) {
- error(node.operand, ts.Diagnostics.Operator_0_cannot_be_applied_to_type_1, ts.tokenToString(node.operator), typeToString(getBaseTypeOfLiteralType(operandType)));
- }
- return numberType;
- }
- return getUnaryResultType(operandType);
- case 53 /* ExclamationToken */:
- checkTruthinessExpression(node.operand);
- var facts = getTypeFacts(operandType) & (4194304 /* Truthy */ | 8388608 /* Falsy */);
- return facts === 4194304 /* Truthy */ ? falseType :
- facts === 8388608 /* Falsy */ ? trueType :
- booleanType;
- case 45 /* PlusPlusToken */:
- case 46 /* MinusMinusToken */:
- var ok = checkArithmeticOperandType(node.operand, checkNonNullType(operandType, node.operand), ts.Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_bigint_or_an_enum_type);
- if (ok) {
- // run check only if former checks succeeded to avoid reporting cascading errors
- checkReferenceExpression(node.operand, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_or_a_property_access, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_may_not_be_an_optional_property_access);
- }
- return getUnaryResultType(operandType);
+ else if (ts.isJsxOpeningLikeElement(node)) {
+ callIsIncomplete = node.attributes.end === node.end;
+ if (callIsIncomplete) {
+ return true;
+ }
+ argCount = effectiveMinimumArguments === 0 ? args.length : 1;
+ effectiveParameterCount = args.length === 0 ? effectiveParameterCount : 1; // class may have argumentless ctor functions - still resolve ctor and compare vs props member type
+ effectiveMinimumArguments = Math.min(effectiveMinimumArguments, 1); // sfc may specify context argument - handled by framework and not typechecked
}
- return errorType;
- }
- function checkPostfixUnaryExpression(node) {
- var operandType = checkExpression(node.operand);
- if (operandType === silentNeverType) {
- return silentNeverType;
+ else if (!node.arguments) {
+ // This only happens when we have something of the form: 'new C'
+ ts.Debug.assert(node.kind === 204 /* NewExpression */);
+ return getMinArgumentCount(signature) === 0;
}
- var ok = checkArithmeticOperandType(node.operand, checkNonNullType(operandType, node.operand), ts.Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_bigint_or_an_enum_type);
- if (ok) {
- // run check only if former checks succeeded to avoid reporting cascading errors
- checkReferenceExpression(node.operand, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_or_a_property_access, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_may_not_be_an_optional_property_access);
+ else {
+ argCount = signatureHelpTrailingComma ? args.length + 1 : args.length;
+ // If we are missing the close parenthesis, the call is incomplete.
+ callIsIncomplete = node.arguments.end === node.end;
+ // If a spread argument is present, check that it corresponds to a rest parameter or at least that it's in the valid range.
+ var spreadArgIndex = getSpreadArgumentIndex(args);
+ if (spreadArgIndex >= 0) {
+ return spreadArgIndex >= getMinArgumentCount(signature) && (hasEffectiveRestParameter(signature) || spreadArgIndex < getParameterCount(signature));
+ }
}
- return getUnaryResultType(operandType);
- }
- function getUnaryResultType(operandType) {
- if (maybeTypeOfKind(operandType, 2112 /* BigIntLike */)) {
- return isTypeAssignableToKind(operandType, 3 /* AnyOrUnknown */) || maybeTypeOfKind(operandType, 296 /* NumberLike */)
- ? numberOrBigIntType
- : bigintType;
+ // Too many arguments implies incorrect arity.
+ if (!hasEffectiveRestParameter(signature) && argCount > effectiveParameterCount) {
+ return false;
}
- // If it's not a bigint type, implicit coercion will result in a number
- return numberType;
- }
- // Return true if type might be of the given kind. A union or intersection type might be of a given
- // kind if at least one constituent type is of the given kind.
- function maybeTypeOfKind(type, kind) {
- if (type.flags & kind) {
+ // If the call is incomplete, we should skip the lower bound check.
+ // JSX signatures can have extra parameters provided by the library which we don't check
+ if (callIsIncomplete || argCount >= effectiveMinimumArguments) {
return true;
}
- if (type.flags & 3145728 /* UnionOrIntersection */) {
- var types = type.types;
- for (var _i = 0, types_19 = types; _i < types_19.length; _i++) {
- var t = types_19[_i];
- if (maybeTypeOfKind(t, kind)) {
- return true;
- }
+ for (var i = argCount; i < effectiveMinimumArguments; i++) {
+ var type = getTypeAtPosition(signature, i);
+ if (filterType(type, ts.isInJSFile(node) && !strictNullChecks ? acceptsVoidUndefinedUnknownOrAny : acceptsVoid).flags & 131072 /* Never */) {
+ return false;
}
}
- return false;
+ return true;
}
- function isTypeAssignableToKind(source, kind, strict) {
- if (source.flags & kind) {
- return true;
- }
- if (strict && source.flags & (3 /* AnyOrUnknown */ | 16384 /* Void */ | 32768 /* Undefined */ | 65536 /* Null */)) {
- return false;
- }
- return !!(kind & 296 /* NumberLike */) && isTypeAssignableTo(source, numberType) ||
- !!(kind & 2112 /* BigIntLike */) && isTypeAssignableTo(source, bigintType) ||
- !!(kind & 132 /* StringLike */) && isTypeAssignableTo(source, stringType) ||
- !!(kind & 528 /* BooleanLike */) && isTypeAssignableTo(source, booleanType) ||
- !!(kind & 16384 /* Void */) && isTypeAssignableTo(source, voidType) ||
- !!(kind & 131072 /* Never */) && isTypeAssignableTo(source, neverType) ||
- !!(kind & 65536 /* Null */) && isTypeAssignableTo(source, nullType) ||
- !!(kind & 32768 /* Undefined */) && isTypeAssignableTo(source, undefinedType) ||
- !!(kind & 4096 /* ESSymbol */) && isTypeAssignableTo(source, esSymbolType) ||
- !!(kind & 67108864 /* NonPrimitive */) && isTypeAssignableTo(source, nonPrimitiveType);
+ function hasCorrectTypeArgumentArity(signature, typeArguments) {
+ // If the user supplied type arguments, but the number of type arguments does not match
+ // the declared number of type parameters, the call has an incorrect arity.
+ var numTypeParameters = ts.length(signature.typeParameters);
+ var minTypeArgumentCount = getMinTypeArgumentCount(signature.typeParameters);
+ return !ts.some(typeArguments) ||
+ (typeArguments.length >= minTypeArgumentCount && typeArguments.length <= numTypeParameters);
}
- function allTypesAssignableToKind(source, kind, strict) {
- return source.flags & 1048576 /* Union */ ?
- ts.every(source.types, function (subType) { return allTypesAssignableToKind(subType, kind, strict); }) :
- isTypeAssignableToKind(source, kind, strict);
+ // If type has a single call signature and no other members, return that signature. Otherwise, return undefined.
+ function getSingleCallSignature(type) {
+ return getSingleSignature(type, 0 /* Call */, /*allowMembers*/ false);
}
- function isConstEnumObjectType(type) {
- return !!(ts.getObjectFlags(type) & 16 /* Anonymous */) && !!type.symbol && isConstEnumSymbol(type.symbol);
+ function getSingleCallOrConstructSignature(type) {
+ return getSingleSignature(type, 0 /* Call */, /*allowMembers*/ false) ||
+ getSingleSignature(type, 1 /* Construct */, /*allowMembers*/ false);
}
- function isConstEnumSymbol(symbol) {
- return (symbol.flags & 128 /* ConstEnum */) !== 0;
+ function getSingleSignature(type, kind, allowMembers) {
+ if (type.flags & 524288 /* Object */) {
+ var resolved = resolveStructuredTypeMembers(type);
+ if (allowMembers || resolved.properties.length === 0 && !resolved.stringIndexInfo && !resolved.numberIndexInfo) {
+ if (kind === 0 /* Call */ && resolved.callSignatures.length === 1 && resolved.constructSignatures.length === 0) {
+ return resolved.callSignatures[0];
+ }
+ if (kind === 1 /* Construct */ && resolved.constructSignatures.length === 1 && resolved.callSignatures.length === 0) {
+ return resolved.constructSignatures[0];
+ }
+ }
+ }
+ return undefined;
}
- function checkInstanceOfExpression(left, right, leftType, rightType) {
- if (leftType === silentNeverType || rightType === silentNeverType) {
- return silentNeverType;
+ // Instantiate a generic signature in the context of a non-generic signature (section 3.8.5 in TypeScript spec)
+ function instantiateSignatureInContextOf(signature, contextualSignature, inferenceContext, compareTypes) {
+ var context = createInferenceContext(signature.typeParameters, signature, 0 /* None */, compareTypes);
+ // We clone the inferenceContext to avoid fixing. For example, when the source signature is (x: T) => T[] and
+ // the contextual signature is (...args: A) => B, we want to infer the element type of A's constraint (say 'any')
+ // for T but leave it possible to later infer '[any]' back to A.
+ var restType = getEffectiveRestType(contextualSignature);
+ var mapper = inferenceContext && (restType && restType.flags & 262144 /* TypeParameter */ ? inferenceContext.nonFixingMapper : inferenceContext.mapper);
+ var sourceSignature = mapper ? instantiateSignature(contextualSignature, mapper) : contextualSignature;
+ applyToParameterTypes(sourceSignature, signature, function (source, target) {
+ // Type parameters from outer context referenced by source type are fixed by instantiation of the source type
+ inferTypes(context.inferences, source, target);
+ });
+ if (!inferenceContext) {
+ applyToReturnTypes(contextualSignature, signature, function (source, target) {
+ inferTypes(context.inferences, source, target, 64 /* ReturnType */);
+ });
}
- // TypeScript 1.0 spec (April 2014): 4.15.4
- // The instanceof operator requires the left operand to be of type Any, an object type, or a type parameter type,
- // and the right operand to be of type Any, a subtype of the 'Function' interface type, or have a call or construct signature.
- // The result is always of the Boolean primitive type.
- // NOTE: do not raise error if leftType is unknown as related error was already reported
- if (!isTypeAny(leftType) &&
- allTypesAssignableToKind(leftType, 131068 /* Primitive */)) {
- error(left, ts.Diagnostics.The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
+ return getSignatureInstantiation(signature, getInferredTypes(context), ts.isInJSFile(contextualSignature.declaration));
+ }
+ function inferJsxTypeArguments(node, signature, checkMode, context) {
+ var paramType = getEffectiveFirstArgumentForJsxSignature(signature, node);
+ var checkAttrType = checkExpressionWithContextualType(node.attributes, paramType, context, checkMode);
+ inferTypes(context.inferences, checkAttrType, paramType);
+ return getInferredTypes(context);
+ }
+ function inferTypeArguments(node, signature, args, checkMode, context) {
+ if (ts.isJsxOpeningLikeElement(node)) {
+ return inferJsxTypeArguments(node, signature, checkMode, context);
}
- // NOTE: do not raise error if right is unknown as related error was already reported
- if (!(isTypeAny(rightType) || typeHasCallOrConstructSignatures(rightType) || isTypeSubtypeOf(rightType, globalFunctionType))) {
- error(right, ts.Diagnostics.The_right_hand_side_of_an_instanceof_expression_must_be_of_type_any_or_of_a_type_assignable_to_the_Function_interface_type);
+ // If a contextual type is available, infer from that type to the return type of the call expression. For
+ // example, given a 'function wrap(cb: (x: T) => U): (x: T) => U' and a call expression
+ // 'let f: (x: string) => number = wrap(s => s.length)', we infer from the declared type of 'f' to the
+ // return type of 'wrap'.
+ if (node.kind !== 161 /* Decorator */) {
+ var contextualType = getContextualType(node, ts.every(signature.typeParameters, function (p) { return !!getDefaultFromTypeParameter(p); }) ? 8 /* SkipBindingPatterns */ : 0 /* None */);
+ if (contextualType) {
+ // We clone the inference context to avoid disturbing a resolution in progress for an
+ // outer call expression. Effectively we just want a snapshot of whatever has been
+ // inferred for any outer call expression so far.
+ var outerContext = getInferenceContext(node);
+ var outerMapper = getMapperFromContext(cloneInferenceContext(outerContext, 1 /* NoDefault */));
+ var instantiatedType = instantiateType(contextualType, outerMapper);
+ // If the contextual type is a generic function type with a single call signature, we
+ // instantiate the type with its own type parameters and type arguments. This ensures that
+ // the type parameters are not erased to type any during type inference such that they can
+ // be inferred as actual types from the contextual type. For example:
+ // declare function arrayMap(f: (x: T) => U): (a: T[]) => U[];
+ // const boxElements: (a: A[]) => { value: A }[] = arrayMap(value => ({ value }));
+ // Above, the type of the 'value' parameter is inferred to be 'A'.
+ var contextualSignature = getSingleCallSignature(instantiatedType);
+ var inferenceSourceType = contextualSignature && contextualSignature.typeParameters ?
+ getOrCreateTypeFromSignature(getSignatureInstantiationWithoutFillingInTypeArguments(contextualSignature, contextualSignature.typeParameters)) :
+ instantiatedType;
+ var inferenceTargetType = getReturnTypeOfSignature(signature);
+ // Inferences made from return types have lower priority than all other inferences.
+ inferTypes(context.inferences, inferenceSourceType, inferenceTargetType, 64 /* ReturnType */);
+ // Create a type mapper for instantiating generic contextual types using the inferences made
+ // from the return type. We need a separate inference pass here because (a) instantiation of
+ // the source type uses the outer context's return mapper (which excludes inferences made from
+ // outer arguments), and (b) we don't want any further inferences going into this context.
+ var returnContext = createInferenceContext(signature.typeParameters, signature, context.flags);
+ var returnSourceType = instantiateType(contextualType, outerContext && outerContext.returnMapper);
+ inferTypes(returnContext.inferences, returnSourceType, inferenceTargetType);
+ context.returnMapper = ts.some(returnContext.inferences, hasInferenceCandidates) ? getMapperFromContext(cloneInferredPartOfContext(returnContext)) : undefined;
+ }
}
- return booleanType;
+ var restType = getNonArrayRestType(signature);
+ var argCount = restType ? Math.min(getParameterCount(signature) - 1, args.length) : args.length;
+ if (restType && restType.flags & 262144 /* TypeParameter */) {
+ var info = ts.find(context.inferences, function (info) { return info.typeParameter === restType; });
+ if (info) {
+ info.impliedArity = ts.findIndex(args, isSpreadArgument, argCount) < 0 ? args.length - argCount : undefined;
+ }
+ }
+ var thisType = getThisTypeOfSignature(signature);
+ if (thisType) {
+ var thisArgumentNode = getThisArgumentOfCall(node);
+ var thisArgumentType = thisArgumentNode ? checkExpression(thisArgumentNode) : voidType;
+ inferTypes(context.inferences, thisArgumentType, thisType);
+ }
+ for (var i = 0; i < argCount; i++) {
+ var arg = args[i];
+ if (arg.kind !== 222 /* OmittedExpression */) {
+ var paramType = getTypeAtPosition(signature, i);
+ var argType = checkExpressionWithContextualType(arg, paramType, context, checkMode);
+ inferTypes(context.inferences, argType, paramType);
+ }
+ }
+ if (restType) {
+ var spreadType = getSpreadArgumentType(args, argCount, args.length, restType, context, checkMode);
+ inferTypes(context.inferences, spreadType, restType);
+ }
+ return getInferredTypes(context);
}
- function checkInExpression(left, right, leftType, rightType) {
- if (leftType === silentNeverType || rightType === silentNeverType) {
- return silentNeverType;
+ function getMutableArrayOrTupleType(type) {
+ return type.flags & 1048576 /* Union */ ? mapType(type, getMutableArrayOrTupleType) :
+ type.flags & 1 /* Any */ || isMutableArrayOrTuple(getBaseConstraintOfType(type) || type) ? type :
+ isTupleType(type) ? createTupleType(getTypeArguments(type), type.target.elementFlags, /*readonly*/ false, type.target.labeledElementDeclarations) :
+ createTupleType([type], [8 /* Variadic */]);
+ }
+ function getSpreadArgumentType(args, index, argCount, restType, context, checkMode) {
+ if (index >= argCount - 1) {
+ var arg = args[argCount - 1];
+ if (isSpreadArgument(arg)) {
+ // We are inferring from a spread expression in the last argument position, i.e. both the parameter
+ // and the argument are ...x forms.
+ return getMutableArrayOrTupleType(arg.kind === 227 /* SyntheticExpression */ ? arg.type :
+ checkExpressionWithContextualType(arg.expression, restType, context, checkMode));
+ }
}
- leftType = checkNonNullType(leftType, left);
- rightType = checkNonNullType(rightType, right);
- // TypeScript 1.0 spec (April 2014): 4.15.5
- // The in operator requires the left operand to be of type Any, the String primitive type, or the Number primitive type,
- // and the right operand to be of type Any, an object type, or a type parameter type.
- // The result is always of the Boolean primitive type.
- if (!(isTypeComparableTo(leftType, stringType) || isTypeAssignableToKind(leftType, 296 /* NumberLike */ | 12288 /* ESSymbolLike */))) {
- error(left, ts.Diagnostics.The_left_hand_side_of_an_in_expression_must_be_of_type_any_string_number_or_symbol);
+ var types = [];
+ var flags = [];
+ var names = [];
+ for (var i = index; i < argCount; i++) {
+ var arg = args[i];
+ if (isSpreadArgument(arg)) {
+ var spreadType = arg.kind === 227 /* SyntheticExpression */ ? arg.type : checkExpression(arg.expression);
+ if (isArrayLikeType(spreadType)) {
+ types.push(spreadType);
+ flags.push(8 /* Variadic */);
+ }
+ else {
+ types.push(checkIteratedTypeOrElementType(33 /* Spread */, spreadType, undefinedType, arg.kind === 220 /* SpreadElement */ ? arg.expression : arg));
+ flags.push(4 /* Rest */);
+ }
+ }
+ else {
+ var contextualType = getIndexedAccessType(restType, getLiteralType(i - index));
+ var argType = checkExpressionWithContextualType(arg, contextualType, context, checkMode);
+ var hasPrimitiveContextualType = maybeTypeOfKind(contextualType, 131068 /* Primitive */ | 4194304 /* Index */ | 134217728 /* TemplateLiteral */ | 268435456 /* StringMapping */);
+ types.push(hasPrimitiveContextualType ? getRegularTypeOfLiteralType(argType) : getWidenedLiteralType(argType));
+ flags.push(1 /* Required */);
+ }
+ if (arg.kind === 227 /* SyntheticExpression */ && arg.tupleNameSource) {
+ names.push(arg.tupleNameSource);
+ }
}
- if (!allTypesAssignableToKind(rightType, 67108864 /* NonPrimitive */ | 58982400 /* InstantiableNonPrimitive */)) {
- error(right, ts.Diagnostics.The_right_hand_side_of_an_in_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
+ return createTupleType(types, flags, /*readonly*/ false, ts.length(names) === ts.length(types) ? names : undefined);
+ }
+ function checkTypeArguments(signature, typeArgumentNodes, reportErrors, headMessage) {
+ var isJavascript = ts.isInJSFile(signature.declaration);
+ var typeParameters = signature.typeParameters;
+ var typeArgumentTypes = fillMissingTypeArguments(ts.map(typeArgumentNodes, getTypeFromTypeNode), typeParameters, getMinTypeArgumentCount(typeParameters), isJavascript);
+ var mapper;
+ for (var i = 0; i < typeArgumentNodes.length; i++) {
+ ts.Debug.assert(typeParameters[i] !== undefined, "Should not call checkTypeArguments with too many type arguments");
+ var constraint = getConstraintOfTypeParameter(typeParameters[i]);
+ if (constraint) {
+ var errorInfo = reportErrors && headMessage ? (function () { return ts.chainDiagnosticMessages(/*details*/ undefined, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1); }) : undefined;
+ var typeArgumentHeadMessage = headMessage || ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1;
+ if (!mapper) {
+ mapper = createTypeMapper(typeParameters, typeArgumentTypes);
+ }
+ var typeArgument = typeArgumentTypes[i];
+ if (!checkTypeAssignableTo(typeArgument, getTypeWithThisArgument(instantiateType(constraint, mapper), typeArgument), reportErrors ? typeArgumentNodes[i] : undefined, typeArgumentHeadMessage, errorInfo)) {
+ return undefined;
+ }
+ }
}
- return booleanType;
+ return typeArgumentTypes;
}
- function checkObjectLiteralAssignment(node, sourceType, rightIsThis) {
- var properties = node.properties;
- if (strictNullChecks && properties.length === 0) {
- return checkNonNullType(sourceType, node);
+ function getJsxReferenceKind(node) {
+ if (isJsxIntrinsicIdentifier(node.tagName)) {
+ return 2 /* Mixed */;
}
- for (var i = 0; i < properties.length; i++) {
- checkObjectLiteralDestructuringPropertyAssignment(node, sourceType, i, properties, rightIsThis);
+ var tagType = getApparentType(checkExpression(node.tagName));
+ if (ts.length(getSignaturesOfType(tagType, 1 /* Construct */))) {
+ return 0 /* Component */;
}
- return sourceType;
+ if (ts.length(getSignaturesOfType(tagType, 0 /* Call */))) {
+ return 1 /* Function */;
+ }
+ return 2 /* Mixed */;
}
- /** Note: If property cannot be a SpreadAssignment, then allProperties does not need to be provided */
- function checkObjectLiteralDestructuringPropertyAssignment(node, objectLiteralType, propertyIndex, allProperties, rightIsThis) {
- if (rightIsThis === void 0) { rightIsThis = false; }
- var properties = node.properties;
- var property = properties[propertyIndex];
- if (property.kind === 281 /* PropertyAssignment */ || property.kind === 282 /* ShorthandPropertyAssignment */) {
- var name = property.name;
- var exprType = getLiteralTypeFromPropertyName(name);
- if (isTypeUsableAsPropertyName(exprType)) {
- var text = getPropertyNameFromType(exprType);
- var prop = getPropertyOfType(objectLiteralType, text);
- if (prop) {
- markPropertyAsReferenced(prop, property, rightIsThis);
- checkPropertyAccessibility(property, /*isSuper*/ false, objectLiteralType, prop);
+ /**
+ * Check if the given signature can possibly be a signature called by the JSX opening-like element.
+ * @param node a JSX opening-like element we are trying to figure its call signature
+ * @param signature a candidate signature we are trying whether it is a call signature
+ * @param relation a relationship to check parameter and argument type
+ */
+ function checkApplicableSignatureForJsxOpeningLikeElement(node, signature, relation, checkMode, reportErrors, containingMessageChain, errorOutputContainer) {
+ // Stateless function components can have maximum of three arguments: "props", "context", and "updater".
+ // However "context" and "updater" are implicit and can't be specify by users. Only the first parameter, props,
+ // can be specified by users through attributes property.
+ var paramType = getEffectiveFirstArgumentForJsxSignature(signature, node);
+ var attributesType = checkExpressionWithContextualType(node.attributes, paramType, /*inferenceContext*/ undefined, checkMode);
+ return checkTagNameDoesNotExpectTooManyArguments() && checkTypeRelatedToAndOptionallyElaborate(attributesType, paramType, relation, reportErrors ? node.tagName : undefined, node.attributes,
+ /*headMessage*/ undefined, containingMessageChain, errorOutputContainer);
+ function checkTagNameDoesNotExpectTooManyArguments() {
+ var _a;
+ var tagType = ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node) && !isJsxIntrinsicIdentifier(node.tagName) ? checkExpression(node.tagName) : undefined;
+ if (!tagType) {
+ return true;
+ }
+ var tagCallSignatures = getSignaturesOfType(tagType, 0 /* Call */);
+ if (!ts.length(tagCallSignatures)) {
+ return true;
+ }
+ var factory = getJsxFactoryEntity(node);
+ if (!factory) {
+ return true;
+ }
+ var factorySymbol = resolveEntityName(factory, 111551 /* Value */, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, node);
+ if (!factorySymbol) {
+ return true;
+ }
+ var factoryType = getTypeOfSymbol(factorySymbol);
+ var callSignatures = getSignaturesOfType(factoryType, 0 /* Call */);
+ if (!ts.length(callSignatures)) {
+ return true;
+ }
+ var hasFirstParamSignatures = false;
+ var maxParamCount = 0;
+ // Check that _some_ first parameter expects a FC-like thing, and that some overload of the SFC expects an acceptable number of arguments
+ for (var _i = 0, callSignatures_1 = callSignatures; _i < callSignatures_1.length; _i++) {
+ var sig = callSignatures_1[_i];
+ var firstparam = getTypeAtPosition(sig, 0);
+ var signaturesOfParam = getSignaturesOfType(firstparam, 0 /* Call */);
+ if (!ts.length(signaturesOfParam))
+ continue;
+ for (var _b = 0, signaturesOfParam_1 = signaturesOfParam; _b < signaturesOfParam_1.length; _b++) {
+ var paramSig = signaturesOfParam_1[_b];
+ hasFirstParamSignatures = true;
+ if (hasEffectiveRestParameter(paramSig)) {
+ return true; // some signature has a rest param, so function components can have an arbitrary number of arguments
+ }
+ var paramCount = getParameterCount(paramSig);
+ if (paramCount > maxParamCount) {
+ maxParamCount = paramCount;
+ }
}
}
- var elementType = getIndexedAccessType(objectLiteralType, exprType, name);
- var type = getFlowTypeOfDestructuring(property, elementType);
- return checkDestructuringAssignment(property.kind === 282 /* ShorthandPropertyAssignment */ ? property : property.initializer, type);
- }
- else if (property.kind === 283 /* SpreadAssignment */) {
- if (propertyIndex < properties.length - 1) {
- error(property, ts.Diagnostics.A_rest_element_must_be_last_in_a_destructuring_pattern);
+ if (!hasFirstParamSignatures) {
+ // Not a single signature had a first parameter which expected a signature - for back compat, and
+ // to guard against generic factories which won't have signatures directly, do not error
+ return true;
}
- else {
- if (languageVersion < 99 /* ESNext */) {
- checkExternalEmitHelpers(property, 4 /* Rest */);
+ var absoluteMinArgCount = Infinity;
+ for (var _c = 0, tagCallSignatures_1 = tagCallSignatures; _c < tagCallSignatures_1.length; _c++) {
+ var tagSig = tagCallSignatures_1[_c];
+ var tagRequiredArgCount = getMinArgumentCount(tagSig);
+ if (tagRequiredArgCount < absoluteMinArgCount) {
+ absoluteMinArgCount = tagRequiredArgCount;
}
- var nonRestNames = [];
- if (allProperties) {
- for (var _i = 0, allProperties_1 = allProperties; _i < allProperties_1.length; _i++) {
- var otherProperty = allProperties_1[_i];
- if (!ts.isSpreadAssignment(otherProperty)) {
- nonRestNames.push(otherProperty.name);
- }
- }
+ }
+ if (absoluteMinArgCount <= maxParamCount) {
+ return true; // some signature accepts the number of arguments the function component provides
+ }
+ if (reportErrors) {
+ var diag = ts.createDiagnosticForNode(node.tagName, ts.Diagnostics.Tag_0_expects_at_least_1_arguments_but_the_JSX_factory_2_provides_at_most_3, ts.entityNameToString(node.tagName), absoluteMinArgCount, ts.entityNameToString(factory), maxParamCount);
+ var tagNameDeclaration = (_a = getSymbolAtLocation(node.tagName)) === null || _a === void 0 ? void 0 : _a.valueDeclaration;
+ if (tagNameDeclaration) {
+ ts.addRelatedInfo(diag, ts.createDiagnosticForNode(tagNameDeclaration, ts.Diagnostics._0_is_declared_here, ts.entityNameToString(node.tagName)));
+ }
+ if (errorOutputContainer && errorOutputContainer.skipLogging) {
+ (errorOutputContainer.errors || (errorOutputContainer.errors = [])).push(diag);
+ }
+ if (!errorOutputContainer.skipLogging) {
+ diagnostics.add(diag);
}
- var type = getRestType(objectLiteralType, nonRestNames, objectLiteralType.symbol);
- checkGrammarForDisallowedTrailingComma(allProperties, ts.Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma);
- return checkDestructuringAssignment(property.expression, type);
}
- }
- else {
- error(property, ts.Diagnostics.Property_assignment_expected);
+ return false;
}
}
- function checkArrayLiteralAssignment(node, sourceType, checkMode) {
- var elements = node.elements;
- if (languageVersion < 2 /* ES2015 */ && compilerOptions.downlevelIteration) {
- checkExternalEmitHelpers(node, 512 /* Read */);
+ function getSignatureApplicabilityError(node, args, signature, relation, checkMode, reportErrors, containingMessageChain) {
+ var errorOutputContainer = { errors: undefined, skipLogging: true };
+ if (ts.isJsxOpeningLikeElement(node)) {
+ if (!checkApplicableSignatureForJsxOpeningLikeElement(node, signature, relation, checkMode, reportErrors, containingMessageChain, errorOutputContainer)) {
+ ts.Debug.assert(!reportErrors || !!errorOutputContainer.errors, "jsx should have errors when reporting errors");
+ return errorOutputContainer.errors || ts.emptyArray;
+ }
+ return undefined;
}
- // This elementType will be used if the specific property corresponding to this index is not
- // present (aka the tuple element property). This call also checks that the parentType is in
- // fact an iterable or array (depending on target language).
- var elementType = checkIteratedTypeOrElementType(65 /* Destructuring */, sourceType, undefinedType, node) || errorType;
- for (var i = 0; i < elements.length; i++) {
- checkArrayLiteralDestructuringElementAssignment(node, sourceType, i, elementType, checkMode);
+ var thisType = getThisTypeOfSignature(signature);
+ if (thisType && thisType !== voidType && node.kind !== 204 /* NewExpression */) {
+ // If the called expression is not of the form `x.f` or `x["f"]`, then sourceType = voidType
+ // If the signature's 'this' type is voidType, then the check is skipped -- anything is compatible.
+ // If the expression is a new expression, then the check is skipped.
+ var thisArgumentNode = getThisArgumentOfCall(node);
+ var thisArgumentType = void 0;
+ if (thisArgumentNode) {
+ thisArgumentType = checkExpression(thisArgumentNode);
+ if (ts.isOptionalChainRoot(thisArgumentNode.parent)) {
+ thisArgumentType = getNonNullableType(thisArgumentType);
+ }
+ else if (ts.isOptionalChain(thisArgumentNode.parent)) {
+ thisArgumentType = removeOptionalTypeMarker(thisArgumentType);
+ }
+ }
+ else {
+ thisArgumentType = voidType;
+ }
+ var errorNode = reportErrors ? (thisArgumentNode || node) : undefined;
+ var headMessage_1 = ts.Diagnostics.The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1;
+ if (!checkTypeRelatedTo(thisArgumentType, thisType, relation, errorNode, headMessage_1, containingMessageChain, errorOutputContainer)) {
+ ts.Debug.assert(!reportErrors || !!errorOutputContainer.errors, "this parameter should have errors when reporting errors");
+ return errorOutputContainer.errors || ts.emptyArray;
+ }
}
- return sourceType;
- }
- function checkArrayLiteralDestructuringElementAssignment(node, sourceType, elementIndex, elementType, checkMode) {
- var elements = node.elements;
- var element = elements[elementIndex];
- if (element.kind !== 215 /* OmittedExpression */) {
- if (element.kind !== 213 /* SpreadElement */) {
- var indexType = getLiteralType(elementIndex);
- if (isArrayLikeType(sourceType)) {
- // We create a synthetic expression so that getIndexedAccessType doesn't get confused
- // when the element is a SyntaxKind.ElementAccessExpression.
- var accessFlags = hasDefaultValue(element) ? 8 /* NoTupleBoundsCheck */ : 0;
- var elementType_2 = getIndexedAccessTypeOrUndefined(sourceType, indexType, createSyntheticExpression(element, indexType), accessFlags) || errorType;
- var assignedType = hasDefaultValue(element) ? getTypeWithFacts(elementType_2, 524288 /* NEUndefined */) : elementType_2;
- var type = getFlowTypeOfDestructuring(element, assignedType);
- return checkDestructuringAssignment(element, type, checkMode);
+ var headMessage = ts.Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1;
+ var restType = getNonArrayRestType(signature);
+ var argCount = restType ? Math.min(getParameterCount(signature) - 1, args.length) : args.length;
+ for (var i = 0; i < argCount; i++) {
+ var arg = args[i];
+ if (arg.kind !== 222 /* OmittedExpression */) {
+ var paramType = getTypeAtPosition(signature, i);
+ var argType = checkExpressionWithContextualType(arg, paramType, /*inferenceContext*/ undefined, checkMode);
+ // If one or more arguments are still excluded (as indicated by CheckMode.SkipContextSensitive),
+ // we obtain the regular type of any object literal arguments because we may not have inferred complete
+ // parameter types yet and therefore excess property checks may yield false positives (see #17041).
+ var checkArgType = checkMode & 4 /* SkipContextSensitive */ ? getRegularTypeOfObjectLiteral(argType) : argType;
+ if (!checkTypeRelatedToAndOptionallyElaborate(checkArgType, paramType, relation, reportErrors ? arg : undefined, arg, headMessage, containingMessageChain, errorOutputContainer)) {
+ ts.Debug.assert(!reportErrors || !!errorOutputContainer.errors, "parameter should have errors when reporting errors");
+ maybeAddMissingAwaitInfo(arg, checkArgType, paramType);
+ return errorOutputContainer.errors || ts.emptyArray;
}
- return checkDestructuringAssignment(element, elementType, checkMode);
}
- if (elementIndex < elements.length - 1) {
- error(element, ts.Diagnostics.A_rest_element_must_be_last_in_a_destructuring_pattern);
+ }
+ if (restType) {
+ var spreadType = getSpreadArgumentType(args, argCount, args.length, restType, /*context*/ undefined, checkMode);
+ var errorNode = reportErrors ? argCount < args.length ? args[argCount] : node : undefined;
+ if (!checkTypeRelatedTo(spreadType, restType, relation, errorNode, headMessage, /*containingMessageChain*/ undefined, errorOutputContainer)) {
+ ts.Debug.assert(!reportErrors || !!errorOutputContainer.errors, "rest parameter should have errors when reporting errors");
+ maybeAddMissingAwaitInfo(errorNode, spreadType, restType);
+ return errorOutputContainer.errors || ts.emptyArray;
}
- else {
- var restExpression = element.expression;
- if (restExpression.kind === 209 /* BinaryExpression */ && restExpression.operatorToken.kind === 62 /* EqualsToken */) {
- error(restExpression.operatorToken, ts.Diagnostics.A_rest_element_cannot_have_an_initializer);
+ }
+ return undefined;
+ function maybeAddMissingAwaitInfo(errorNode, source, target) {
+ if (errorNode && reportErrors && errorOutputContainer.errors && errorOutputContainer.errors.length) {
+ // Bail if target is Promise-like---something else is wrong
+ if (getAwaitedTypeOfPromise(target)) {
+ return;
}
- else {
- checkGrammarForDisallowedTrailingComma(node.elements, ts.Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma);
- var type = everyType(sourceType, isTupleType) ?
- mapType(sourceType, function (t) { return sliceTupleType(t, elementIndex); }) :
- createArrayType(elementType);
- return checkDestructuringAssignment(restExpression, type, checkMode);
+ var awaitedTypeOfSource = getAwaitedTypeOfPromise(source);
+ if (awaitedTypeOfSource && isTypeRelatedTo(awaitedTypeOfSource, target, relation)) {
+ ts.addRelatedInfo(errorOutputContainer.errors[0], ts.createDiagnosticForNode(errorNode, ts.Diagnostics.Did_you_forget_to_use_await));
}
}
}
- return undefined;
}
- function checkDestructuringAssignment(exprOrAssignment, sourceType, checkMode, rightIsThis) {
- var target;
- if (exprOrAssignment.kind === 282 /* ShorthandPropertyAssignment */) {
- var prop = exprOrAssignment;
- if (prop.objectAssignmentInitializer) {
- // In strict null checking mode, if a default value of a non-undefined type is specified, remove
- // undefined from the final type.
- if (strictNullChecks &&
- !(getFalsyFlags(checkExpression(prop.objectAssignmentInitializer)) & 32768 /* Undefined */)) {
- sourceType = getTypeWithFacts(sourceType, 524288 /* NEUndefined */);
- }
- checkBinaryLikeExpression(prop.name, prop.equalsToken, prop.objectAssignmentInitializer, checkMode);
+ /**
+ * Returns the this argument in calls like x.f(...) and x[f](...). Undefined otherwise.
+ */
+ function getThisArgumentOfCall(node) {
+ if (node.kind === 203 /* CallExpression */) {
+ var callee = ts.skipOuterExpressions(node.expression);
+ if (ts.isAccessExpression(callee)) {
+ return callee.expression;
}
- target = exprOrAssignment.name;
}
- else {
- target = exprOrAssignment;
+ }
+ function createSyntheticExpression(parent, type, isSpread, tupleNameSource) {
+ var result = ts.parseNodeFactory.createSyntheticExpression(type, isSpread, tupleNameSource);
+ ts.setTextRange(result, parent);
+ ts.setParent(result, parent);
+ return result;
+ }
+ /**
+ * Returns the effective arguments for an expression that works like a function invocation.
+ */
+ function getEffectiveCallArguments(node) {
+ if (node.kind === 205 /* TaggedTemplateExpression */) {
+ var template = node.template;
+ var args_3 = [createSyntheticExpression(template, getGlobalTemplateStringsArrayType())];
+ if (template.kind === 218 /* TemplateExpression */) {
+ ts.forEach(template.templateSpans, function (span) {
+ args_3.push(span.expression);
+ });
+ }
+ return args_3;
}
- if (target.kind === 209 /* BinaryExpression */ && target.operatorToken.kind === 62 /* EqualsToken */) {
- checkBinaryExpression(target, checkMode);
- target = target.left;
+ if (node.kind === 161 /* Decorator */) {
+ return getEffectiveDecoratorArguments(node);
}
- if (target.kind === 193 /* ObjectLiteralExpression */) {
- return checkObjectLiteralAssignment(target, sourceType, rightIsThis);
+ if (ts.isJsxOpeningLikeElement(node)) {
+ return node.attributes.properties.length > 0 || (ts.isJsxOpeningElement(node) && node.parent.children.length > 0) ? [node.attributes] : ts.emptyArray;
}
- if (target.kind === 192 /* ArrayLiteralExpression */) {
- return checkArrayLiteralAssignment(target, sourceType, checkMode);
+ var args = node.arguments || ts.emptyArray;
+ var spreadIndex = getSpreadArgumentIndex(args);
+ if (spreadIndex >= 0) {
+ // Create synthetic arguments from spreads of tuple types.
+ var effectiveArgs_1 = args.slice(0, spreadIndex);
+ var _loop_20 = function (i) {
+ var arg = args[i];
+ // We can call checkExpressionCached because spread expressions never have a contextual type.
+ var spreadType = arg.kind === 220 /* SpreadElement */ && (flowLoopCount ? checkExpression(arg.expression) : checkExpressionCached(arg.expression));
+ if (spreadType && isTupleType(spreadType)) {
+ ts.forEach(getTypeArguments(spreadType), function (t, i) {
+ var _a;
+ var flags = spreadType.target.elementFlags[i];
+ var syntheticArg = createSyntheticExpression(arg, flags & 4 /* Rest */ ? createArrayType(t) : t, !!(flags & 12 /* Variable */), (_a = spreadType.target.labeledElementDeclarations) === null || _a === void 0 ? void 0 : _a[i]);
+ effectiveArgs_1.push(syntheticArg);
+ });
+ }
+ else {
+ effectiveArgs_1.push(arg);
+ }
+ };
+ for (var i = spreadIndex; i < args.length; i++) {
+ _loop_20(i);
+ }
+ return effectiveArgs_1;
}
- return checkReferenceAssignment(target, sourceType, checkMode);
+ return args;
}
- function checkReferenceAssignment(target, sourceType, checkMode) {
- var targetType = checkExpression(target, checkMode);
- var error = target.parent.kind === 283 /* SpreadAssignment */ ?
- ts.Diagnostics.The_target_of_an_object_rest_assignment_must_be_a_variable_or_a_property_access :
- ts.Diagnostics.The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access;
- var optionalError = target.parent.kind === 283 /* SpreadAssignment */ ?
- ts.Diagnostics.The_target_of_an_object_rest_assignment_may_not_be_an_optional_property_access :
- ts.Diagnostics.The_left_hand_side_of_an_assignment_expression_may_not_be_an_optional_property_access;
- if (checkReferenceExpression(target, error, optionalError)) {
- checkTypeAssignableToAndOptionallyElaborate(sourceType, targetType, target, target);
- }
- if (ts.isPrivateIdentifierPropertyAccessExpression(target)) {
- checkExternalEmitHelpers(target.parent, 524288 /* ClassPrivateFieldSet */);
+ /**
+ * Returns the synthetic argument list for a decorator invocation.
+ */
+ function getEffectiveDecoratorArguments(node) {
+ var parent = node.parent;
+ var expr = node.expression;
+ switch (parent.kind) {
+ case 252 /* ClassDeclaration */:
+ case 221 /* ClassExpression */:
+ // For a class decorator, the `target` is the type of the class (e.g. the
+ // "static" or "constructor" side of the class).
+ return [
+ createSyntheticExpression(expr, getTypeOfSymbol(getSymbolOfNode(parent)))
+ ];
+ case 160 /* Parameter */:
+ // A parameter declaration decorator will have three arguments (see
+ // `ParameterDecorator` in core.d.ts).
+ var func = parent.parent;
+ return [
+ createSyntheticExpression(expr, parent.parent.kind === 166 /* Constructor */ ? getTypeOfSymbol(getSymbolOfNode(func)) : errorType),
+ createSyntheticExpression(expr, anyType),
+ createSyntheticExpression(expr, numberType)
+ ];
+ case 163 /* PropertyDeclaration */:
+ case 165 /* MethodDeclaration */:
+ case 167 /* GetAccessor */:
+ case 168 /* SetAccessor */:
+ // A method or accessor declaration decorator will have two or three arguments (see
+ // `PropertyDecorator` and `MethodDecorator` in core.d.ts). If we are emitting decorators
+ // for ES3, we will only pass two arguments.
+ var hasPropDesc = parent.kind !== 163 /* PropertyDeclaration */ && languageVersion !== 0 /* ES3 */;
+ return [
+ createSyntheticExpression(expr, getParentTypeOfClassElement(parent)),
+ createSyntheticExpression(expr, getClassElementPropertyKeyType(parent)),
+ createSyntheticExpression(expr, hasPropDesc ? createTypedPropertyDescriptorType(getTypeOfNode(parent)) : anyType)
+ ];
}
- return sourceType;
+ return ts.Debug.fail();
}
/**
- * This is a *shallow* check: An expression is side-effect-free if the
- * evaluation of the expression *itself* cannot produce side effects.
- * For example, x++ / 3 is side-effect free because the / operator
- * does not have side effects.
- * The intent is to "smell test" an expression for correctness in positions where
- * its value is discarded (e.g. the left side of the comma operator).
+ * Returns the argument count for a decorator node that works like a function invocation.
*/
- function isSideEffectFree(node) {
- node = ts.skipParentheses(node);
- switch (node.kind) {
- case 75 /* Identifier */:
- case 10 /* StringLiteral */:
- case 13 /* RegularExpressionLiteral */:
- case 198 /* TaggedTemplateExpression */:
- case 211 /* TemplateExpression */:
- case 14 /* NoSubstitutionTemplateLiteral */:
- case 8 /* NumericLiteral */:
- case 9 /* BigIntLiteral */:
- case 106 /* TrueKeyword */:
- case 91 /* FalseKeyword */:
- case 100 /* NullKeyword */:
- case 146 /* UndefinedKeyword */:
- case 201 /* FunctionExpression */:
- case 214 /* ClassExpression */:
- case 202 /* ArrowFunction */:
- case 192 /* ArrayLiteralExpression */:
- case 193 /* ObjectLiteralExpression */:
- case 204 /* TypeOfExpression */:
- case 218 /* NonNullExpression */:
- case 267 /* JsxSelfClosingElement */:
- case 266 /* JsxElement */:
- return true;
- case 210 /* ConditionalExpression */:
- return isSideEffectFree(node.whenTrue) &&
- isSideEffectFree(node.whenFalse);
- case 209 /* BinaryExpression */:
- if (ts.isAssignmentOperator(node.operatorToken.kind)) {
- return false;
- }
- return isSideEffectFree(node.left) &&
- isSideEffectFree(node.right);
- case 207 /* PrefixUnaryExpression */:
- case 208 /* PostfixUnaryExpression */:
- // Unary operators ~, !, +, and - have no side effects.
- // The rest do.
- switch (node.operator) {
- case 53 /* ExclamationToken */:
- case 39 /* PlusToken */:
- case 40 /* MinusToken */:
- case 54 /* TildeToken */:
- return true;
- }
- return false;
- // Some forms listed here for clarity
- case 205 /* VoidExpression */: // Explicit opt-out
- case 199 /* TypeAssertionExpression */: // Not SEF, but can produce useful type warnings
- case 217 /* AsExpression */: // Not SEF, but can produce useful type warnings
+ function getDecoratorArgumentCount(node, signature) {
+ switch (node.parent.kind) {
+ case 252 /* ClassDeclaration */:
+ case 221 /* ClassExpression */:
+ return 1;
+ case 163 /* PropertyDeclaration */:
+ return 2;
+ case 165 /* MethodDeclaration */:
+ case 167 /* GetAccessor */:
+ case 168 /* SetAccessor */:
+ // For ES3 or decorators with only two parameters we supply only two arguments
+ return languageVersion === 0 /* ES3 */ || signature.parameters.length <= 2 ? 2 : 3;
+ case 160 /* Parameter */:
+ return 3;
default:
- return false;
+ return ts.Debug.fail();
}
}
- function isTypeEqualityComparableTo(source, target) {
- return (target.flags & 98304 /* Nullable */) !== 0 || isTypeComparableTo(source, target);
+ function getDiagnosticSpanForCallNode(node, doNotIncludeArguments) {
+ var start;
+ var length;
+ var sourceFile = ts.getSourceFileOfNode(node);
+ if (ts.isPropertyAccessExpression(node.expression)) {
+ var nameSpan = ts.getErrorSpanForNode(sourceFile, node.expression.name);
+ start = nameSpan.start;
+ length = doNotIncludeArguments ? nameSpan.length : node.end - start;
+ }
+ else {
+ var expressionSpan = ts.getErrorSpanForNode(sourceFile, node.expression);
+ start = expressionSpan.start;
+ length = doNotIncludeArguments ? expressionSpan.length : node.end - start;
+ }
+ return { start: start, length: length, sourceFile: sourceFile };
}
- var CheckBinaryExpressionState;
- (function (CheckBinaryExpressionState) {
- CheckBinaryExpressionState[CheckBinaryExpressionState["MaybeCheckLeft"] = 0] = "MaybeCheckLeft";
- CheckBinaryExpressionState[CheckBinaryExpressionState["CheckRight"] = 1] = "CheckRight";
- CheckBinaryExpressionState[CheckBinaryExpressionState["FinishCheck"] = 2] = "FinishCheck";
- })(CheckBinaryExpressionState || (CheckBinaryExpressionState = {}));
- function checkBinaryExpression(node, checkMode) {
- var workStacks = {
- expr: [node],
- state: [0 /* MaybeCheckLeft */],
- leftType: [undefined]
- };
- var stackIndex = 0;
- var lastResult;
- while (stackIndex >= 0) {
- node = workStacks.expr[stackIndex];
- switch (workStacks.state[stackIndex]) {
- case 0 /* MaybeCheckLeft */: {
- if (ts.isInJSFile(node) && ts.getAssignedExpandoInitializer(node)) {
- finishInvocation(checkExpression(node.right, checkMode));
- break;
- }
- checkGrammarNullishCoalesceWithLogicalExpression(node);
- var operator = node.operatorToken.kind;
- if (operator === 62 /* EqualsToken */ && (node.left.kind === 193 /* ObjectLiteralExpression */ || node.left.kind === 192 /* ArrayLiteralExpression */)) {
- finishInvocation(checkDestructuringAssignment(node.left, checkExpression(node.right, checkMode), checkMode, node.right.kind === 104 /* ThisKeyword */));
- break;
- }
- advanceState(1 /* CheckRight */);
- maybeCheckExpression(node.left);
- break;
- }
- case 1 /* CheckRight */: {
- var leftType = lastResult;
- workStacks.leftType[stackIndex] = leftType;
- var operator = node.operatorToken.kind;
- if (operator === 55 /* AmpersandAmpersandToken */ || operator === 56 /* BarBarToken */ || operator === 60 /* QuestionQuestionToken */) {
- checkTruthinessOfType(leftType, node.left);
- }
- advanceState(2 /* FinishCheck */);
- maybeCheckExpression(node.right);
- break;
- }
- case 2 /* FinishCheck */: {
- var leftType = workStacks.leftType[stackIndex];
- var rightType = lastResult;
- finishInvocation(checkBinaryLikeExpressionWorker(node.left, node.operatorToken, node.right, leftType, rightType, node));
- break;
- }
- default: return ts.Debug.fail("Invalid state " + workStacks.state[stackIndex] + " for checkBinaryExpression");
+ function getDiagnosticForCallNode(node, message, arg0, arg1, arg2, arg3) {
+ if (ts.isCallExpression(node)) {
+ var _a = getDiagnosticSpanForCallNode(node), sourceFile = _a.sourceFile, start = _a.start, length_5 = _a.length;
+ return ts.createFileDiagnostic(sourceFile, start, length_5, message, arg0, arg1, arg2, arg3);
+ }
+ else {
+ return ts.createDiagnosticForNode(node, message, arg0, arg1, arg2, arg3);
+ }
+ }
+ function isPromiseResolveArityError(node) {
+ if (!ts.isCallExpression(node) || !ts.isIdentifier(node.expression))
+ return false;
+ var symbol = resolveName(node.expression, node.expression.escapedText, 111551 /* Value */, undefined, undefined, false);
+ var decl = symbol === null || symbol === void 0 ? void 0 : symbol.valueDeclaration;
+ if (!decl || !ts.isParameter(decl) || !isFunctionExpressionOrArrowFunction(decl.parent) || !ts.isNewExpression(decl.parent.parent) || !ts.isIdentifier(decl.parent.parent.expression)) {
+ return false;
+ }
+ var globalPromiseSymbol = getGlobalPromiseConstructorSymbol(/*reportErrors*/ false);
+ if (!globalPromiseSymbol)
+ return false;
+ var constructorSymbol = getSymbolAtLocation(decl.parent.parent.expression, /*ignoreErrors*/ true);
+ return constructorSymbol === globalPromiseSymbol;
+ }
+ function getArgumentArityError(node, signatures, args) {
+ var min = Number.POSITIVE_INFINITY;
+ var max = Number.NEGATIVE_INFINITY;
+ var belowArgCount = Number.NEGATIVE_INFINITY;
+ var aboveArgCount = Number.POSITIVE_INFINITY;
+ var argCount = args.length;
+ var closestSignature;
+ for (var _i = 0, signatures_8 = signatures; _i < signatures_8.length; _i++) {
+ var sig = signatures_8[_i];
+ var minCount = getMinArgumentCount(sig);
+ var maxCount = getParameterCount(sig);
+ if (minCount < argCount && minCount > belowArgCount)
+ belowArgCount = minCount;
+ if (argCount < maxCount && maxCount < aboveArgCount)
+ aboveArgCount = maxCount;
+ if (minCount < min) {
+ min = minCount;
+ closestSignature = sig;
}
+ max = Math.max(max, maxCount);
}
- return lastResult;
- function finishInvocation(result) {
- lastResult = result;
- stackIndex--;
+ var hasRestParameter = ts.some(signatures, hasEffectiveRestParameter);
+ var paramRange = hasRestParameter ? min :
+ min < max ? min + "-" + max :
+ min;
+ var hasSpreadArgument = getSpreadArgumentIndex(args) > -1;
+ if (argCount <= max && hasSpreadArgument) {
+ argCount--;
}
- /**
- * Note that `advanceState` sets the _current_ head state, and that `maybeCheckExpression` potentially pushes on a new
- * head state; so `advanceState` must be called before any `maybeCheckExpression` during a state's execution.
- */
- function advanceState(nextState) {
- workStacks.state[stackIndex] = nextState;
+ var spanArray;
+ var related;
+ var error = hasRestParameter || hasSpreadArgument ?
+ hasRestParameter && hasSpreadArgument ?
+ ts.Diagnostics.Expected_at_least_0_arguments_but_got_1_or_more :
+ hasRestParameter ?
+ ts.Diagnostics.Expected_at_least_0_arguments_but_got_1 :
+ ts.Diagnostics.Expected_0_arguments_but_got_1_or_more :
+ paramRange === 1 && argCount === 0 && isPromiseResolveArityError(node) ?
+ ts.Diagnostics.Expected_0_arguments_but_got_1_Did_you_forget_to_include_void_in_your_type_argument_to_Promise :
+ ts.Diagnostics.Expected_0_arguments_but_got_1;
+ if (closestSignature && getMinArgumentCount(closestSignature) > argCount && closestSignature.declaration) {
+ var paramDecl = closestSignature.declaration.parameters[closestSignature.thisParameter ? argCount + 1 : argCount];
+ if (paramDecl) {
+ related = ts.createDiagnosticForNode(paramDecl, ts.isBindingPattern(paramDecl.name) ? ts.Diagnostics.An_argument_matching_this_binding_pattern_was_not_provided :
+ ts.isRestParameter(paramDecl) ? ts.Diagnostics.Arguments_for_the_rest_parameter_0_were_not_provided : ts.Diagnostics.An_argument_for_0_was_not_provided, !paramDecl.name ? argCount : !ts.isBindingPattern(paramDecl.name) ? ts.idText(ts.getFirstIdentifier(paramDecl.name)) : undefined);
+ }
+ }
+ if (min < argCount && argCount < max) {
+ return getDiagnosticForCallNode(node, ts.Diagnostics.No_overload_expects_0_arguments_but_overloads_do_exist_that_expect_either_1_or_2_arguments, argCount, belowArgCount, aboveArgCount);
+ }
+ if (!hasSpreadArgument && argCount < min) {
+ var diagnostic_1 = getDiagnosticForCallNode(node, error, paramRange, argCount);
+ return related ? ts.addRelatedInfo(diagnostic_1, related) : diagnostic_1;
+ }
+ if (hasRestParameter || hasSpreadArgument) {
+ spanArray = ts.factory.createNodeArray(args);
+ if (hasSpreadArgument && argCount) {
+ var nextArg = ts.elementAt(args, getSpreadArgumentIndex(args) + 1) || undefined;
+ spanArray = ts.factory.createNodeArray(args.slice(max > argCount && nextArg ? args.indexOf(nextArg) : Math.min(max, args.length - 1)));
+ }
+ }
+ else {
+ spanArray = ts.factory.createNodeArray(args.slice(max));
+ }
+ var pos = ts.first(spanArray).pos;
+ var end = ts.last(spanArray).end;
+ if (end === pos) {
+ end++;
}
- function maybeCheckExpression(node) {
- if (ts.isBinaryExpression(node)) {
- stackIndex++;
- workStacks.expr[stackIndex] = node;
- workStacks.state[stackIndex] = 0 /* MaybeCheckLeft */;
- workStacks.leftType[stackIndex] = undefined;
+ ts.setTextRangePosEnd(spanArray, pos, end);
+ var diagnostic = ts.createDiagnosticForNodeArray(ts.getSourceFileOfNode(node), spanArray, error, paramRange, argCount);
+ return related ? ts.addRelatedInfo(diagnostic, related) : diagnostic;
+ }
+ function getTypeArgumentArityError(node, signatures, typeArguments) {
+ var argCount = typeArguments.length;
+ // No overloads exist
+ if (signatures.length === 1) {
+ var sig = signatures[0];
+ var min_1 = getMinTypeArgumentCount(sig.typeParameters);
+ var max = ts.length(sig.typeParameters);
+ return ts.createDiagnosticForNodeArray(ts.getSourceFileOfNode(node), typeArguments, ts.Diagnostics.Expected_0_type_arguments_but_got_1, min_1 < max ? min_1 + "-" + max : min_1, argCount);
+ }
+ // Overloads exist
+ var belowArgCount = -Infinity;
+ var aboveArgCount = Infinity;
+ for (var _i = 0, signatures_9 = signatures; _i < signatures_9.length; _i++) {
+ var sig = signatures_9[_i];
+ var min_2 = getMinTypeArgumentCount(sig.typeParameters);
+ var max = ts.length(sig.typeParameters);
+ if (min_2 > argCount) {
+ aboveArgCount = Math.min(aboveArgCount, min_2);
}
- else {
- lastResult = checkExpression(node, checkMode);
+ else if (max < argCount) {
+ belowArgCount = Math.max(belowArgCount, max);
}
}
+ if (belowArgCount !== -Infinity && aboveArgCount !== Infinity) {
+ return ts.createDiagnosticForNodeArray(ts.getSourceFileOfNode(node), typeArguments, ts.Diagnostics.No_overload_expects_0_type_arguments_but_overloads_do_exist_that_expect_either_1_or_2_type_arguments, argCount, belowArgCount, aboveArgCount);
+ }
+ return ts.createDiagnosticForNodeArray(ts.getSourceFileOfNode(node), typeArguments, ts.Diagnostics.Expected_0_type_arguments_but_got_1, belowArgCount === -Infinity ? aboveArgCount : belowArgCount, argCount);
}
- function checkGrammarNullishCoalesceWithLogicalExpression(node) {
- var left = node.left, operatorToken = node.operatorToken, right = node.right;
- if (operatorToken.kind === 60 /* QuestionQuestionToken */) {
- if (ts.isBinaryExpression(left) && (left.operatorToken.kind === 56 /* BarBarToken */ || left.operatorToken.kind === 55 /* AmpersandAmpersandToken */)) {
- grammarErrorOnNode(left, ts.Diagnostics._0_and_1_operations_cannot_be_mixed_without_parentheses, ts.tokenToString(left.operatorToken.kind), ts.tokenToString(operatorToken.kind));
+ function resolveCall(node, signatures, candidatesOutArray, checkMode, callChainFlags, fallbackError) {
+ var isTaggedTemplate = node.kind === 205 /* TaggedTemplateExpression */;
+ var isDecorator = node.kind === 161 /* Decorator */;
+ var isJsxOpeningOrSelfClosingElement = ts.isJsxOpeningLikeElement(node);
+ var reportErrors = !candidatesOutArray && produceDiagnostics;
+ var typeArguments;
+ if (!isDecorator) {
+ typeArguments = node.typeArguments;
+ // We already perform checking on the type arguments on the class declaration itself.
+ if (isTaggedTemplate || isJsxOpeningOrSelfClosingElement || node.expression.kind !== 105 /* SuperKeyword */) {
+ ts.forEach(typeArguments, checkSourceElement);
}
- if (ts.isBinaryExpression(right) && (right.operatorToken.kind === 56 /* BarBarToken */ || right.operatorToken.kind === 55 /* AmpersandAmpersandToken */)) {
- grammarErrorOnNode(right, ts.Diagnostics._0_and_1_operations_cannot_be_mixed_without_parentheses, ts.tokenToString(right.operatorToken.kind), ts.tokenToString(operatorToken.kind));
+ }
+ var candidates = candidatesOutArray || [];
+ // reorderCandidates fills up the candidates array directly
+ reorderCandidates(signatures, candidates, callChainFlags);
+ if (!candidates.length) {
+ if (reportErrors) {
+ diagnostics.add(getDiagnosticForCallNode(node, ts.Diagnostics.Call_target_does_not_contain_any_signatures));
}
+ return resolveErrorCall(node);
}
- }
- // Note that this and `checkBinaryExpression` above should behave mostly the same, except this elides some
- // expression-wide checks and does not use a work stack to fold nested binary expressions into the same callstack frame
- function checkBinaryLikeExpression(left, operatorToken, right, checkMode, errorNode) {
- var operator = operatorToken.kind;
- if (operator === 62 /* EqualsToken */ && (left.kind === 193 /* ObjectLiteralExpression */ || left.kind === 192 /* ArrayLiteralExpression */)) {
- return checkDestructuringAssignment(left, checkExpression(right, checkMode), checkMode, right.kind === 104 /* ThisKeyword */);
+ var args = getEffectiveCallArguments(node);
+ // The excludeArgument array contains true for each context sensitive argument (an argument
+ // is context sensitive it is susceptible to a one-time permanent contextual typing).
+ //
+ // The idea is that we will perform type argument inference & assignability checking once
+ // without using the susceptible parameters that are functions, and once more for those
+ // parameters, contextually typing each as we go along.
+ //
+ // For a tagged template, then the first argument be 'undefined' if necessary because it
+ // represents a TemplateStringsArray.
+ //
+ // For a decorator, no arguments are susceptible to contextual typing due to the fact
+ // decorators are applied to a declaration by the emitter, and not to an expression.
+ var isSingleNonGenericCandidate = candidates.length === 1 && !candidates[0].typeParameters;
+ var argCheckMode = !isDecorator && !isSingleNonGenericCandidate && ts.some(args, isContextSensitive) ? 4 /* SkipContextSensitive */ : 0 /* Normal */;
+ // The following variables are captured and modified by calls to chooseOverload.
+ // If overload resolution or type argument inference fails, we want to report the
+ // best error possible. The best error is one which says that an argument was not
+ // assignable to a parameter. This implies that everything else about the overload
+ // was fine. So if there is any overload that is only incorrect because of an
+ // argument, we will report an error on that one.
+ //
+ // function foo(s: string): void;
+ // function foo(n: number): void; // Report argument error on this overload
+ // function foo(): void;
+ // foo(true);
+ //
+ // If none of the overloads even made it that far, there are two possibilities.
+ // There was a problem with type arguments for some overload, in which case
+ // report an error on that. Or none of the overloads even had correct arity,
+ // in which case give an arity error.
+ //
+ // function foo(x: T): void; // Report type argument error
+ // function foo(): void;
+ // foo(0);
+ //
+ var candidatesForArgumentError;
+ var candidateForArgumentArityError;
+ var candidateForTypeArgumentError;
+ var result;
+ // If we are in signature help, a trailing comma indicates that we intend to provide another argument,
+ // so we will only accept overloads with arity at least 1 higher than the current number of provided arguments.
+ var signatureHelpTrailingComma = !!(checkMode & 16 /* IsForSignatureHelp */) && node.kind === 203 /* CallExpression */ && node.arguments.hasTrailingComma;
+ // Section 4.12.1:
+ // if the candidate list contains one or more signatures for which the type of each argument
+ // expression is a subtype of each corresponding parameter type, the return type of the first
+ // of those signatures becomes the return type of the function call.
+ // Otherwise, the return type of the first signature in the candidate list becomes the return
+ // type of the function call.
+ //
+ // Whether the call is an error is determined by assignability of the arguments. The subtype pass
+ // is just important for choosing the best signature. So in the case where there is only one
+ // signature, the subtype pass is useless. So skipping it is an optimization.
+ if (candidates.length > 1) {
+ result = chooseOverload(candidates, subtypeRelation, isSingleNonGenericCandidate, signatureHelpTrailingComma);
}
- var leftType;
- if (operator === 55 /* AmpersandAmpersandToken */ || operator === 56 /* BarBarToken */ || operator === 60 /* QuestionQuestionToken */) {
- leftType = checkTruthinessExpression(left, checkMode);
+ if (!result) {
+ result = chooseOverload(candidates, assignableRelation, isSingleNonGenericCandidate, signatureHelpTrailingComma);
}
- else {
- leftType = checkExpression(left, checkMode);
+ if (result) {
+ return result;
}
- var rightType = checkExpression(right, checkMode);
- return checkBinaryLikeExpressionWorker(left, operatorToken, right, leftType, rightType, errorNode);
- }
- function checkBinaryLikeExpressionWorker(left, operatorToken, right, leftType, rightType, errorNode) {
- var operator = operatorToken.kind;
- switch (operator) {
- case 41 /* AsteriskToken */:
- case 42 /* AsteriskAsteriskToken */:
- case 65 /* AsteriskEqualsToken */:
- case 66 /* AsteriskAsteriskEqualsToken */:
- case 43 /* SlashToken */:
- case 67 /* SlashEqualsToken */:
- case 44 /* PercentToken */:
- case 68 /* PercentEqualsToken */:
- case 40 /* MinusToken */:
- case 64 /* MinusEqualsToken */:
- case 47 /* LessThanLessThanToken */:
- case 69 /* LessThanLessThanEqualsToken */:
- case 48 /* GreaterThanGreaterThanToken */:
- case 70 /* GreaterThanGreaterThanEqualsToken */:
- case 49 /* GreaterThanGreaterThanGreaterThanToken */:
- case 71 /* GreaterThanGreaterThanGreaterThanEqualsToken */:
- case 51 /* BarToken */:
- case 73 /* BarEqualsToken */:
- case 52 /* CaretToken */:
- case 74 /* CaretEqualsToken */:
- case 50 /* AmpersandToken */:
- case 72 /* AmpersandEqualsToken */:
- if (leftType === silentNeverType || rightType === silentNeverType) {
- return silentNeverType;
- }
- leftType = checkNonNullType(leftType, left);
- rightType = checkNonNullType(rightType, right);
- var suggestedOperator = void 0;
- // if a user tries to apply a bitwise operator to 2 boolean operands
- // try and return them a helpful suggestion
- if ((leftType.flags & 528 /* BooleanLike */) &&
- (rightType.flags & 528 /* BooleanLike */) &&
- (suggestedOperator = getSuggestedBooleanOperator(operatorToken.kind)) !== undefined) {
- error(errorNode || operatorToken, ts.Diagnostics.The_0_operator_is_not_allowed_for_boolean_types_Consider_using_1_instead, ts.tokenToString(operatorToken.kind), ts.tokenToString(suggestedOperator));
- return numberType;
- }
- else {
- // otherwise just check each operand separately and report errors as normal
- var leftOk = checkArithmeticOperandType(left, leftType, ts.Diagnostics.The_left_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type, /*isAwaitValid*/ true);
- var rightOk = checkArithmeticOperandType(right, rightType, ts.Diagnostics.The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type, /*isAwaitValid*/ true);
- var resultType_1;
- // If both are any or unknown, allow operation; assume it will resolve to number
- if ((isTypeAssignableToKind(leftType, 3 /* AnyOrUnknown */) && isTypeAssignableToKind(rightType, 3 /* AnyOrUnknown */)) ||
- // Or, if neither could be bigint, implicit coercion results in a number result
- !(maybeTypeOfKind(leftType, 2112 /* BigIntLike */) || maybeTypeOfKind(rightType, 2112 /* BigIntLike */))) {
- resultType_1 = numberType;
+ // No signatures were applicable. Now report errors based on the last applicable signature with
+ // no arguments excluded from assignability checks.
+ // If candidate is undefined, it means that no candidates had a suitable arity. In that case,
+ // skip the checkApplicableSignature check.
+ if (reportErrors) {
+ if (candidatesForArgumentError) {
+ if (candidatesForArgumentError.length === 1 || candidatesForArgumentError.length > 3) {
+ var last_2 = candidatesForArgumentError[candidatesForArgumentError.length - 1];
+ var chain_1;
+ if (candidatesForArgumentError.length > 3) {
+ chain_1 = ts.chainDiagnosticMessages(chain_1, ts.Diagnostics.The_last_overload_gave_the_following_error);
+ chain_1 = ts.chainDiagnosticMessages(chain_1, ts.Diagnostics.No_overload_matches_this_call);
}
- // At least one is assignable to bigint, so check that both are
- else if (bothAreBigIntLike(leftType, rightType)) {
- switch (operator) {
- case 49 /* GreaterThanGreaterThanGreaterThanToken */:
- case 71 /* GreaterThanGreaterThanGreaterThanEqualsToken */:
- reportOperatorError();
+ var diags = getSignatureApplicabilityError(node, args, last_2, assignableRelation, 0 /* Normal */, /*reportErrors*/ true, function () { return chain_1; });
+ if (diags) {
+ for (var _i = 0, diags_1 = diags; _i < diags_1.length; _i++) {
+ var d = diags_1[_i];
+ if (last_2.declaration && candidatesForArgumentError.length > 3) {
+ ts.addRelatedInfo(d, ts.createDiagnosticForNode(last_2.declaration, ts.Diagnostics.The_last_overload_is_declared_here));
+ }
+ addImplementationSuccessElaboration(last_2, d);
+ diagnostics.add(d);
}
- resultType_1 = bigintType;
}
- // Exactly one of leftType/rightType is assignable to bigint
else {
- reportOperatorError(bothAreBigIntLike);
- resultType_1 = errorType;
- }
- if (leftOk && rightOk) {
- checkAssignmentOperator(resultType_1);
+ ts.Debug.fail("No error for last overload signature");
}
- return resultType_1;
}
- case 39 /* PlusToken */:
- case 63 /* PlusEqualsToken */:
- if (leftType === silentNeverType || rightType === silentNeverType) {
- return silentNeverType;
+ else {
+ var allDiagnostics = [];
+ var max = 0;
+ var min_3 = Number.MAX_VALUE;
+ var minIndex = 0;
+ var i_1 = 0;
+ var _loop_21 = function (c) {
+ var chain_2 = function () { return ts.chainDiagnosticMessages(/*details*/ undefined, ts.Diagnostics.Overload_0_of_1_2_gave_the_following_error, i_1 + 1, candidates.length, signatureToString(c)); };
+ var diags_2 = getSignatureApplicabilityError(node, args, c, assignableRelation, 0 /* Normal */, /*reportErrors*/ true, chain_2);
+ if (diags_2) {
+ if (diags_2.length <= min_3) {
+ min_3 = diags_2.length;
+ minIndex = i_1;
+ }
+ max = Math.max(max, diags_2.length);
+ allDiagnostics.push(diags_2);
+ }
+ else {
+ ts.Debug.fail("No error for 3 or fewer overload signatures");
+ }
+ i_1++;
+ };
+ for (var _a = 0, candidatesForArgumentError_1 = candidatesForArgumentError; _a < candidatesForArgumentError_1.length; _a++) {
+ var c = candidatesForArgumentError_1[_a];
+ _loop_21(c);
+ }
+ var diags_3 = max > 1 ? allDiagnostics[minIndex] : ts.flatten(allDiagnostics);
+ ts.Debug.assert(diags_3.length > 0, "No errors reported for 3 or fewer overload signatures");
+ var chain = ts.chainDiagnosticMessages(ts.map(diags_3, function (d) { return typeof d.messageText === "string" ? d : d.messageText; }), ts.Diagnostics.No_overload_matches_this_call);
+ // The below is a spread to guarantee we get a new (mutable) array - our `flatMap` helper tries to do "smart" optimizations where it reuses input
+ // arrays and the emptyArray singleton where possible, which is decidedly not what we want while we're still constructing this diagnostic
+ var related = __spreadArrays(ts.flatMap(diags_3, function (d) { return d.relatedInformation; }));
+ var diag = void 0;
+ if (ts.every(diags_3, function (d) { return d.start === diags_3[0].start && d.length === diags_3[0].length && d.file === diags_3[0].file; })) {
+ var _b = diags_3[0], file = _b.file, start = _b.start, length_6 = _b.length;
+ diag = { file: file, start: start, length: length_6, code: chain.code, category: chain.category, messageText: chain, relatedInformation: related };
+ }
+ else {
+ diag = ts.createDiagnosticForNodeFromMessageChain(node, chain, related);
+ }
+ addImplementationSuccessElaboration(candidatesForArgumentError[0], diag);
+ diagnostics.add(diag);
}
- if (!isTypeAssignableToKind(leftType, 132 /* StringLike */) && !isTypeAssignableToKind(rightType, 132 /* StringLike */)) {
- leftType = checkNonNullType(leftType, left);
- rightType = checkNonNullType(rightType, right);
+ }
+ else if (candidateForArgumentArityError) {
+ diagnostics.add(getArgumentArityError(node, [candidateForArgumentArityError], args));
+ }
+ else if (candidateForTypeArgumentError) {
+ checkTypeArguments(candidateForTypeArgumentError, node.typeArguments, /*reportErrors*/ true, fallbackError);
+ }
+ else {
+ var signaturesWithCorrectTypeArgumentArity = ts.filter(signatures, function (s) { return hasCorrectTypeArgumentArity(s, typeArguments); });
+ if (signaturesWithCorrectTypeArgumentArity.length === 0) {
+ diagnostics.add(getTypeArgumentArityError(node, signatures, typeArguments));
}
- var resultType = void 0;
- if (isTypeAssignableToKind(leftType, 296 /* NumberLike */, /*strict*/ true) && isTypeAssignableToKind(rightType, 296 /* NumberLike */, /*strict*/ true)) {
- // Operands of an enum type are treated as having the primitive type Number.
- // If both operands are of the Number primitive type, the result is of the Number primitive type.
- resultType = numberType;
+ else if (!isDecorator) {
+ diagnostics.add(getArgumentArityError(node, signaturesWithCorrectTypeArgumentArity, args));
}
- else if (isTypeAssignableToKind(leftType, 2112 /* BigIntLike */, /*strict*/ true) && isTypeAssignableToKind(rightType, 2112 /* BigIntLike */, /*strict*/ true)) {
- // If both operands are of the BigInt primitive type, the result is of the BigInt primitive type.
- resultType = bigintType;
+ else if (fallbackError) {
+ diagnostics.add(getDiagnosticForCallNode(node, fallbackError));
}
- else if (isTypeAssignableToKind(leftType, 132 /* StringLike */, /*strict*/ true) || isTypeAssignableToKind(rightType, 132 /* StringLike */, /*strict*/ true)) {
- // If one or both operands are of the String primitive type, the result is of the String primitive type.
- resultType = stringType;
+ }
+ }
+ return getCandidateForOverloadFailure(node, candidates, args, !!candidatesOutArray);
+ function addImplementationSuccessElaboration(failed, diagnostic) {
+ var _a, _b;
+ var oldCandidatesForArgumentError = candidatesForArgumentError;
+ var oldCandidateForArgumentArityError = candidateForArgumentArityError;
+ var oldCandidateForTypeArgumentError = candidateForTypeArgumentError;
+ var failedSignatureDeclarations = ((_b = (_a = failed.declaration) === null || _a === void 0 ? void 0 : _a.symbol) === null || _b === void 0 ? void 0 : _b.declarations) || ts.emptyArray;
+ var isOverload = failedSignatureDeclarations.length > 1;
+ var implDecl = isOverload ? ts.find(failedSignatureDeclarations, function (d) { return ts.isFunctionLikeDeclaration(d) && ts.nodeIsPresent(d.body); }) : undefined;
+ if (implDecl) {
+ var candidate = getSignatureFromDeclaration(implDecl);
+ var isSingleNonGenericCandidate_1 = !candidate.typeParameters;
+ if (chooseOverload([candidate], assignableRelation, isSingleNonGenericCandidate_1)) {
+ ts.addRelatedInfo(diagnostic, ts.createDiagnosticForNode(implDecl, ts.Diagnostics.The_call_would_have_succeeded_against_this_implementation_but_implementation_signatures_of_overloads_are_not_externally_visible));
+ }
+ }
+ candidatesForArgumentError = oldCandidatesForArgumentError;
+ candidateForArgumentArityError = oldCandidateForArgumentArityError;
+ candidateForTypeArgumentError = oldCandidateForTypeArgumentError;
+ }
+ function chooseOverload(candidates, relation, isSingleNonGenericCandidate, signatureHelpTrailingComma) {
+ if (signatureHelpTrailingComma === void 0) { signatureHelpTrailingComma = false; }
+ candidatesForArgumentError = undefined;
+ candidateForArgumentArityError = undefined;
+ candidateForTypeArgumentError = undefined;
+ if (isSingleNonGenericCandidate) {
+ var candidate = candidates[0];
+ if (ts.some(typeArguments) || !hasCorrectArity(node, args, candidate, signatureHelpTrailingComma)) {
+ return undefined;
}
- else if (isTypeAny(leftType) || isTypeAny(rightType)) {
- // Otherwise, the result is of type Any.
- // NOTE: unknown type here denotes error type. Old compiler treated this case as any type so do we.
- resultType = leftType === errorType || rightType === errorType ? errorType : anyType;
+ if (getSignatureApplicabilityError(node, args, candidate, relation, 0 /* Normal */, /*reportErrors*/ false, /*containingMessageChain*/ undefined)) {
+ candidatesForArgumentError = [candidate];
+ return undefined;
}
- // Symbols are not allowed at all in arithmetic expressions
- if (resultType && !checkForDisallowedESSymbolOperand(operator)) {
- return resultType;
+ return candidate;
+ }
+ for (var candidateIndex = 0; candidateIndex < candidates.length; candidateIndex++) {
+ var candidate = candidates[candidateIndex];
+ if (!hasCorrectTypeArgumentArity(candidate, typeArguments) || !hasCorrectArity(node, args, candidate, signatureHelpTrailingComma)) {
+ continue;
}
- if (!resultType) {
- // Types that have a reasonably good chance of being a valid operand type.
- // If both types have an awaited type of one of these, we'll assume the user
- // might be missing an await without doing an exhaustive check that inserting
- // await(s) will actually be a completely valid binary expression.
- var closeEnoughKind_1 = 296 /* NumberLike */ | 2112 /* BigIntLike */ | 132 /* StringLike */ | 3 /* AnyOrUnknown */;
- reportOperatorError(function (left, right) {
- return isTypeAssignableToKind(left, closeEnoughKind_1) &&
- isTypeAssignableToKind(right, closeEnoughKind_1);
- });
- return anyType;
+ var checkCandidate = void 0;
+ var inferenceContext = void 0;
+ if (candidate.typeParameters) {
+ var typeArgumentTypes = void 0;
+ if (ts.some(typeArguments)) {
+ typeArgumentTypes = checkTypeArguments(candidate, typeArguments, /*reportErrors*/ false);
+ if (!typeArgumentTypes) {
+ candidateForTypeArgumentError = candidate;
+ continue;
+ }
+ }
+ else {
+ inferenceContext = createInferenceContext(candidate.typeParameters, candidate, /*flags*/ ts.isInJSFile(node) ? 2 /* AnyDefault */ : 0 /* None */);
+ typeArgumentTypes = inferTypeArguments(node, candidate, args, argCheckMode | 8 /* SkipGenericFunctions */, inferenceContext);
+ argCheckMode |= inferenceContext.flags & 4 /* SkippedGenericFunction */ ? 8 /* SkipGenericFunctions */ : 0 /* Normal */;
+ }
+ checkCandidate = getSignatureInstantiation(candidate, typeArgumentTypes, ts.isInJSFile(candidate.declaration), inferenceContext && inferenceContext.inferredTypeParameters);
+ // If the original signature has a generic rest type, instantiation may produce a
+ // signature with different arity and we need to perform another arity check.
+ if (getNonArrayRestType(candidate) && !hasCorrectArity(node, args, checkCandidate, signatureHelpTrailingComma)) {
+ candidateForArgumentArityError = checkCandidate;
+ continue;
+ }
}
- if (operator === 63 /* PlusEqualsToken */) {
- checkAssignmentOperator(resultType);
+ else {
+ checkCandidate = candidate;
}
- return resultType;
- case 29 /* LessThanToken */:
- case 31 /* GreaterThanToken */:
- case 32 /* LessThanEqualsToken */:
- case 33 /* GreaterThanEqualsToken */:
- if (checkForDisallowedESSymbolOperand(operator)) {
- leftType = getBaseTypeOfLiteralType(checkNonNullType(leftType, left));
- rightType = getBaseTypeOfLiteralType(checkNonNullType(rightType, right));
- reportOperatorErrorUnless(function (left, right) {
- return isTypeComparableTo(left, right) || isTypeComparableTo(right, left) || (isTypeAssignableTo(left, numberOrBigIntType) && isTypeAssignableTo(right, numberOrBigIntType));
- });
+ if (getSignatureApplicabilityError(node, args, checkCandidate, relation, argCheckMode, /*reportErrors*/ false, /*containingMessageChain*/ undefined)) {
+ // Give preference to error candidates that have no rest parameters (as they are more specific)
+ (candidatesForArgumentError || (candidatesForArgumentError = [])).push(checkCandidate);
+ continue;
}
- return booleanType;
- case 34 /* EqualsEqualsToken */:
- case 35 /* ExclamationEqualsToken */:
- case 36 /* EqualsEqualsEqualsToken */:
- case 37 /* ExclamationEqualsEqualsToken */:
- reportOperatorErrorUnless(function (left, right) { return isTypeEqualityComparableTo(left, right) || isTypeEqualityComparableTo(right, left); });
- return booleanType;
- case 98 /* InstanceOfKeyword */:
- return checkInstanceOfExpression(left, right, leftType, rightType);
- case 97 /* InKeyword */:
- return checkInExpression(left, right, leftType, rightType);
- case 55 /* AmpersandAmpersandToken */:
- return getTypeFacts(leftType) & 4194304 /* Truthy */ ?
- getUnionType([extractDefinitelyFalsyTypes(strictNullChecks ? leftType : getBaseTypeOfLiteralType(rightType)), rightType]) :
- leftType;
- case 56 /* BarBarToken */:
- return getTypeFacts(leftType) & 8388608 /* Falsy */ ?
- getUnionType([removeDefinitelyFalsyTypes(leftType), rightType], 2 /* Subtype */) :
- leftType;
- case 60 /* QuestionQuestionToken */:
- return getTypeFacts(leftType) & 262144 /* EQUndefinedOrNull */ ?
- getUnionType([getNonNullableType(leftType), rightType], 2 /* Subtype */) :
- leftType;
- case 62 /* EqualsToken */:
- var declKind = ts.isBinaryExpression(left.parent) ? ts.getAssignmentDeclarationKind(left.parent) : 0 /* None */;
- checkAssignmentDeclaration(declKind, rightType);
- if (isAssignmentDeclaration(declKind)) {
- if (!(rightType.flags & 524288 /* Object */) ||
- declKind !== 2 /* ModuleExports */ &&
- declKind !== 6 /* Prototype */ &&
- !isEmptyObjectType(rightType) &&
- !isFunctionObjectType(rightType) &&
- !(ts.getObjectFlags(rightType) & 1 /* Class */)) {
- // don't check assignability of module.exports=, C.prototype=, or expando types because they will necessarily be incomplete
- checkAssignmentOperator(rightType);
+ if (argCheckMode) {
+ // If one or more context sensitive arguments were excluded, we start including
+ // them now (and keeping do so for any subsequent candidates) and perform a second
+ // round of type inference and applicability checking for this particular candidate.
+ argCheckMode = 0 /* Normal */;
+ if (inferenceContext) {
+ var typeArgumentTypes = inferTypeArguments(node, candidate, args, argCheckMode, inferenceContext);
+ checkCandidate = getSignatureInstantiation(candidate, typeArgumentTypes, ts.isInJSFile(candidate.declaration), inferenceContext && inferenceContext.inferredTypeParameters);
+ // If the original signature has a generic rest type, instantiation may produce a
+ // signature with different arity and we need to perform another arity check.
+ if (getNonArrayRestType(candidate) && !hasCorrectArity(node, args, checkCandidate, signatureHelpTrailingComma)) {
+ candidateForArgumentArityError = checkCandidate;
+ continue;
+ }
+ }
+ if (getSignatureApplicabilityError(node, args, checkCandidate, relation, argCheckMode, /*reportErrors*/ false, /*containingMessageChain*/ undefined)) {
+ // Give preference to error candidates that have no rest parameters (as they are more specific)
+ (candidatesForArgumentError || (candidatesForArgumentError = [])).push(checkCandidate);
+ continue;
}
- return leftType;
}
- else {
- checkAssignmentOperator(rightType);
- return getRegularTypeOfObjectLiteral(rightType);
+ candidates[candidateIndex] = checkCandidate;
+ return checkCandidate;
+ }
+ return undefined;
+ }
+ }
+ // No signature was applicable. We have already reported the errors for the invalid signature.
+ function getCandidateForOverloadFailure(node, candidates, args, hasCandidatesOutArray) {
+ ts.Debug.assert(candidates.length > 0); // Else should not have called this.
+ checkNodeDeferred(node);
+ // Normally we will combine overloads. Skip this if they have type parameters since that's hard to combine.
+ // Don't do this if there is a `candidatesOutArray`,
+ // because then we want the chosen best candidate to be one of the overloads, not a combination.
+ return hasCandidatesOutArray || candidates.length === 1 || candidates.some(function (c) { return !!c.typeParameters; })
+ ? pickLongestCandidateSignature(node, candidates, args)
+ : createUnionOfSignaturesForOverloadFailure(candidates);
+ }
+ function createUnionOfSignaturesForOverloadFailure(candidates) {
+ var thisParameters = ts.mapDefined(candidates, function (c) { return c.thisParameter; });
+ var thisParameter;
+ if (thisParameters.length) {
+ thisParameter = createCombinedSymbolFromTypes(thisParameters, thisParameters.map(getTypeOfParameter));
+ }
+ var _a = ts.minAndMax(candidates, getNumNonRestParameters), minArgumentCount = _a.min, maxNonRestParam = _a.max;
+ var parameters = [];
+ var _loop_22 = function (i) {
+ var symbols = ts.mapDefined(candidates, function (s) { return signatureHasRestParameter(s) ?
+ i < s.parameters.length - 1 ? s.parameters[i] : ts.last(s.parameters) :
+ i < s.parameters.length ? s.parameters[i] : undefined; });
+ ts.Debug.assert(symbols.length !== 0);
+ parameters.push(createCombinedSymbolFromTypes(symbols, ts.mapDefined(candidates, function (candidate) { return tryGetTypeAtPosition(candidate, i); })));
+ };
+ for (var i = 0; i < maxNonRestParam; i++) {
+ _loop_22(i);
+ }
+ var restParameterSymbols = ts.mapDefined(candidates, function (c) { return signatureHasRestParameter(c) ? ts.last(c.parameters) : undefined; });
+ var flags = 0 /* None */;
+ if (restParameterSymbols.length !== 0) {
+ var type = createArrayType(getUnionType(ts.mapDefined(candidates, tryGetRestTypeOfSignature), 2 /* Subtype */));
+ parameters.push(createCombinedSymbolForOverloadFailure(restParameterSymbols, type));
+ flags |= 1 /* HasRestParameter */;
+ }
+ if (candidates.some(signatureHasLiteralTypes)) {
+ flags |= 2 /* HasLiteralTypes */;
+ }
+ return createSignature(candidates[0].declaration,
+ /*typeParameters*/ undefined, // Before calling this we tested for `!candidates.some(c => !!c.typeParameters)`.
+ thisParameter, parameters,
+ /*resolvedReturnType*/ getIntersectionType(candidates.map(getReturnTypeOfSignature)),
+ /*typePredicate*/ undefined, minArgumentCount, flags);
+ }
+ function getNumNonRestParameters(signature) {
+ var numParams = signature.parameters.length;
+ return signatureHasRestParameter(signature) ? numParams - 1 : numParams;
+ }
+ function createCombinedSymbolFromTypes(sources, types) {
+ return createCombinedSymbolForOverloadFailure(sources, getUnionType(types, 2 /* Subtype */));
+ }
+ function createCombinedSymbolForOverloadFailure(sources, type) {
+ // This function is currently only used for erroneous overloads, so it's good enough to just use the first source.
+ return createSymbolWithType(ts.first(sources), type);
+ }
+ function pickLongestCandidateSignature(node, candidates, args) {
+ // Pick the longest signature. This way we can get a contextual type for cases like:
+ // declare function f(a: { xa: number; xb: number; }, b: number);
+ // f({ |
+ // Also, use explicitly-supplied type arguments if they are provided, so we can get a contextual signature in cases like:
+ // declare function f(k: keyof T);
+ // f("
+ var bestIndex = getLongestCandidateIndex(candidates, apparentArgumentCount === undefined ? args.length : apparentArgumentCount);
+ var candidate = candidates[bestIndex];
+ var typeParameters = candidate.typeParameters;
+ if (!typeParameters) {
+ return candidate;
+ }
+ var typeArgumentNodes = callLikeExpressionMayHaveTypeArguments(node) ? node.typeArguments : undefined;
+ var instantiated = typeArgumentNodes
+ ? createSignatureInstantiation(candidate, getTypeArgumentsFromNodes(typeArgumentNodes, typeParameters, ts.isInJSFile(node)))
+ : inferSignatureInstantiationForOverloadFailure(node, typeParameters, candidate, args);
+ candidates[bestIndex] = instantiated;
+ return instantiated;
+ }
+ function getTypeArgumentsFromNodes(typeArgumentNodes, typeParameters, isJs) {
+ var typeArguments = typeArgumentNodes.map(getTypeOfNode);
+ while (typeArguments.length > typeParameters.length) {
+ typeArguments.pop();
+ }
+ while (typeArguments.length < typeParameters.length) {
+ typeArguments.push(getConstraintOfTypeParameter(typeParameters[typeArguments.length]) || getDefaultTypeArgumentType(isJs));
+ }
+ return typeArguments;
+ }
+ function inferSignatureInstantiationForOverloadFailure(node, typeParameters, candidate, args) {
+ var inferenceContext = createInferenceContext(typeParameters, candidate, /*flags*/ ts.isInJSFile(node) ? 2 /* AnyDefault */ : 0 /* None */);
+ var typeArgumentTypes = inferTypeArguments(node, candidate, args, 4 /* SkipContextSensitive */ | 8 /* SkipGenericFunctions */, inferenceContext);
+ return createSignatureInstantiation(candidate, typeArgumentTypes);
+ }
+ function getLongestCandidateIndex(candidates, argsCount) {
+ var maxParamsIndex = -1;
+ var maxParams = -1;
+ for (var i = 0; i < candidates.length; i++) {
+ var candidate = candidates[i];
+ var paramCount = getParameterCount(candidate);
+ if (hasEffectiveRestParameter(candidate) || paramCount >= argsCount) {
+ return i;
+ }
+ if (paramCount > maxParams) {
+ maxParams = paramCount;
+ maxParamsIndex = i;
+ }
+ }
+ return maxParamsIndex;
+ }
+ function resolveCallExpression(node, candidatesOutArray, checkMode) {
+ if (node.expression.kind === 105 /* SuperKeyword */) {
+ var superType = checkSuperExpression(node.expression);
+ if (isTypeAny(superType)) {
+ for (var _i = 0, _a = node.arguments; _i < _a.length; _i++) {
+ var arg = _a[_i];
+ checkExpression(arg); // Still visit arguments so they get marked for visibility, etc
}
- case 27 /* CommaToken */:
- if (!compilerOptions.allowUnreachableCode && isSideEffectFree(left) && !isEvalNode(right)) {
- error(left, ts.Diagnostics.Left_side_of_comma_operator_is_unused_and_has_no_side_effects);
+ return anySignature;
+ }
+ if (superType !== errorType) {
+ // In super call, the candidate signatures are the matching arity signatures of the base constructor function instantiated
+ // with the type arguments specified in the extends clause.
+ var baseTypeNode = ts.getEffectiveBaseTypeNode(ts.getContainingClass(node));
+ if (baseTypeNode) {
+ var baseConstructors = getInstantiatedConstructorsForTypeArguments(superType, baseTypeNode.typeArguments, baseTypeNode);
+ return resolveCall(node, baseConstructors, candidatesOutArray, checkMode, 0 /* None */);
}
- return rightType;
- default:
- return ts.Debug.fail();
+ }
+ return resolveUntypedCall(node);
}
- function bothAreBigIntLike(left, right) {
- return isTypeAssignableToKind(left, 2112 /* BigIntLike */) && isTypeAssignableToKind(right, 2112 /* BigIntLike */);
+ var callChainFlags;
+ var funcType = checkExpression(node.expression);
+ if (ts.isCallChain(node)) {
+ var nonOptionalType = getOptionalExpressionType(funcType, node.expression);
+ callChainFlags = nonOptionalType === funcType ? 0 /* None */ :
+ ts.isOutermostOptionalChain(node) ? 8 /* IsOuterCallChain */ :
+ 4 /* IsInnerCallChain */;
+ funcType = nonOptionalType;
}
- function checkAssignmentDeclaration(kind, rightType) {
- if (kind === 2 /* ModuleExports */) {
- for (var _i = 0, _a = getPropertiesOfObjectType(rightType); _i < _a.length; _i++) {
- var prop = _a[_i];
- var propType = getTypeOfSymbol(prop);
- if (propType.symbol && propType.symbol.flags & 32 /* Class */) {
- var name = prop.escapedName;
- var symbol = resolveName(prop.valueDeclaration, name, 788968 /* Type */, undefined, name, /*isUse*/ false);
- if (symbol && symbol.declarations.some(ts.isJSDocTypedefTag)) {
- addDuplicateDeclarationErrorsForSymbols(symbol, ts.Diagnostics.Duplicate_identifier_0, ts.unescapeLeadingUnderscores(name), prop);
- addDuplicateDeclarationErrorsForSymbols(prop, ts.Diagnostics.Duplicate_identifier_0, ts.unescapeLeadingUnderscores(name), symbol);
- }
+ else {
+ callChainFlags = 0 /* None */;
+ }
+ funcType = checkNonNullTypeWithReporter(funcType, node.expression, reportCannotInvokePossiblyNullOrUndefinedError);
+ if (funcType === silentNeverType) {
+ return silentNeverSignature;
+ }
+ var apparentType = getApparentType(funcType);
+ if (apparentType === errorType) {
+ // Another error has already been reported
+ return resolveErrorCall(node);
+ }
+ // Technically, this signatures list may be incomplete. We are taking the apparent type,
+ // but we are not including call signatures that may have been added to the Object or
+ // Function interface, since they have none by default. This is a bit of a leap of faith
+ // that the user will not add any.
+ var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */);
+ var numConstructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */).length;
+ // TS 1.0 Spec: 4.12
+ // In an untyped function call no TypeArgs are permitted, Args can be any argument list, no contextual
+ // types are provided for the argument expressions, and the result is always of type Any.
+ if (isUntypedFunctionCall(funcType, apparentType, callSignatures.length, numConstructSignatures)) {
+ // The unknownType indicates that an error already occurred (and was reported). No
+ // need to report another error in this case.
+ if (funcType !== errorType && node.typeArguments) {
+ error(node, ts.Diagnostics.Untyped_function_calls_may_not_accept_type_arguments);
+ }
+ return resolveUntypedCall(node);
+ }
+ // If FuncExpr's apparent type(section 3.8.1) is a function type, the call is a typed function call.
+ // TypeScript employs overload resolution in typed function calls in order to support functions
+ // with multiple call signatures.
+ if (!callSignatures.length) {
+ if (numConstructSignatures) {
+ error(node, ts.Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new, typeToString(funcType));
+ }
+ else {
+ var relatedInformation = void 0;
+ if (node.arguments.length === 1) {
+ var text = ts.getSourceFileOfNode(node).text;
+ if (ts.isLineBreak(text.charCodeAt(ts.skipTrivia(text, node.expression.end, /* stopAfterLineBreak */ true) - 1))) {
+ relatedInformation = ts.createDiagnosticForNode(node.expression, ts.Diagnostics.Are_you_missing_a_semicolon);
}
}
+ invocationError(node.expression, apparentType, 0 /* Call */, relatedInformation);
}
+ return resolveErrorCall(node);
}
- function isEvalNode(node) {
- return node.kind === 75 /* Identifier */ && node.escapedText === "eval";
+ // When a call to a generic function is an argument to an outer call to a generic function for which
+ // inference is in process, we have a choice to make. If the inner call relies on inferences made from
+ // its contextual type to its return type, deferring the inner call processing allows the best possible
+ // contextual type to accumulate. But if the outer call relies on inferences made from the return type of
+ // the inner call, the inner call should be processed early. There's no sure way to know which choice is
+ // right (only a full unification algorithm can determine that), so we resort to the following heuristic:
+ // If no type arguments are specified in the inner call and at least one call signature is generic and
+ // returns a function type, we choose to defer processing. This narrowly permits function composition
+ // operators to flow inferences through return types, but otherwise processes calls right away. We
+ // use the resolvingSignature singleton to indicate that we deferred processing. This result will be
+ // propagated out and eventually turned into nonInferrableType (a type that is assignable to anything and
+ // from which we never make inferences).
+ if (checkMode & 8 /* SkipGenericFunctions */ && !node.typeArguments && callSignatures.some(isGenericFunctionReturningFunction)) {
+ skippedGenericFunction(node, checkMode);
+ return resolvingSignature;
}
- // Return true if there was no error, false if there was an error.
- function checkForDisallowedESSymbolOperand(operator) {
- var offendingSymbolOperand = maybeTypeOfKind(leftType, 12288 /* ESSymbolLike */) ? left :
- maybeTypeOfKind(rightType, 12288 /* ESSymbolLike */) ? right :
- undefined;
- if (offendingSymbolOperand) {
- error(offendingSymbolOperand, ts.Diagnostics.The_0_operator_cannot_be_applied_to_type_symbol, ts.tokenToString(operator));
- return false;
+ // If the function is explicitly marked with `@class`, then it must be constructed.
+ if (callSignatures.some(function (sig) { return ts.isInJSFile(sig.declaration) && !!ts.getJSDocClassTag(sig.declaration); })) {
+ error(node, ts.Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new, typeToString(funcType));
+ return resolveErrorCall(node);
+ }
+ return resolveCall(node, callSignatures, candidatesOutArray, checkMode, callChainFlags);
+ }
+ function isGenericFunctionReturningFunction(signature) {
+ return !!(signature.typeParameters && isFunctionType(getReturnTypeOfSignature(signature)));
+ }
+ /**
+ * TS 1.0 spec: 4.12
+ * If FuncExpr is of type Any, or of an object type that has no call or construct signatures
+ * but is a subtype of the Function interface, the call is an untyped function call.
+ */
+ function isUntypedFunctionCall(funcType, apparentFuncType, numCallSignatures, numConstructSignatures) {
+ // We exclude union types because we may have a union of function types that happen to have no common signatures.
+ return isTypeAny(funcType) || isTypeAny(apparentFuncType) && !!(funcType.flags & 262144 /* TypeParameter */) ||
+ !numCallSignatures && !numConstructSignatures && !(apparentFuncType.flags & (1048576 /* Union */ | 131072 /* Never */)) && isTypeAssignableTo(funcType, globalFunctionType);
+ }
+ function resolveNewExpression(node, candidatesOutArray, checkMode) {
+ if (node.arguments && languageVersion < 1 /* ES5 */) {
+ var spreadIndex = getSpreadArgumentIndex(node.arguments);
+ if (spreadIndex >= 0) {
+ error(node.arguments[spreadIndex], ts.Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher);
}
- return true;
}
- function getSuggestedBooleanOperator(operator) {
- switch (operator) {
- case 51 /* BarToken */:
- case 73 /* BarEqualsToken */:
- return 56 /* BarBarToken */;
- case 52 /* CaretToken */:
- case 74 /* CaretEqualsToken */:
- return 37 /* ExclamationEqualsEqualsToken */;
- case 50 /* AmpersandToken */:
- case 72 /* AmpersandEqualsToken */:
- return 55 /* AmpersandAmpersandToken */;
- default:
- return undefined;
+ var expressionType = checkNonNullExpression(node.expression);
+ if (expressionType === silentNeverType) {
+ return silentNeverSignature;
+ }
+ // If expressionType's apparent type(section 3.8.1) is an object type with one or
+ // more construct signatures, the expression is processed in the same manner as a
+ // function call, but using the construct signatures as the initial set of candidate
+ // signatures for overload resolution. The result type of the function call becomes
+ // the result type of the operation.
+ expressionType = getApparentType(expressionType);
+ if (expressionType === errorType) {
+ // Another error has already been reported
+ return resolveErrorCall(node);
+ }
+ // TS 1.0 spec: 4.11
+ // If expressionType is of type Any, Args can be any argument
+ // list and the result of the operation is of type Any.
+ if (isTypeAny(expressionType)) {
+ if (node.typeArguments) {
+ error(node, ts.Diagnostics.Untyped_function_calls_may_not_accept_type_arguments);
}
+ return resolveUntypedCall(node);
}
- function checkAssignmentOperator(valueType) {
- if (produceDiagnostics && ts.isAssignmentOperator(operator)) {
- // TypeScript 1.0 spec (April 2014): 4.17
- // An assignment of the form
- // VarExpr = ValueExpr
- // requires VarExpr to be classified as a reference
- // A compound assignment furthermore requires VarExpr to be classified as a reference (section 4.1)
- // and the type of the non-compound operation to be assignable to the type of VarExpr.
- if (checkReferenceExpression(left, ts.Diagnostics.The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access, ts.Diagnostics.The_left_hand_side_of_an_assignment_expression_may_not_be_an_optional_property_access)
- && (!ts.isIdentifier(left) || ts.unescapeLeadingUnderscores(left.escapedText) !== "exports")) {
- // to avoid cascading errors check assignability only if 'isReference' check succeeded and no errors were reported
- checkTypeAssignableToAndOptionallyElaborate(valueType, leftType, left, right);
- }
+ // Technically, this signatures list may be incomplete. We are taking the apparent type,
+ // but we are not including construct signatures that may have been added to the Object or
+ // Function interface, since they have none by default. This is a bit of a leap of faith
+ // that the user will not add any.
+ var constructSignatures = getSignaturesOfType(expressionType, 1 /* Construct */);
+ if (constructSignatures.length) {
+ if (!isConstructorAccessible(node, constructSignatures[0])) {
+ return resolveErrorCall(node);
+ }
+ // If the expression is a class of abstract type, then it cannot be instantiated.
+ // Note, only class declarations can be declared abstract.
+ // In the case of a merged class-module or class-interface declaration,
+ // only the class declaration node will have the Abstract flag set.
+ var valueDecl = expressionType.symbol && ts.getClassLikeDeclarationOfSymbol(expressionType.symbol);
+ if (valueDecl && ts.hasSyntacticModifier(valueDecl, 128 /* Abstract */)) {
+ error(node, ts.Diagnostics.Cannot_create_an_instance_of_an_abstract_class);
+ return resolveErrorCall(node);
}
+ return resolveCall(node, constructSignatures, candidatesOutArray, checkMode, 0 /* None */);
}
- function isAssignmentDeclaration(kind) {
- switch (kind) {
- case 2 /* ModuleExports */:
- return true;
- case 1 /* ExportsProperty */:
- case 5 /* Property */:
- case 6 /* Prototype */:
- case 3 /* PrototypeProperty */:
- case 4 /* ThisProperty */:
- var symbol = getSymbolOfNode(left);
- var init = ts.getAssignedExpandoInitializer(right);
- return init && ts.isObjectLiteralExpression(init) &&
- symbol && ts.hasEntries(symbol.exports);
- default:
- return false;
+ // If expressionType's apparent type is an object type with no construct signatures but
+ // one or more call signatures, the expression is processed as a function call. A compile-time
+ // error occurs if the result of the function call is not Void. The type of the result of the
+ // operation is Any. It is an error to have a Void this type.
+ var callSignatures = getSignaturesOfType(expressionType, 0 /* Call */);
+ if (callSignatures.length) {
+ var signature = resolveCall(node, callSignatures, candidatesOutArray, checkMode, 0 /* None */);
+ if (!noImplicitAny) {
+ if (signature.declaration && !isJSConstructor(signature.declaration) && getReturnTypeOfSignature(signature) !== voidType) {
+ error(node, ts.Diagnostics.Only_a_void_function_can_be_called_with_the_new_keyword);
+ }
+ if (getThisTypeOfSignature(signature) === voidType) {
+ error(node, ts.Diagnostics.A_function_that_is_called_with_the_new_keyword_cannot_have_a_this_type_that_is_void);
+ }
}
+ return signature;
}
- /**
- * Returns true if an error is reported
- */
- function reportOperatorErrorUnless(typesAreCompatible) {
- if (!typesAreCompatible(leftType, rightType)) {
- reportOperatorError(typesAreCompatible);
- return true;
+ invocationError(node.expression, expressionType, 1 /* Construct */);
+ return resolveErrorCall(node);
+ }
+ function typeHasProtectedAccessibleBase(target, type) {
+ var baseTypes = getBaseTypes(type);
+ if (!ts.length(baseTypes)) {
+ return false;
+ }
+ var firstBase = baseTypes[0];
+ if (firstBase.flags & 2097152 /* Intersection */) {
+ var types = firstBase.types;
+ var mixinFlags = findMixins(types);
+ var i = 0;
+ for (var _i = 0, _a = firstBase.types; _i < _a.length; _i++) {
+ var intersectionMember = _a[_i];
+ // We want to ignore mixin ctors
+ if (!mixinFlags[i]) {
+ if (ts.getObjectFlags(intersectionMember) & (1 /* Class */ | 2 /* Interface */)) {
+ if (intersectionMember.symbol === target) {
+ return true;
+ }
+ if (typeHasProtectedAccessibleBase(target, intersectionMember)) {
+ return true;
+ }
+ }
+ }
+ i++;
}
return false;
}
- function reportOperatorError(isRelated) {
- var _a;
- var wouldWorkWithAwait = false;
- var errNode = errorNode || operatorToken;
- if (isRelated) {
- var awaitedLeftType = getAwaitedType(leftType);
- var awaitedRightType = getAwaitedType(rightType);
- wouldWorkWithAwait = !(awaitedLeftType === leftType && awaitedRightType === rightType)
- && !!(awaitedLeftType && awaitedRightType)
- && isRelated(awaitedLeftType, awaitedRightType);
+ if (firstBase.symbol === target) {
+ return true;
+ }
+ return typeHasProtectedAccessibleBase(target, firstBase);
+ }
+ function isConstructorAccessible(node, signature) {
+ if (!signature || !signature.declaration) {
+ return true;
+ }
+ var declaration = signature.declaration;
+ var modifiers = ts.getSelectedEffectiveModifierFlags(declaration, 24 /* NonPublicAccessibilityModifier */);
+ // (1) Public constructors and (2) constructor functions are always accessible.
+ if (!modifiers || declaration.kind !== 166 /* Constructor */) {
+ return true;
+ }
+ var declaringClassDeclaration = ts.getClassLikeDeclarationOfSymbol(declaration.parent.symbol);
+ var declaringClass = getDeclaredTypeOfSymbol(declaration.parent.symbol);
+ // A private or protected constructor can only be instantiated within its own class (or a subclass, for protected)
+ if (!isNodeWithinClass(node, declaringClassDeclaration)) {
+ var containingClass = ts.getContainingClass(node);
+ if (containingClass && modifiers & 16 /* Protected */) {
+ var containingType = getTypeOfNode(containingClass);
+ if (typeHasProtectedAccessibleBase(declaration.parent.symbol, containingType)) {
+ return true;
+ }
}
- var effectiveLeft = leftType;
- var effectiveRight = rightType;
- if (!wouldWorkWithAwait && isRelated) {
- _a = getBaseTypesIfUnrelated(leftType, rightType, isRelated), effectiveLeft = _a[0], effectiveRight = _a[1];
+ if (modifiers & 8 /* Private */) {
+ error(node, ts.Diagnostics.Constructor_of_class_0_is_private_and_only_accessible_within_the_class_declaration, typeToString(declaringClass));
}
- var _b = getTypeNamesForErrorDisplay(effectiveLeft, effectiveRight), leftStr = _b[0], rightStr = _b[1];
- if (!tryGiveBetterPrimaryError(errNode, wouldWorkWithAwait, leftStr, rightStr)) {
- errorAndMaybeSuggestAwait(errNode, wouldWorkWithAwait, ts.Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2, ts.tokenToString(operatorToken.kind), leftStr, rightStr);
+ if (modifiers & 16 /* Protected */) {
+ error(node, ts.Diagnostics.Constructor_of_class_0_is_protected_and_only_accessible_within_the_class_declaration, typeToString(declaringClass));
}
+ return false;
}
- function tryGiveBetterPrimaryError(errNode, maybeMissingAwait, leftStr, rightStr) {
- var typeName;
- switch (operatorToken.kind) {
- case 36 /* EqualsEqualsEqualsToken */:
- case 34 /* EqualsEqualsToken */:
- typeName = "false";
- break;
- case 37 /* ExclamationEqualsEqualsToken */:
- case 35 /* ExclamationEqualsToken */:
- typeName = "true";
+ return true;
+ }
+ function invocationErrorDetails(errorTarget, apparentType, kind) {
+ var errorInfo;
+ var isCall = kind === 0 /* Call */;
+ var awaitedType = getAwaitedType(apparentType);
+ var maybeMissingAwait = awaitedType && getSignaturesOfType(awaitedType, kind).length > 0;
+ if (apparentType.flags & 1048576 /* Union */) {
+ var types = apparentType.types;
+ var hasSignatures = false;
+ for (var _i = 0, types_19 = types; _i < types_19.length; _i++) {
+ var constituent = types_19[_i];
+ var signatures = getSignaturesOfType(constituent, kind);
+ if (signatures.length !== 0) {
+ hasSignatures = true;
+ if (errorInfo) {
+ // Bail early if we already have an error, no chance of "No constituent of type is callable"
+ break;
+ }
+ }
+ else {
+ // Error on the first non callable constituent only
+ if (!errorInfo) {
+ errorInfo = ts.chainDiagnosticMessages(errorInfo, isCall ?
+ ts.Diagnostics.Type_0_has_no_call_signatures :
+ ts.Diagnostics.Type_0_has_no_construct_signatures, typeToString(constituent));
+ errorInfo = ts.chainDiagnosticMessages(errorInfo, isCall ?
+ ts.Diagnostics.Not_all_constituents_of_type_0_are_callable :
+ ts.Diagnostics.Not_all_constituents_of_type_0_are_constructable, typeToString(apparentType));
+ }
+ if (hasSignatures) {
+ // Bail early if we already found a siganture, no chance of "No constituent of type is callable"
+ break;
+ }
+ }
}
- if (typeName) {
- return errorAndMaybeSuggestAwait(errNode, maybeMissingAwait, ts.Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap, typeName, leftStr, rightStr);
+ if (!hasSignatures) {
+ errorInfo = ts.chainDiagnosticMessages(
+ /* detials */ undefined, isCall ?
+ ts.Diagnostics.No_constituent_of_type_0_is_callable :
+ ts.Diagnostics.No_constituent_of_type_0_is_constructable, typeToString(apparentType));
+ }
+ if (!errorInfo) {
+ errorInfo = ts.chainDiagnosticMessages(errorInfo, isCall ?
+ ts.Diagnostics.Each_member_of_the_union_type_0_has_signatures_but_none_of_those_signatures_are_compatible_with_each_other :
+ ts.Diagnostics.Each_member_of_the_union_type_0_has_construct_signatures_but_none_of_those_signatures_are_compatible_with_each_other, typeToString(apparentType));
}
- return undefined;
}
+ else {
+ errorInfo = ts.chainDiagnosticMessages(errorInfo, isCall ?
+ ts.Diagnostics.Type_0_has_no_call_signatures :
+ ts.Diagnostics.Type_0_has_no_construct_signatures, typeToString(apparentType));
+ }
+ var headMessage = isCall ? ts.Diagnostics.This_expression_is_not_callable : ts.Diagnostics.This_expression_is_not_constructable;
+ // Diagnose get accessors incorrectly called as functions
+ if (ts.isCallExpression(errorTarget.parent) && errorTarget.parent.arguments.length === 0) {
+ var resolvedSymbol = getNodeLinks(errorTarget).resolvedSymbol;
+ if (resolvedSymbol && resolvedSymbol.flags & 32768 /* GetAccessor */) {
+ headMessage = ts.Diagnostics.This_expression_is_not_callable_because_it_is_a_get_accessor_Did_you_mean_to_use_it_without;
+ }
+ }
+ return {
+ messageChain: ts.chainDiagnosticMessages(errorInfo, headMessage),
+ relatedMessage: maybeMissingAwait ? ts.Diagnostics.Did_you_forget_to_use_await : undefined,
+ };
}
- function getBaseTypesIfUnrelated(leftType, rightType, isRelated) {
- var effectiveLeft = leftType;
- var effectiveRight = rightType;
- var leftBase = getBaseTypeOfLiteralType(leftType);
- var rightBase = getBaseTypeOfLiteralType(rightType);
- if (!isRelated(leftBase, rightBase)) {
- effectiveLeft = leftBase;
- effectiveRight = rightBase;
+ function invocationError(errorTarget, apparentType, kind, relatedInformation) {
+ var _a = invocationErrorDetails(errorTarget, apparentType, kind), messageChain = _a.messageChain, relatedInfo = _a.relatedMessage;
+ var diagnostic = ts.createDiagnosticForNodeFromMessageChain(errorTarget, messageChain);
+ if (relatedInfo) {
+ ts.addRelatedInfo(diagnostic, ts.createDiagnosticForNode(errorTarget, relatedInfo));
}
- return [effectiveLeft, effectiveRight];
+ if (ts.isCallExpression(errorTarget.parent)) {
+ var _b = getDiagnosticSpanForCallNode(errorTarget.parent, /* doNotIncludeArguments */ true), start = _b.start, length_7 = _b.length;
+ diagnostic.start = start;
+ diagnostic.length = length_7;
+ }
+ diagnostics.add(diagnostic);
+ invocationErrorRecovery(apparentType, kind, relatedInformation ? ts.addRelatedInfo(diagnostic, relatedInformation) : diagnostic);
}
- function checkYieldExpression(node) {
- // Grammar checking
- if (produceDiagnostics) {
- if (!(node.flags & 8192 /* YieldContext */)) {
- grammarErrorOnFirstToken(node, ts.Diagnostics.A_yield_expression_is_only_allowed_in_a_generator_body);
- }
- if (isInParameterInitializerBeforeContainingFunction(node)) {
- error(node, ts.Diagnostics.yield_expressions_cannot_be_used_in_a_parameter_initializer);
+ function invocationErrorRecovery(apparentType, kind, diagnostic) {
+ if (!apparentType.symbol) {
+ return;
+ }
+ var importNode = getSymbolLinks(apparentType.symbol).originatingImport;
+ // Create a diagnostic on the originating import if possible onto which we can attach a quickfix
+ // An import call expression cannot be rewritten into another form to correct the error - the only solution is to use `.default` at the use-site
+ if (importNode && !ts.isImportCall(importNode)) {
+ var sigs = getSignaturesOfType(getTypeOfSymbol(getSymbolLinks(apparentType.symbol).target), kind);
+ if (!sigs || !sigs.length)
+ return;
+ ts.addRelatedInfo(diagnostic, ts.createDiagnosticForNode(importNode, ts.Diagnostics.Type_originates_at_this_import_A_namespace_style_import_cannot_be_called_or_constructed_and_will_cause_a_failure_at_runtime_Consider_using_a_default_import_or_import_require_here_instead));
+ }
+ }
+ function resolveTaggedTemplateExpression(node, candidatesOutArray, checkMode) {
+ var tagType = checkExpression(node.tag);
+ var apparentType = getApparentType(tagType);
+ if (apparentType === errorType) {
+ // Another error has already been reported
+ return resolveErrorCall(node);
+ }
+ var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */);
+ var numConstructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */).length;
+ if (isUntypedFunctionCall(tagType, apparentType, callSignatures.length, numConstructSignatures)) {
+ return resolveUntypedCall(node);
+ }
+ if (!callSignatures.length) {
+ if (ts.isArrayLiteralExpression(node.parent)) {
+ var diagnostic = ts.createDiagnosticForNode(node.tag, ts.Diagnostics.It_is_likely_that_you_are_missing_a_comma_to_separate_these_two_template_expressions_They_form_a_tagged_template_expression_which_cannot_be_invoked);
+ diagnostics.add(diagnostic);
+ return resolveErrorCall(node);
}
+ invocationError(node.tag, apparentType, 0 /* Call */);
+ return resolveErrorCall(node);
}
- var func = ts.getContainingFunction(node);
- if (!func)
- return anyType;
- var functionFlags = ts.getFunctionFlags(func);
- if (!(functionFlags & 1 /* Generator */)) {
- // If the user's code is syntactically correct, the func should always have a star. After all, we are in a yield context.
- return anyType;
+ return resolveCall(node, callSignatures, candidatesOutArray, checkMode, 0 /* None */);
+ }
+ /**
+ * Gets the localized diagnostic head message to use for errors when resolving a decorator as a call expression.
+ */
+ function getDiagnosticHeadMessageForDecoratorResolution(node) {
+ switch (node.parent.kind) {
+ case 252 /* ClassDeclaration */:
+ case 221 /* ClassExpression */:
+ return ts.Diagnostics.Unable_to_resolve_signature_of_class_decorator_when_called_as_an_expression;
+ case 160 /* Parameter */:
+ return ts.Diagnostics.Unable_to_resolve_signature_of_parameter_decorator_when_called_as_an_expression;
+ case 163 /* PropertyDeclaration */:
+ return ts.Diagnostics.Unable_to_resolve_signature_of_property_decorator_when_called_as_an_expression;
+ case 165 /* MethodDeclaration */:
+ case 167 /* GetAccessor */:
+ case 168 /* SetAccessor */:
+ return ts.Diagnostics.Unable_to_resolve_signature_of_method_decorator_when_called_as_an_expression;
+ default:
+ return ts.Debug.fail();
}
- var isAsync = (functionFlags & 2 /* Async */) !== 0;
- if (node.asteriskToken) {
- // Async generator functions prior to ESNext require the __await, __asyncDelegator,
- // and __asyncValues helpers
- if (isAsync && languageVersion < 99 /* ESNext */) {
- checkExternalEmitHelpers(node, 53248 /* AsyncDelegatorIncludes */);
+ }
+ /**
+ * Resolves a decorator as if it were a call expression.
+ */
+ function resolveDecorator(node, candidatesOutArray, checkMode) {
+ var funcType = checkExpression(node.expression);
+ var apparentType = getApparentType(funcType);
+ if (apparentType === errorType) {
+ return resolveErrorCall(node);
+ }
+ var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */);
+ var numConstructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */).length;
+ if (isUntypedFunctionCall(funcType, apparentType, callSignatures.length, numConstructSignatures)) {
+ return resolveUntypedCall(node);
+ }
+ if (isPotentiallyUncalledDecorator(node, callSignatures)) {
+ var nodeStr = ts.getTextOfNode(node.expression, /*includeTrivia*/ false);
+ error(node, ts.Diagnostics._0_accepts_too_few_arguments_to_be_used_as_a_decorator_here_Did_you_mean_to_call_it_first_and_write_0, nodeStr);
+ return resolveErrorCall(node);
+ }
+ var headMessage = getDiagnosticHeadMessageForDecoratorResolution(node);
+ if (!callSignatures.length) {
+ var errorDetails = invocationErrorDetails(node.expression, apparentType, 0 /* Call */);
+ var messageChain = ts.chainDiagnosticMessages(errorDetails.messageChain, headMessage);
+ var diag = ts.createDiagnosticForNodeFromMessageChain(node.expression, messageChain);
+ if (errorDetails.relatedMessage) {
+ ts.addRelatedInfo(diag, ts.createDiagnosticForNode(node.expression, errorDetails.relatedMessage));
}
- // Generator functions prior to ES2015 require the __values helper
- if (!isAsync && languageVersion < 2 /* ES2015 */ && compilerOptions.downlevelIteration) {
- checkExternalEmitHelpers(node, 256 /* Values */);
+ diagnostics.add(diag);
+ invocationErrorRecovery(apparentType, 0 /* Call */, diag);
+ return resolveErrorCall(node);
+ }
+ return resolveCall(node, callSignatures, candidatesOutArray, checkMode, 0 /* None */, headMessage);
+ }
+ function createSignatureForJSXIntrinsic(node, result) {
+ var namespace = getJsxNamespaceAt(node);
+ var exports = namespace && getExportsOfSymbol(namespace);
+ // We fake up a SFC signature for each intrinsic, however a more specific per-element signature drawn from the JSX declaration
+ // file would probably be preferable.
+ var typeSymbol = exports && getSymbol(exports, JsxNames.Element, 788968 /* Type */);
+ var returnNode = typeSymbol && nodeBuilder.symbolToEntityName(typeSymbol, 788968 /* Type */, node);
+ var declaration = ts.factory.createFunctionTypeNode(/*typeParameters*/ undefined, [ts.factory.createParameterDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, /*dotdotdot*/ undefined, "props", /*questionMark*/ undefined, nodeBuilder.typeToTypeNode(result, node))], returnNode ? ts.factory.createTypeReferenceNode(returnNode, /*typeArguments*/ undefined) : ts.factory.createKeywordTypeNode(128 /* AnyKeyword */));
+ var parameterSymbol = createSymbol(1 /* FunctionScopedVariable */, "props");
+ parameterSymbol.type = result;
+ return createSignature(declaration,
+ /*typeParameters*/ undefined,
+ /*thisParameter*/ undefined, [parameterSymbol], typeSymbol ? getDeclaredTypeOfSymbol(typeSymbol) : errorType,
+ /*returnTypePredicate*/ undefined, 1, 0 /* None */);
+ }
+ function resolveJsxOpeningLikeElement(node, candidatesOutArray, checkMode) {
+ if (isJsxIntrinsicIdentifier(node.tagName)) {
+ var result = getIntrinsicAttributesTypeFromJsxOpeningLikeElement(node);
+ var fakeSignature = createSignatureForJSXIntrinsic(node, result);
+ checkTypeAssignableToAndOptionallyElaborate(checkExpressionWithContextualType(node.attributes, getEffectiveFirstArgumentForJsxSignature(fakeSignature, node), /*mapper*/ undefined, 0 /* Normal */), result, node.tagName, node.attributes);
+ if (ts.length(node.typeArguments)) {
+ ts.forEach(node.typeArguments, checkSourceElement);
+ diagnostics.add(ts.createDiagnosticForNodeArray(ts.getSourceFileOfNode(node), node.typeArguments, ts.Diagnostics.Expected_0_type_arguments_but_got_1, 0, ts.length(node.typeArguments)));
}
+ return fakeSignature;
}
- // There is no point in doing an assignability check if the function
- // has no explicit return type because the return type is directly computed
- // from the yield expressions.
- var returnType = getReturnTypeFromAnnotation(func);
- var iterationTypes = returnType && getIterationTypesOfGeneratorFunctionReturnType(returnType, isAsync);
- var signatureYieldType = iterationTypes && iterationTypes.yieldType || anyType;
- var signatureNextType = iterationTypes && iterationTypes.nextType || anyType;
- var resolvedSignatureNextType = isAsync ? getAwaitedType(signatureNextType) || anyType : signatureNextType;
- var yieldExpressionType = node.expression ? checkExpression(node.expression) : undefinedWideningType;
- var yieldedType = getYieldedTypeOfYieldExpression(node, yieldExpressionType, resolvedSignatureNextType, isAsync);
- if (returnType && yieldedType) {
- checkTypeAssignableToAndOptionallyElaborate(yieldedType, signatureYieldType, node.expression || node, node.expression);
+ var exprTypes = checkExpression(node.tagName);
+ var apparentType = getApparentType(exprTypes);
+ if (apparentType === errorType) {
+ return resolveErrorCall(node);
}
- if (node.asteriskToken) {
- var use = isAsync ? 19 /* AsyncYieldStar */ : 17 /* YieldStar */;
- return getIterationTypeOfIterable(use, 1 /* Return */, yieldExpressionType, node.expression)
- || anyType;
+ var signatures = getUninstantiatedJsxSignaturesOfType(exprTypes, node);
+ if (isUntypedFunctionCall(exprTypes, apparentType, signatures.length, /*constructSignatures*/ 0)) {
+ return resolveUntypedCall(node);
}
- else if (returnType) {
- return getIterationTypeOfGeneratorFunctionReturnType(2 /* Next */, returnType, isAsync)
- || anyType;
+ if (signatures.length === 0) {
+ // We found no signatures at all, which is an error
+ error(node.tagName, ts.Diagnostics.JSX_element_type_0_does_not_have_any_construct_or_call_signatures, ts.getTextOfNode(node.tagName));
+ return resolveErrorCall(node);
}
- return getContextualIterationType(2 /* Next */, func) || anyType;
- }
- function checkConditionalExpression(node, checkMode) {
- var type = checkTruthinessExpression(node.condition);
- checkTestingKnownTruthyCallableType(node.condition, node.whenTrue, type);
- var type1 = checkExpression(node.whenTrue, checkMode);
- var type2 = checkExpression(node.whenFalse, checkMode);
- return getUnionType([type1, type2], 2 /* Subtype */);
+ return resolveCall(node, signatures, candidatesOutArray, checkMode, 0 /* None */);
}
- function checkTemplateExpression(node) {
- // We just want to check each expressions, but we are unconcerned with
- // the type of each expression, as any value may be coerced into a string.
- // It is worth asking whether this is what we really want though.
- // A place where we actually *are* concerned with the expressions' types are
- // in tagged templates.
- ts.forEach(node.templateSpans, function (templateSpan) {
- if (maybeTypeOfKind(checkExpression(templateSpan.expression), 12288 /* ESSymbolLike */)) {
- error(templateSpan.expression, ts.Diagnostics.Implicit_conversion_of_a_symbol_to_a_string_will_fail_at_runtime_Consider_wrapping_this_expression_in_String);
- }
+ /**
+ * Sometimes, we have a decorator that could accept zero arguments,
+ * but is receiving too many arguments as part of the decorator invocation.
+ * In those cases, a user may have meant to *call* the expression before using it as a decorator.
+ */
+ function isPotentiallyUncalledDecorator(decorator, signatures) {
+ return signatures.length && ts.every(signatures, function (signature) {
+ return signature.minArgumentCount === 0 &&
+ !signatureHasRestParameter(signature) &&
+ signature.parameters.length < getDecoratorArgumentCount(decorator, signature);
});
- return stringType;
}
- function getContextNode(node) {
- if (node.kind === 274 /* JsxAttributes */ && !ts.isJsxSelfClosingElement(node.parent)) {
- return node.parent.parent; // Needs to be the root JsxElement, so it encompasses the attributes _and_ the children (which are essentially part of the attributes)
+ function resolveSignature(node, candidatesOutArray, checkMode) {
+ switch (node.kind) {
+ case 203 /* CallExpression */:
+ return resolveCallExpression(node, candidatesOutArray, checkMode);
+ case 204 /* NewExpression */:
+ return resolveNewExpression(node, candidatesOutArray, checkMode);
+ case 205 /* TaggedTemplateExpression */:
+ return resolveTaggedTemplateExpression(node, candidatesOutArray, checkMode);
+ case 161 /* Decorator */:
+ return resolveDecorator(node, candidatesOutArray, checkMode);
+ case 275 /* JsxOpeningElement */:
+ case 274 /* JsxSelfClosingElement */:
+ return resolveJsxOpeningLikeElement(node, candidatesOutArray, checkMode);
}
- return node;
+ throw ts.Debug.assertNever(node, "Branch in 'resolveSignature' should be unreachable.");
}
- function checkExpressionWithContextualType(node, contextualType, inferenceContext, checkMode) {
- var context = getContextNode(node);
- var saveContextualType = context.contextualType;
- var saveInferenceContext = context.inferenceContext;
- try {
- context.contextualType = contextualType;
- context.inferenceContext = inferenceContext;
- var type = checkExpression(node, checkMode | 1 /* Contextual */ | (inferenceContext ? 2 /* Inferential */ : 0));
- // We strip literal freshness when an appropriate contextual type is present such that contextually typed
- // literals always preserve their literal types (otherwise they might widen during type inference). An alternative
- // here would be to not mark contextually typed literals as fresh in the first place.
- var result = maybeTypeOfKind(type, 2944 /* Literal */) && isLiteralOfContextualType(type, instantiateContextualType(contextualType, node)) ?
- getRegularTypeOfLiteralType(type) : type;
- return result;
+ /**
+ * Resolve a signature of a given call-like expression.
+ * @param node a call-like expression to try resolve a signature for
+ * @param candidatesOutArray an array of signature to be filled in by the function. It is passed by signature help in the language service;
+ * the function will fill it up with appropriate candidate signatures
+ * @return a signature of the call-like expression or undefined if one can't be found
+ */
+ function getResolvedSignature(node, candidatesOutArray, checkMode) {
+ var links = getNodeLinks(node);
+ // If getResolvedSignature has already been called, we will have cached the resolvedSignature.
+ // However, it is possible that either candidatesOutArray was not passed in the first time,
+ // or that a different candidatesOutArray was passed in. Therefore, we need to redo the work
+ // to correctly fill the candidatesOutArray.
+ var cached = links.resolvedSignature;
+ if (cached && cached !== resolvingSignature && !candidatesOutArray) {
+ return cached;
}
- finally {
- // In the event our operation is canceled or some other exception occurs, reset the contextual type
- // so that we do not accidentally hold onto an instance of the checker, as a Type created in the services layer
- // may hold onto the checker that created it.
- context.contextualType = saveContextualType;
- context.inferenceContext = saveInferenceContext;
+ links.resolvedSignature = resolvingSignature;
+ var result = resolveSignature(node, candidatesOutArray, checkMode || 0 /* Normal */);
+ // When CheckMode.SkipGenericFunctions is set we use resolvingSignature to indicate that call
+ // resolution should be deferred.
+ if (result !== resolvingSignature) {
+ // If signature resolution originated in control flow type analysis (for example to compute the
+ // assigned type in a flow assignment) we don't cache the result as it may be based on temporary
+ // types from the control flow analysis.
+ links.resolvedSignature = flowLoopStart === flowLoopCount ? result : cached;
}
+ return result;
}
- function checkExpressionCached(node, checkMode) {
- var links = getNodeLinks(node);
- if (!links.resolvedType) {
- if (checkMode && checkMode !== 0 /* Normal */) {
- return checkExpression(node, checkMode);
- }
- // When computing a type that we're going to cache, we need to ignore any ongoing control flow
- // analysis because variables may have transient types in indeterminable states. Moving flowLoopStart
- // to the top of the stack ensures all transient types are computed from a known point.
- var saveFlowLoopStart = flowLoopStart;
- var saveFlowTypeCache = flowTypeCache;
- flowLoopStart = flowLoopCount;
- flowTypeCache = undefined;
- links.resolvedType = checkExpression(node, checkMode);
- flowTypeCache = saveFlowTypeCache;
- flowLoopStart = saveFlowLoopStart;
+ /**
+ * Indicates whether a declaration can be treated as a constructor in a JavaScript
+ * file.
+ */
+ function isJSConstructor(node) {
+ var _a;
+ if (!node || !ts.isInJSFile(node)) {
+ return false;
}
- return links.resolvedType;
- }
- function isTypeAssertion(node) {
- node = ts.skipParentheses(node);
- return node.kind === 199 /* TypeAssertionExpression */ || node.kind === 217 /* AsExpression */;
- }
- function checkDeclarationInitializer(declaration, contextualType) {
- var initializer = ts.getEffectiveInitializer(declaration);
- var type = getQuickTypeOfExpression(initializer) ||
- (contextualType ? checkExpressionWithContextualType(initializer, contextualType, /*inferenceContext*/ undefined, 0 /* Normal */) : checkExpressionCached(initializer));
- return ts.isParameter(declaration) && declaration.name.kind === 190 /* ArrayBindingPattern */ &&
- isTupleType(type) && !type.target.hasRestElement && getTypeReferenceArity(type) < declaration.name.elements.length ?
- padTupleType(type, declaration.name) : type;
+ var func = ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) ? node :
+ ts.isVariableDeclaration(node) && node.initializer && ts.isFunctionExpression(node.initializer) ? node.initializer :
+ undefined;
+ if (func) {
+ // If the node has a @class tag, treat it like a constructor.
+ if (ts.getJSDocClassTag(node))
+ return true;
+ // If the symbol of the node has members, treat it like a constructor.
+ var symbol = getSymbolOfNode(func);
+ return !!((_a = symbol === null || symbol === void 0 ? void 0 : symbol.members) === null || _a === void 0 ? void 0 : _a.size);
+ }
+ return false;
}
- function padTupleType(type, pattern) {
- var patternElements = pattern.elements;
- var arity = getTypeReferenceArity(type);
- var elementTypes = arity ? getTypeArguments(type).slice() : [];
- for (var i = arity; i < patternElements.length; i++) {
- var e = patternElements[i];
- if (i < patternElements.length - 1 || !(e.kind === 191 /* BindingElement */ && e.dotDotDotToken)) {
- elementTypes.push(!ts.isOmittedExpression(e) && hasDefaultValue(e) ? getTypeFromBindingElement(e, /*includePatternInType*/ false, /*reportErrors*/ false) : anyType);
- if (!ts.isOmittedExpression(e) && !hasDefaultValue(e)) {
- reportImplicitAny(e, anyType);
+ function mergeJSSymbols(target, source) {
+ var _a, _b;
+ if (source) {
+ var links = getSymbolLinks(source);
+ if (!links.inferredClassSymbol || !links.inferredClassSymbol.has(getSymbolId(target))) {
+ var inferred = ts.isTransientSymbol(target) ? target : cloneSymbol(target);
+ inferred.exports = inferred.exports || ts.createSymbolTable();
+ inferred.members = inferred.members || ts.createSymbolTable();
+ inferred.flags |= source.flags & 32 /* Class */;
+ if ((_a = source.exports) === null || _a === void 0 ? void 0 : _a.size) {
+ mergeSymbolTable(inferred.exports, source.exports);
+ }
+ if ((_b = source.members) === null || _b === void 0 ? void 0 : _b.size) {
+ mergeSymbolTable(inferred.members, source.members);
}
+ (links.inferredClassSymbol || (links.inferredClassSymbol = new ts.Map())).set(getSymbolId(inferred), inferred);
+ return inferred;
}
+ return links.inferredClassSymbol.get(getSymbolId(target));
}
- return createTupleType(elementTypes, type.target.minLength, /*hasRestElement*/ false, type.target.readonly);
}
- function widenTypeInferredFromInitializer(declaration, type) {
- var widened = ts.getCombinedNodeFlags(declaration) & 2 /* Const */ || ts.isDeclarationReadonly(declaration) ? type : getWidenedLiteralType(type);
- if (ts.isInJSFile(declaration)) {
- if (widened.flags & 98304 /* Nullable */) {
- reportImplicitAny(declaration, anyType);
- return anyType;
- }
- else if (isEmptyArrayLiteralType(widened)) {
- reportImplicitAny(declaration, anyArrayType);
- return anyArrayType;
+ function getAssignedClassSymbol(decl) {
+ var _a;
+ var assignmentSymbol = decl && getSymbolOfExpando(decl, /*allowDeclaration*/ true);
+ var prototype = (_a = assignmentSymbol === null || assignmentSymbol === void 0 ? void 0 : assignmentSymbol.exports) === null || _a === void 0 ? void 0 : _a.get("prototype");
+ var init = (prototype === null || prototype === void 0 ? void 0 : prototype.valueDeclaration) && getAssignedJSPrototype(prototype.valueDeclaration);
+ return init ? getSymbolOfNode(init) : undefined;
+ }
+ function getSymbolOfExpando(node, allowDeclaration) {
+ if (!node.parent) {
+ return undefined;
+ }
+ var name;
+ var decl;
+ if (ts.isVariableDeclaration(node.parent) && node.parent.initializer === node) {
+ if (!ts.isInJSFile(node) && !(ts.isVarConst(node.parent) && ts.isFunctionLikeDeclaration(node))) {
+ return undefined;
}
+ name = node.parent.name;
+ decl = node.parent;
}
- return widened;
- }
- function isLiteralOfContextualType(candidateType, contextualType) {
- if (contextualType) {
- if (contextualType.flags & 3145728 /* UnionOrIntersection */) {
- var types = contextualType.types;
- return ts.some(types, function (t) { return isLiteralOfContextualType(candidateType, t); });
+ else if (ts.isBinaryExpression(node.parent)) {
+ var parentNode = node.parent;
+ var parentNodeOperator = node.parent.operatorToken.kind;
+ if (parentNodeOperator === 62 /* EqualsToken */ && (allowDeclaration || parentNode.right === node)) {
+ name = parentNode.left;
+ decl = name;
}
- if (contextualType.flags & 58982400 /* InstantiableNonPrimitive */) {
- // If the contextual type is a type variable constrained to a primitive type, consider
- // this a literal context for literals of that primitive type. For example, given a
- // type parameter 'T extends string', infer string literal types for T.
- var constraint = getBaseConstraintOfType(contextualType) || unknownType;
- return maybeTypeOfKind(constraint, 4 /* String */) && maybeTypeOfKind(candidateType, 128 /* StringLiteral */) ||
- maybeTypeOfKind(constraint, 8 /* Number */) && maybeTypeOfKind(candidateType, 256 /* NumberLiteral */) ||
- maybeTypeOfKind(constraint, 64 /* BigInt */) && maybeTypeOfKind(candidateType, 2048 /* BigIntLiteral */) ||
- maybeTypeOfKind(constraint, 4096 /* ESSymbol */) && maybeTypeOfKind(candidateType, 8192 /* UniqueESSymbol */) ||
- isLiteralOfContextualType(candidateType, constraint);
+ else if (parentNodeOperator === 56 /* BarBarToken */ || parentNodeOperator === 60 /* QuestionQuestionToken */) {
+ if (ts.isVariableDeclaration(parentNode.parent) && parentNode.parent.initializer === parentNode) {
+ name = parentNode.parent.name;
+ decl = parentNode.parent;
+ }
+ else if (ts.isBinaryExpression(parentNode.parent) && parentNode.parent.operatorToken.kind === 62 /* EqualsToken */ && (allowDeclaration || parentNode.parent.right === parentNode)) {
+ name = parentNode.parent.left;
+ decl = name;
+ }
+ if (!name || !ts.isBindableStaticNameExpression(name) || !ts.isSameEntityName(name, parentNode.left)) {
+ return undefined;
+ }
}
- // If the contextual type is a literal of a particular primitive type, we consider this a
- // literal context for all literals of that primitive type.
- return !!(contextualType.flags & (128 /* StringLiteral */ | 4194304 /* Index */) && maybeTypeOfKind(candidateType, 128 /* StringLiteral */) ||
- contextualType.flags & 256 /* NumberLiteral */ && maybeTypeOfKind(candidateType, 256 /* NumberLiteral */) ||
- contextualType.flags & 2048 /* BigIntLiteral */ && maybeTypeOfKind(candidateType, 2048 /* BigIntLiteral */) ||
- contextualType.flags & 512 /* BooleanLiteral */ && maybeTypeOfKind(candidateType, 512 /* BooleanLiteral */) ||
- contextualType.flags & 8192 /* UniqueESSymbol */ && maybeTypeOfKind(candidateType, 8192 /* UniqueESSymbol */));
}
- return false;
+ else if (allowDeclaration && ts.isFunctionDeclaration(node)) {
+ name = node.name;
+ decl = node;
+ }
+ if (!decl || !name || (!allowDeclaration && !ts.getExpandoInitializer(node, ts.isPrototypeAccess(name)))) {
+ return undefined;
+ }
+ return getSymbolOfNode(decl);
}
- function isConstContext(node) {
+ function getAssignedJSPrototype(node) {
+ if (!node.parent) {
+ return false;
+ }
var parent = node.parent;
- return ts.isAssertionExpression(parent) && ts.isConstTypeReference(parent.type) ||
- (ts.isParenthesizedExpression(parent) || ts.isArrayLiteralExpression(parent) || ts.isSpreadElement(parent)) && isConstContext(parent) ||
- (ts.isPropertyAssignment(parent) || ts.isShorthandPropertyAssignment(parent)) && isConstContext(parent.parent);
- }
- function checkExpressionForMutableLocation(node, checkMode, contextualType, forceTuple) {
- var type = checkExpression(node, checkMode, forceTuple);
- return isConstContext(node) ? getRegularTypeOfLiteralType(type) :
- isTypeAssertion(node) ? type :
- getWidenedLiteralLikeTypeForContextualType(type, instantiateContextualType(arguments.length === 2 ? getContextualType(node) : contextualType, node));
- }
- function checkPropertyAssignment(node, checkMode) {
- // Do not use hasDynamicName here, because that returns false for well known symbols.
- // We want to perform checkComputedPropertyName for all computed properties, including
- // well known symbols.
- if (node.name.kind === 154 /* ComputedPropertyName */) {
- checkComputedPropertyName(node.name);
+ while (parent && parent.kind === 201 /* PropertyAccessExpression */) {
+ parent = parent.parent;
}
- return checkExpressionForMutableLocation(node.initializer, checkMode);
- }
- function checkObjectLiteralMethod(node, checkMode) {
- // Grammar checking
- checkGrammarMethod(node);
- // Do not use hasDynamicName here, because that returns false for well known symbols.
- // We want to perform checkComputedPropertyName for all computed properties, including
- // well known symbols.
- if (node.name.kind === 154 /* ComputedPropertyName */) {
- checkComputedPropertyName(node.name);
+ if (parent && ts.isBinaryExpression(parent) && ts.isPrototypeAccess(parent.left) && parent.operatorToken.kind === 62 /* EqualsToken */) {
+ var right = ts.getInitializerOfBinaryExpression(parent);
+ return ts.isObjectLiteralExpression(right) && right;
}
- var uninstantiatedType = checkFunctionExpressionOrObjectLiteralMethod(node, checkMode);
- return instantiateTypeWithSingleGenericCallSignature(node, uninstantiatedType, checkMode);
}
- function instantiateTypeWithSingleGenericCallSignature(node, type, checkMode) {
- if (checkMode && checkMode & (2 /* Inferential */ | 8 /* SkipGenericFunctions */)) {
- var callSignature = getSingleSignature(type, 0 /* Call */, /*allowMembers*/ true);
- var constructSignature = getSingleSignature(type, 1 /* Construct */, /*allowMembers*/ true);
- var signature = callSignature || constructSignature;
- if (signature && signature.typeParameters) {
- var contextualType = getApparentTypeOfContextualType(node, 2 /* NoConstraints */);
- if (contextualType) {
- var contextualSignature = getSingleSignature(getNonNullableType(contextualType), callSignature ? 0 /* Call */ : 1 /* Construct */, /*allowMembers*/ false);
- if (contextualSignature && !contextualSignature.typeParameters) {
- if (checkMode & 8 /* SkipGenericFunctions */) {
- skippedGenericFunction(node, checkMode);
- return anyFunctionType;
- }
- var context = getInferenceContext(node);
- // We have an expression that is an argument of a generic function for which we are performing
- // type argument inference. The expression is of a function type with a single generic call
- // signature and a contextual function type with a single non-generic call signature. Now check
- // if the outer function returns a function type with a single non-generic call signature and
- // if some of the outer function type parameters have no inferences so far. If so, we can
- // potentially add inferred type parameters to the outer function return type.
- var returnType = context.signature && getReturnTypeOfSignature(context.signature);
- var returnSignature = returnType && getSingleCallOrConstructSignature(returnType);
- if (returnSignature && !returnSignature.typeParameters && !ts.every(context.inferences, hasInferenceCandidates)) {
- // Instantiate the signature with its own type parameters as type arguments, possibly
- // renaming the type parameters to ensure they have unique names.
- var uniqueTypeParameters = getUniqueTypeParameters(context, signature.typeParameters);
- var instantiatedSignature = getSignatureInstantiationWithoutFillingInTypeArguments(signature, uniqueTypeParameters);
- // Infer from the parameters of the instantiated signature to the parameters of the
- // contextual signature starting with an empty set of inference candidates.
- var inferences_3 = ts.map(context.inferences, function (info) { return createInferenceInfo(info.typeParameter); });
- applyToParameterTypes(instantiatedSignature, contextualSignature, function (source, target) {
- inferTypes(inferences_3, source, target, /*priority*/ 0, /*contravariant*/ true);
- });
- if (ts.some(inferences_3, hasInferenceCandidates)) {
- // We have inference candidates, indicating that one or more type parameters are referenced
- // in the parameter types of the contextual signature. Now also infer from the return type.
- applyToReturnTypes(instantiatedSignature, contextualSignature, function (source, target) {
- inferTypes(inferences_3, source, target);
- });
- // If the type parameters for which we produced candidates do not have any inferences yet,
- // we adopt the new inference candidates and add the type parameters of the expression type
- // to the set of inferred type parameters for the outer function return type.
- if (!hasOverlappingInferences(context.inferences, inferences_3)) {
- mergeInferences(context.inferences, inferences_3);
- context.inferredTypeParameters = ts.concatenate(context.inferredTypeParameters, uniqueTypeParameters);
- return getOrCreateTypeFromSignature(instantiatedSignature);
- }
- }
- }
- return getOrCreateTypeFromSignature(instantiateSignatureInContextOf(signature, contextualSignature, context));
- }
- }
- }
+ /**
+ * Syntactically and semantically checks a call or new expression.
+ * @param node The call/new expression to be checked.
+ * @returns On success, the expression's signature's return type. On failure, anyType.
+ */
+ function checkCallExpression(node, checkMode) {
+ var _a;
+ if (!checkGrammarTypeArguments(node, node.typeArguments))
+ checkGrammarArguments(node.arguments);
+ var signature = getResolvedSignature(node, /*candidatesOutArray*/ undefined, checkMode);
+ if (signature === resolvingSignature) {
+ // CheckMode.SkipGenericFunctions is enabled and this is a call to a generic function that
+ // returns a function type. We defer checking and return nonInferrableType.
+ return nonInferrableType;
}
- return type;
- }
- function skippedGenericFunction(node, checkMode) {
- if (checkMode & 2 /* Inferential */) {
- // We have skipped a generic function during inferential typing. Obtain the inference context and
- // indicate this has occurred such that we know a second pass of inference is be needed.
- var context = getInferenceContext(node);
- context.flags |= 4 /* SkippedGenericFunction */;
+ checkDeprecatedSignature(signature, node);
+ if (node.expression.kind === 105 /* SuperKeyword */) {
+ return voidType;
}
- }
- function hasInferenceCandidates(info) {
- return !!(info.candidates || info.contraCandidates);
- }
- function hasOverlappingInferences(a, b) {
- for (var i = 0; i < a.length; i++) {
- if (hasInferenceCandidates(a[i]) && hasInferenceCandidates(b[i])) {
- return true;
+ if (node.kind === 204 /* NewExpression */) {
+ var declaration = signature.declaration;
+ if (declaration &&
+ declaration.kind !== 166 /* Constructor */ &&
+ declaration.kind !== 170 /* ConstructSignature */ &&
+ declaration.kind !== 175 /* ConstructorType */ &&
+ !ts.isJSDocConstructSignature(declaration) &&
+ !isJSConstructor(declaration)) {
+ // When resolved signature is a call signature (and not a construct signature) the result type is any
+ if (noImplicitAny) {
+ error(node, ts.Diagnostics.new_expression_whose_target_lacks_a_construct_signature_implicitly_has_an_any_type);
+ }
+ return anyType;
}
}
- return false;
- }
- function mergeInferences(target, source) {
- for (var i = 0; i < target.length; i++) {
- if (!hasInferenceCandidates(target[i]) && hasInferenceCandidates(source[i])) {
- target[i] = source[i];
- }
+ // In JavaScript files, calls to any identifier 'require' are treated as external module imports
+ if (ts.isInJSFile(node) && isCommonJsRequire(node)) {
+ return resolveExternalModuleTypeByLiteral(node.arguments[0]);
}
- }
- function getUniqueTypeParameters(context, typeParameters) {
- var result = [];
- var oldTypeParameters;
- var newTypeParameters;
- for (var _i = 0, typeParameters_2 = typeParameters; _i < typeParameters_2.length; _i++) {
- var tp = typeParameters_2[_i];
- var name = tp.symbol.escapedName;
- if (hasTypeParameterByName(context.inferredTypeParameters, name) || hasTypeParameterByName(result, name)) {
- var newName = getUniqueTypeParameterName(ts.concatenate(context.inferredTypeParameters, result), name);
- var symbol = createSymbol(262144 /* TypeParameter */, newName);
- var newTypeParameter = createTypeParameter(symbol);
- newTypeParameter.target = tp;
- oldTypeParameters = ts.append(oldTypeParameters, tp);
- newTypeParameters = ts.append(newTypeParameters, newTypeParameter);
- result.push(newTypeParameter);
+ var returnType = getReturnTypeOfSignature(signature);
+ // Treat any call to the global 'Symbol' function that is part of a const variable or readonly property
+ // as a fresh unique symbol literal type.
+ if (returnType.flags & 12288 /* ESSymbolLike */ && isSymbolOrSymbolForCall(node)) {
+ return getESSymbolLikeTypeForNode(ts.walkUpParenthesizedExpressions(node.parent));
+ }
+ if (node.kind === 203 /* CallExpression */ && node.parent.kind === 233 /* ExpressionStatement */ &&
+ returnType.flags & 16384 /* Void */ && getTypePredicateOfSignature(signature)) {
+ if (!ts.isDottedName(node.expression)) {
+ error(node.expression, ts.Diagnostics.Assertions_require_the_call_target_to_be_an_identifier_or_qualified_name);
}
- else {
- result.push(tp);
+ else if (!getEffectsSignature(node)) {
+ var diagnostic = error(node.expression, ts.Diagnostics.Assertions_require_every_name_in_the_call_target_to_be_declared_with_an_explicit_type_annotation);
+ getTypeOfDottedName(node.expression, diagnostic);
}
}
- if (newTypeParameters) {
- var mapper = createTypeMapper(oldTypeParameters, newTypeParameters);
- for (var _a = 0, newTypeParameters_1 = newTypeParameters; _a < newTypeParameters_1.length; _a++) {
- var tp = newTypeParameters_1[_a];
- tp.mapper = mapper;
+ if (ts.isInJSFile(node)) {
+ var jsSymbol = getSymbolOfExpando(node, /*allowDeclaration*/ false);
+ if ((_a = jsSymbol === null || jsSymbol === void 0 ? void 0 : jsSymbol.exports) === null || _a === void 0 ? void 0 : _a.size) {
+ var jsAssignmentType = createAnonymousType(jsSymbol, jsSymbol.exports, ts.emptyArray, ts.emptyArray, undefined, undefined);
+ jsAssignmentType.objectFlags |= 16384 /* JSLiteral */;
+ return getIntersectionType([returnType, jsAssignmentType]);
}
}
- return result;
- }
- function hasTypeParameterByName(typeParameters, name) {
- return ts.some(typeParameters, function (tp) { return tp.symbol.escapedName === name; });
+ return returnType;
}
- function getUniqueTypeParameterName(typeParameters, baseName) {
- var len = baseName.length;
- while (len > 1 && baseName.charCodeAt(len - 1) >= 48 /* _0 */ && baseName.charCodeAt(len - 1) <= 57 /* _9 */)
- len--;
- var s = baseName.slice(0, len);
- for (var index = 1; true; index++) {
- var augmentedName = (s + index);
- if (!hasTypeParameterByName(typeParameters, augmentedName)) {
- return augmentedName;
- }
+ function checkDeprecatedSignature(signature, node) {
+ if (signature.declaration && signature.declaration.flags & 134217728 /* Deprecated */) {
+ var suggestionNode = getDeprecatedSuggestionNode(node);
+ errorOrSuggestion(/*isError*/ false, suggestionNode, ts.Diagnostics._0_is_deprecated, signatureToString(signature));
}
}
- function getReturnTypeOfSingleNonGenericCallSignature(funcType) {
- var signature = getSingleCallSignature(funcType);
- if (signature && !signature.typeParameters) {
- return getReturnTypeOfSignature(signature);
+ function getDeprecatedSuggestionNode(node) {
+ node = ts.skipParentheses(node);
+ switch (node.kind) {
+ case 203 /* CallExpression */:
+ case 161 /* Decorator */:
+ case 204 /* NewExpression */:
+ return getDeprecatedSuggestionNode(node.expression);
+ case 205 /* TaggedTemplateExpression */:
+ return getDeprecatedSuggestionNode(node.tag);
+ case 275 /* JsxOpeningElement */:
+ case 274 /* JsxSelfClosingElement */:
+ return getDeprecatedSuggestionNode(node.tagName);
+ case 202 /* ElementAccessExpression */:
+ return node.argumentExpression;
+ case 201 /* PropertyAccessExpression */:
+ return node.name;
+ case 173 /* TypeReference */:
+ var typeReference = node;
+ return ts.isQualifiedName(typeReference.typeName) ? typeReference.typeName.right : typeReference;
+ default:
+ return node;
}
}
- function getReturnTypeOfSingleNonGenericSignatureOfCallChain(expr) {
- var funcType = checkExpression(expr.expression);
- var nonOptionalType = getOptionalExpressionType(funcType, expr.expression);
- var returnType = getReturnTypeOfSingleNonGenericCallSignature(funcType);
- return returnType && propagateOptionalTypeMarker(returnType, expr, nonOptionalType !== funcType);
- }
- /**
- * Returns the type of an expression. Unlike checkExpression, this function is simply concerned
- * with computing the type and may not fully check all contained sub-expressions for errors.
- */
- function getTypeOfExpression(node) {
- // Don't bother caching types that require no flow analysis and are quick to compute.
- var quickType = getQuickTypeOfExpression(node);
- if (quickType) {
- return quickType;
+ function isSymbolOrSymbolForCall(node) {
+ if (!ts.isCallExpression(node))
+ return false;
+ var left = node.expression;
+ if (ts.isPropertyAccessExpression(left) && left.name.escapedText === "for") {
+ left = left.expression;
}
- // If a type has been cached for the node, return it.
- if (node.flags & 67108864 /* TypeCached */ && flowTypeCache) {
- var cachedType = flowTypeCache[getNodeId(node)];
- if (cachedType) {
- return cachedType;
- }
+ if (!ts.isIdentifier(left) || left.escapedText !== "Symbol") {
+ return false;
}
- var startInvocationCount = flowInvocationCount;
- var type = checkExpression(node);
- // If control flow analysis was required to determine the type, it is worth caching.
- if (flowInvocationCount !== startInvocationCount) {
- var cache = flowTypeCache || (flowTypeCache = []);
- cache[getNodeId(node)] = type;
- node.flags |= 67108864 /* TypeCached */;
+ // make sure `Symbol` is the global symbol
+ var globalESSymbol = getGlobalESSymbolConstructorSymbol(/*reportErrors*/ false);
+ if (!globalESSymbol) {
+ return false;
}
- return type;
+ return globalESSymbol === resolveName(left, "Symbol", 111551 /* Value */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ false);
}
- function getQuickTypeOfExpression(node) {
- var expr = ts.skipParentheses(node);
- // Optimize for the common case of a call to a function with a single non-generic call
- // signature where we can just fetch the return type without checking the arguments.
- if (ts.isCallExpression(expr) && expr.expression.kind !== 102 /* SuperKeyword */ && !ts.isRequireCall(expr, /*checkArgumentIsStringLiteralLike*/ true) && !isSymbolOrSymbolForCall(expr)) {
- var type = ts.isCallChain(expr) ? getReturnTypeOfSingleNonGenericSignatureOfCallChain(expr) :
- getReturnTypeOfSingleNonGenericCallSignature(checkNonNullExpression(expr.expression));
- if (type) {
- return type;
- }
- }
- else if (ts.isAssertionExpression(expr) && !ts.isConstTypeReference(expr.type)) {
- return getTypeFromTypeNode(expr.type);
- }
- else if (node.kind === 8 /* NumericLiteral */ || node.kind === 10 /* StringLiteral */ ||
- node.kind === 106 /* TrueKeyword */ || node.kind === 91 /* FalseKeyword */) {
- return checkExpression(node);
+ function checkImportCallExpression(node) {
+ // Check grammar of dynamic import
+ if (!checkGrammarArguments(node.arguments))
+ checkGrammarImportCallExpression(node);
+ if (node.arguments.length === 0) {
+ return createPromiseReturnType(node, anyType);
}
- return undefined;
- }
- /**
- * Returns the type of an expression. Unlike checkExpression, this function is simply concerned
- * with computing the type and may not fully check all contained sub-expressions for errors.
- * It is intended for uses where you know there is no contextual type,
- * and requesting the contextual type might cause a circularity or other bad behaviour.
- * It sets the contextual type of the node to any before calling getTypeOfExpression.
- */
- function getContextFreeTypeOfExpression(node) {
- var links = getNodeLinks(node);
- if (links.contextFreeType) {
- return links.contextFreeType;
+ var specifier = node.arguments[0];
+ var specifierType = checkExpressionCached(specifier);
+ // Even though multiple arguments is grammatically incorrect, type-check extra arguments for completion
+ for (var i = 1; i < node.arguments.length; ++i) {
+ checkExpressionCached(node.arguments[i]);
}
- var saveContextualType = node.contextualType;
- node.contextualType = anyType;
- try {
- var type = links.contextFreeType = checkExpression(node, 4 /* SkipContextSensitive */);
- return type;
+ if (specifierType.flags & 32768 /* Undefined */ || specifierType.flags & 65536 /* Null */ || !isTypeAssignableTo(specifierType, stringType)) {
+ error(specifier, ts.Diagnostics.Dynamic_import_s_specifier_must_be_of_type_string_but_here_has_type_0, typeToString(specifierType));
}
- finally {
- // In the event our operation is canceled or some other exception occurs, reset the contextual type
- // so that we do not accidentally hold onto an instance of the checker, as a Type created in the services layer
- // may hold onto the checker that created it.
- node.contextualType = saveContextualType;
+ // resolveExternalModuleName will return undefined if the moduleReferenceExpression is not a string literal
+ var moduleSymbol = resolveExternalModuleName(node, specifier);
+ if (moduleSymbol) {
+ var esModuleSymbol = resolveESModuleSymbol(moduleSymbol, specifier, /*dontRecursivelyResolve*/ true, /*suppressUsageError*/ false);
+ if (esModuleSymbol) {
+ return createPromiseReturnType(node, getTypeWithSyntheticDefaultImportType(getTypeOfSymbol(esModuleSymbol), esModuleSymbol, moduleSymbol));
+ }
}
+ return createPromiseReturnType(node, anyType);
}
- function checkExpression(node, checkMode, forceTuple) {
- var saveCurrentNode = currentNode;
- currentNode = node;
- instantiationCount = 0;
- var uninstantiatedType = checkExpressionWorker(node, checkMode, forceTuple);
- var type = instantiateTypeWithSingleGenericCallSignature(node, uninstantiatedType, checkMode);
- if (isConstEnumObjectType(type)) {
- checkConstEnumAccess(node, type);
+ function getTypeWithSyntheticDefaultImportType(type, symbol, originalSymbol) {
+ if (allowSyntheticDefaultImports && type && type !== errorType) {
+ var synthType = type;
+ if (!synthType.syntheticType) {
+ var file = ts.find(originalSymbol.declarations, ts.isSourceFile);
+ var hasSyntheticDefault = canHaveSyntheticDefault(file, originalSymbol, /*dontResolveAlias*/ false);
+ if (hasSyntheticDefault) {
+ var memberTable = ts.createSymbolTable();
+ var newSymbol = createSymbol(2097152 /* Alias */, "default" /* Default */);
+ newSymbol.parent = originalSymbol;
+ newSymbol.nameType = getLiteralType("default");
+ newSymbol.target = resolveSymbol(symbol);
+ memberTable.set("default" /* Default */, newSymbol);
+ var anonymousSymbol = createSymbol(2048 /* TypeLiteral */, "__type" /* Type */);
+ var defaultContainingObject = createAnonymousType(anonymousSymbol, memberTable, ts.emptyArray, ts.emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined);
+ anonymousSymbol.type = defaultContainingObject;
+ synthType.syntheticType = isValidSpreadType(type) ? getSpreadType(type, defaultContainingObject, anonymousSymbol, /*objectFlags*/ 0, /*readonly*/ false) : defaultContainingObject;
+ }
+ else {
+ synthType.syntheticType = type;
+ }
+ }
+ return synthType.syntheticType;
}
- currentNode = saveCurrentNode;
return type;
}
- function checkConstEnumAccess(node, type) {
- // enum object type for const enums are only permitted in:
- // - 'left' in property access
- // - 'object' in indexed access
- // - target in rhs of import statement
- var ok = (node.parent.kind === 194 /* PropertyAccessExpression */ && node.parent.expression === node) ||
- (node.parent.kind === 195 /* ElementAccessExpression */ && node.parent.expression === node) ||
- ((node.kind === 75 /* Identifier */ || node.kind === 153 /* QualifiedName */) && isInRightSideOfImportOrExportAssignment(node) ||
- (node.parent.kind === 172 /* TypeQuery */ && node.parent.exprName === node)) ||
- (node.parent.kind === 263 /* ExportSpecifier */); // We allow reexporting const enums
- if (!ok) {
- error(node, ts.Diagnostics.const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment_or_type_query);
+ function isCommonJsRequire(node) {
+ if (!ts.isRequireCall(node, /*checkArgumentIsStringLiteralLike*/ true)) {
+ return false;
}
- if (compilerOptions.isolatedModules) {
- ts.Debug.assert(!!(type.symbol.flags & 128 /* ConstEnum */));
- var constEnumDeclaration = type.symbol.valueDeclaration;
- if (constEnumDeclaration.flags & 8388608 /* Ambient */) {
- error(node, ts.Diagnostics.Cannot_access_ambient_const_enums_when_the_isolatedModules_flag_is_provided);
- }
+ // Make sure require is not a local function
+ if (!ts.isIdentifier(node.expression))
+ return ts.Debug.fail();
+ var resolvedRequire = resolveName(node.expression, node.expression.escapedText, 111551 /* Value */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ true); // TODO: GH#18217
+ if (resolvedRequire === requireSymbol) {
+ return true;
}
- }
- function checkParenthesizedExpression(node, checkMode) {
- var tag = ts.isInJSFile(node) ? ts.getJSDocTypeTag(node) : undefined;
- if (tag) {
- return checkAssertionWorker(tag, tag.typeExpression.type, node.expression, checkMode);
+ // project includes symbol named 'require' - make sure that it is ambient and local non-alias
+ if (resolvedRequire.flags & 2097152 /* Alias */) {
+ return false;
}
- return checkExpression(node.expression, checkMode);
+ var targetDeclarationKind = resolvedRequire.flags & 16 /* Function */
+ ? 251 /* FunctionDeclaration */
+ : resolvedRequire.flags & 3 /* Variable */
+ ? 249 /* VariableDeclaration */
+ : 0 /* Unknown */;
+ if (targetDeclarationKind !== 0 /* Unknown */) {
+ var decl = ts.getDeclarationOfKind(resolvedRequire, targetDeclarationKind);
+ // function/variable declaration should be ambient
+ return !!decl && !!(decl.flags & 8388608 /* Ambient */);
+ }
+ return false;
}
- function checkExpressionWorker(node, checkMode, forceTuple) {
- var kind = node.kind;
- if (cancellationToken) {
- // Only bother checking on a few construct kinds. We don't want to be excessively
- // hitting the cancellation token on every node we check.
- switch (kind) {
- case 214 /* ClassExpression */:
- case 201 /* FunctionExpression */:
- case 202 /* ArrowFunction */:
- cancellationToken.throwIfCancellationRequested();
- }
+ function checkTaggedTemplateExpression(node) {
+ if (!checkGrammarTaggedTemplateChain(node))
+ checkGrammarTypeArguments(node, node.typeArguments);
+ if (languageVersion < 2 /* ES2015 */) {
+ checkExternalEmitHelpers(node, 524288 /* MakeTemplateObject */);
}
- switch (kind) {
- case 75 /* Identifier */:
- return checkIdentifier(node);
- case 104 /* ThisKeyword */:
- return checkThisExpression(node);
- case 102 /* SuperKeyword */:
- return checkSuperExpression(node);
- case 100 /* NullKeyword */:
- return nullWideningType;
- case 14 /* NoSubstitutionTemplateLiteral */:
+ var signature = getResolvedSignature(node);
+ checkDeprecatedSignature(signature, node);
+ return getReturnTypeOfSignature(signature);
+ }
+ function checkAssertion(node) {
+ return checkAssertionWorker(node, node.type, node.expression);
+ }
+ function isValidConstAssertionArgument(node) {
+ switch (node.kind) {
case 10 /* StringLiteral */:
- return getFreshTypeOfLiteralType(getLiteralType(node.text));
+ case 14 /* NoSubstitutionTemplateLiteral */:
case 8 /* NumericLiteral */:
- checkGrammarNumericLiteral(node);
- return getFreshTypeOfLiteralType(getLiteralType(+node.text));
case 9 /* BigIntLiteral */:
- checkGrammarBigIntLiteral(node);
- return getFreshTypeOfLiteralType(getBigIntLiteralType(node));
- case 106 /* TrueKeyword */:
- return trueType;
- case 91 /* FalseKeyword */:
- return falseType;
- case 211 /* TemplateExpression */:
- return checkTemplateExpression(node);
- case 13 /* RegularExpressionLiteral */:
- return globalRegExpType;
- case 192 /* ArrayLiteralExpression */:
- return checkArrayLiteral(node, checkMode, forceTuple);
- case 193 /* ObjectLiteralExpression */:
- return checkObjectLiteral(node, checkMode);
- case 194 /* PropertyAccessExpression */:
- return checkPropertyAccessExpression(node);
- case 153 /* QualifiedName */:
- return checkQualifiedName(node);
- case 195 /* ElementAccessExpression */:
- return checkIndexedAccess(node);
- case 196 /* CallExpression */:
- if (node.expression.kind === 96 /* ImportKeyword */) {
- return checkImportCallExpression(node);
+ case 109 /* TrueKeyword */:
+ case 94 /* FalseKeyword */:
+ case 199 /* ArrayLiteralExpression */:
+ case 200 /* ObjectLiteralExpression */:
+ case 218 /* TemplateExpression */:
+ return true;
+ case 207 /* ParenthesizedExpression */:
+ return isValidConstAssertionArgument(node.expression);
+ case 214 /* PrefixUnaryExpression */:
+ var op = node.operator;
+ var arg = node.operand;
+ return op === 40 /* MinusToken */ && (arg.kind === 8 /* NumericLiteral */ || arg.kind === 9 /* BigIntLiteral */) ||
+ op === 39 /* PlusToken */ && arg.kind === 8 /* NumericLiteral */;
+ case 201 /* PropertyAccessExpression */:
+ case 202 /* ElementAccessExpression */:
+ var expr = node.expression;
+ if (ts.isIdentifier(expr)) {
+ var symbol = getSymbolAtLocation(expr);
+ if (symbol && symbol.flags & 2097152 /* Alias */) {
+ symbol = resolveAlias(symbol);
+ }
+ return !!(symbol && (symbol.flags & 384 /* Enum */) && getEnumKind(symbol) === 1 /* Literal */);
}
- // falls through
- case 197 /* NewExpression */:
- return checkCallExpression(node, checkMode);
- case 198 /* TaggedTemplateExpression */:
- return checkTaggedTemplateExpression(node);
- case 200 /* ParenthesizedExpression */:
- return checkParenthesizedExpression(node, checkMode);
- case 214 /* ClassExpression */:
- return checkClassExpression(node);
- case 201 /* FunctionExpression */:
- case 202 /* ArrowFunction */:
- return checkFunctionExpressionOrObjectLiteralMethod(node, checkMode);
- case 204 /* TypeOfExpression */:
- return checkTypeOfExpression(node);
- case 199 /* TypeAssertionExpression */:
- case 217 /* AsExpression */:
- return checkAssertion(node);
- case 218 /* NonNullExpression */:
- return checkNonNullAssertion(node);
- case 219 /* MetaProperty */:
- return checkMetaProperty(node);
- case 203 /* DeleteExpression */:
- return checkDeleteExpression(node);
- case 205 /* VoidExpression */:
- return checkVoidExpression(node);
- case 206 /* AwaitExpression */:
- return checkAwaitExpression(node);
- case 207 /* PrefixUnaryExpression */:
- return checkPrefixUnaryExpression(node);
- case 208 /* PostfixUnaryExpression */:
- return checkPostfixUnaryExpression(node);
- case 209 /* BinaryExpression */:
- return checkBinaryExpression(node, checkMode);
- case 210 /* ConditionalExpression */:
- return checkConditionalExpression(node, checkMode);
- case 213 /* SpreadElement */:
- return checkSpreadExpression(node, checkMode);
- case 215 /* OmittedExpression */:
- return undefinedWideningType;
- case 212 /* YieldExpression */:
- return checkYieldExpression(node);
- case 220 /* SyntheticExpression */:
- return node.type;
- case 276 /* JsxExpression */:
- return checkJsxExpression(node, checkMode);
- case 266 /* JsxElement */:
- return checkJsxElement(node, checkMode);
- case 267 /* JsxSelfClosingElement */:
- return checkJsxSelfClosingElement(node, checkMode);
- case 270 /* JsxFragment */:
- return checkJsxFragment(node);
- case 274 /* JsxAttributes */:
- return checkJsxAttributes(node, checkMode);
- case 268 /* JsxOpeningElement */:
- ts.Debug.fail("Shouldn't ever directly check a JsxOpeningElement");
- }
- return errorType;
- }
- // DECLARATION AND STATEMENT TYPE CHECKING
- function checkTypeParameter(node) {
- // Grammar Checking
- if (node.expression) {
- grammarErrorOnFirstToken(node.expression, ts.Diagnostics.Type_expected);
- }
- checkSourceElement(node.constraint);
- checkSourceElement(node.default);
- var typeParameter = getDeclaredTypeOfTypeParameter(getSymbolOfNode(node));
- // Resolve base constraint to reveal circularity errors
- getBaseConstraintOfType(typeParameter);
- if (!hasNonCircularTypeParameterDefault(typeParameter)) {
- error(node.default, ts.Diagnostics.Type_parameter_0_has_a_circular_default, typeToString(typeParameter));
- }
- var constraintType = getConstraintOfTypeParameter(typeParameter);
- var defaultType = getDefaultFromTypeParameter(typeParameter);
- if (constraintType && defaultType) {
- checkTypeAssignableTo(defaultType, getTypeWithThisArgument(instantiateType(constraintType, makeUnaryTypeMapper(typeParameter, defaultType)), defaultType), node.default, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1);
- }
- if (produceDiagnostics) {
- checkTypeNameIsReserved(node.name, ts.Diagnostics.Type_parameter_name_cannot_be_0);
}
+ return false;
}
- function checkParameter(node) {
- // Grammar checking
- // It is a SyntaxError if the Identifier "eval" or the Identifier "arguments" occurs as the
- // Identifier in a PropertySetParameterList of a PropertyAssignment that is contained in strict code
- // or if its FunctionBody is strict code(11.1.5).
- checkGrammarDecoratorsAndModifiers(node);
- checkVariableLikeDeclaration(node);
- var func = ts.getContainingFunction(node);
- if (ts.hasModifier(node, 92 /* ParameterPropertyModifier */)) {
- if (!(func.kind === 162 /* Constructor */ && ts.nodeIsPresent(func.body))) {
- error(node, ts.Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation);
- }
- if (func.kind === 162 /* Constructor */ && ts.isIdentifier(node.name) && node.name.escapedText === "constructor") {
- error(node.name, ts.Diagnostics.constructor_cannot_be_used_as_a_parameter_property_name);
+ function checkAssertionWorker(errNode, type, expression, checkMode) {
+ var exprType = checkExpression(expression, checkMode);
+ if (ts.isConstTypeReference(type)) {
+ if (!isValidConstAssertionArgument(expression)) {
+ error(expression, ts.Diagnostics.A_const_assertions_can_only_be_applied_to_references_to_enum_members_or_string_number_boolean_array_or_object_literals);
}
+ return getRegularTypeOfLiteralType(exprType);
}
- if (node.questionToken && ts.isBindingPattern(node.name) && func.body) {
- error(node, ts.Diagnostics.A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature);
- }
- if (node.name && ts.isIdentifier(node.name) && (node.name.escapedText === "this" || node.name.escapedText === "new")) {
- if (func.parameters.indexOf(node) !== 0) {
- error(node, ts.Diagnostics.A_0_parameter_must_be_the_first_parameter, node.name.escapedText);
- }
- if (func.kind === 162 /* Constructor */ || func.kind === 166 /* ConstructSignature */ || func.kind === 171 /* ConstructorType */) {
- error(node, ts.Diagnostics.A_constructor_cannot_have_a_this_parameter);
- }
- if (func.kind === 202 /* ArrowFunction */) {
- error(node, ts.Diagnostics.An_arrow_function_cannot_have_a_this_parameter);
- }
- if (func.kind === 163 /* GetAccessor */ || func.kind === 164 /* SetAccessor */) {
- error(node, ts.Diagnostics.get_and_set_accessors_cannot_declare_this_parameters);
+ checkSourceElement(type);
+ exprType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(exprType));
+ var targetType = getTypeFromTypeNode(type);
+ if (produceDiagnostics && targetType !== errorType) {
+ var widenedType = getWidenedType(exprType);
+ if (!isTypeComparableTo(targetType, widenedType)) {
+ checkTypeComparableTo(exprType, targetType, errNode, ts.Diagnostics.Conversion_of_type_0_to_type_1_may_be_a_mistake_because_neither_type_sufficiently_overlaps_with_the_other_If_this_was_intentional_convert_the_expression_to_unknown_first);
}
}
- // Only check rest parameter type if it's not a binding pattern. Since binding patterns are
- // not allowed in a rest parameter, we already have an error from checkGrammarParameterList.
- if (node.dotDotDotToken && !ts.isBindingPattern(node.name) && !isTypeAssignableTo(getReducedType(getTypeOfSymbol(node.symbol)), anyReadonlyArrayType)) {
- error(node, ts.Diagnostics.A_rest_parameter_must_be_of_an_array_type);
- }
+ return targetType;
}
- function checkTypePredicate(node) {
- var parent = getTypePredicateParent(node);
- if (!parent) {
- // The parent must not be valid.
- error(node, ts.Diagnostics.A_type_predicate_is_only_allowed_in_return_type_position_for_functions_and_methods);
- return;
+ function checkNonNullChain(node) {
+ var leftType = checkExpression(node.expression);
+ var nonOptionalType = getOptionalExpressionType(leftType, node.expression);
+ return propagateOptionalTypeMarker(getNonNullableType(nonOptionalType), node, nonOptionalType !== leftType);
+ }
+ function checkNonNullAssertion(node) {
+ return node.flags & 32 /* OptionalChain */ ? checkNonNullChain(node) :
+ getNonNullableType(checkExpression(node.expression));
+ }
+ function checkMetaProperty(node) {
+ checkGrammarMetaProperty(node);
+ if (node.keywordToken === 102 /* NewKeyword */) {
+ return checkNewTargetMetaProperty(node);
}
- var signature = getSignatureFromDeclaration(parent);
- var typePredicate = getTypePredicateOfSignature(signature);
- if (!typePredicate) {
- return;
+ if (node.keywordToken === 99 /* ImportKeyword */) {
+ return checkImportMetaProperty(node);
}
- checkSourceElement(node.type);
- var parameterName = node.parameterName;
- if (typePredicate.kind === 0 /* This */ || typePredicate.kind === 2 /* AssertsThis */) {
- getTypeFromThisTypeNode(parameterName);
+ return ts.Debug.assertNever(node.keywordToken);
+ }
+ function checkNewTargetMetaProperty(node) {
+ var container = ts.getNewTargetContainer(node);
+ if (!container) {
+ error(node, ts.Diagnostics.Meta_property_0_is_only_allowed_in_the_body_of_a_function_declaration_function_expression_or_constructor, "new.target");
+ return errorType;
+ }
+ else if (container.kind === 166 /* Constructor */) {
+ var symbol = getSymbolOfNode(container.parent);
+ return getTypeOfSymbol(symbol);
}
else {
- if (typePredicate.parameterIndex >= 0) {
- if (signatureHasRestParameter(signature) && typePredicate.parameterIndex === signature.parameters.length - 1) {
- error(parameterName, ts.Diagnostics.A_type_predicate_cannot_reference_a_rest_parameter);
- }
- else {
- if (typePredicate.type) {
- var leadingError = function () { return ts.chainDiagnosticMessages(/*details*/ undefined, ts.Diagnostics.A_type_predicate_s_type_must_be_assignable_to_its_parameter_s_type); };
- checkTypeAssignableTo(typePredicate.type, getTypeOfSymbol(signature.parameters[typePredicate.parameterIndex]), node.type,
- /*headMessage*/ undefined, leadingError);
- }
- }
- }
- else if (parameterName) {
- var hasReportedError = false;
- for (var _i = 0, _a = parent.parameters; _i < _a.length; _i++) {
- var name = _a[_i].name;
- if (ts.isBindingPattern(name) &&
- checkIfTypePredicateVariableIsDeclaredInBindingPattern(name, parameterName, typePredicate.parameterName)) {
- hasReportedError = true;
- break;
- }
- }
- if (!hasReportedError) {
- error(node.parameterName, ts.Diagnostics.Cannot_find_parameter_0, typePredicate.parameterName);
- }
- }
+ var symbol = getSymbolOfNode(container);
+ return getTypeOfSymbol(symbol);
}
}
- function getTypePredicateParent(node) {
- switch (node.parent.kind) {
- case 202 /* ArrowFunction */:
- case 165 /* CallSignature */:
- case 244 /* FunctionDeclaration */:
- case 201 /* FunctionExpression */:
- case 170 /* FunctionType */:
- case 161 /* MethodDeclaration */:
- case 160 /* MethodSignature */:
- var parent = node.parent;
- if (node === parent.type) {
- return parent;
- }
+ function checkImportMetaProperty(node) {
+ if (moduleKind !== ts.ModuleKind.ES2020 && moduleKind !== ts.ModuleKind.ESNext && moduleKind !== ts.ModuleKind.System) {
+ error(node, ts.Diagnostics.The_import_meta_meta_property_is_only_allowed_when_the_module_option_is_es2020_esnext_or_system);
}
+ var file = ts.getSourceFileOfNode(node);
+ ts.Debug.assert(!!(file.flags & 2097152 /* PossiblyContainsImportMeta */), "Containing file is missing import meta node flag.");
+ ts.Debug.assert(!!file.externalModuleIndicator, "Containing file should be a module.");
+ return node.name.escapedText === "meta" ? getGlobalImportMetaType() : errorType;
}
- function checkIfTypePredicateVariableIsDeclaredInBindingPattern(pattern, predicateVariableNode, predicateVariableName) {
- for (var _i = 0, _a = pattern.elements; _i < _a.length; _i++) {
- var element = _a[_i];
- if (ts.isOmittedExpression(element)) {
- continue;
- }
- var name = element.name;
- if (name.kind === 75 /* Identifier */ && name.escapedText === predicateVariableName) {
- error(predicateVariableNode, ts.Diagnostics.A_type_predicate_cannot_reference_element_0_in_a_binding_pattern, predicateVariableName);
- return true;
- }
- else if (name.kind === 190 /* ArrayBindingPattern */ || name.kind === 189 /* ObjectBindingPattern */) {
- if (checkIfTypePredicateVariableIsDeclaredInBindingPattern(name, predicateVariableNode, predicateVariableName)) {
- return true;
- }
+ function getTypeOfParameter(symbol) {
+ var type = getTypeOfSymbol(symbol);
+ if (strictNullChecks) {
+ var declaration = symbol.valueDeclaration;
+ if (declaration && ts.hasInitializer(declaration)) {
+ return getOptionalType(type);
}
}
+ return type;
}
- function checkSignatureDeclaration(node) {
- // Grammar checking
- if (node.kind === 167 /* IndexSignature */) {
- checkGrammarIndexSignature(node);
+ function getTupleElementLabel(d) {
+ ts.Debug.assert(ts.isIdentifier(d.name)); // Parameter declarations could be binding patterns, but we only allow identifier names
+ return d.name.escapedText;
+ }
+ function getParameterNameAtPosition(signature, pos, overrideRestType) {
+ var paramCount = signature.parameters.length - (signatureHasRestParameter(signature) ? 1 : 0);
+ if (pos < paramCount) {
+ return signature.parameters[pos].escapedName;
}
- // TODO (yuisu): Remove this check in else-if when SyntaxKind.Construct is moved and ambient context is handled
- else if (node.kind === 170 /* FunctionType */ || node.kind === 244 /* FunctionDeclaration */ || node.kind === 171 /* ConstructorType */ ||
- node.kind === 165 /* CallSignature */ || node.kind === 162 /* Constructor */ ||
- node.kind === 166 /* ConstructSignature */) {
- checkGrammarFunctionLikeDeclaration(node);
+ var restParameter = signature.parameters[paramCount] || unknownSymbol;
+ var restType = overrideRestType || getTypeOfSymbol(restParameter);
+ if (isTupleType(restType)) {
+ var associatedNames = restType.target.labeledElementDeclarations;
+ var index = pos - paramCount;
+ return associatedNames && getTupleElementLabel(associatedNames[index]) || restParameter.escapedName + "_" + index;
}
- var functionFlags = ts.getFunctionFlags(node);
- if (!(functionFlags & 4 /* Invalid */)) {
- // Async generators prior to ESNext require the __await and __asyncGenerator helpers
- if ((functionFlags & 3 /* AsyncGenerator */) === 3 /* AsyncGenerator */ && languageVersion < 99 /* ESNext */) {
- checkExternalEmitHelpers(node, 12288 /* AsyncGeneratorIncludes */);
- }
- // Async functions prior to ES2017 require the __awaiter helper
- if ((functionFlags & 3 /* AsyncGenerator */) === 2 /* Async */ && languageVersion < 4 /* ES2017 */) {
- checkExternalEmitHelpers(node, 64 /* Awaiter */);
- }
- // Generator functions, Async functions, and Async Generator functions prior to
- // ES2015 require the __generator helper
- if ((functionFlags & 3 /* AsyncGenerator */) !== 0 /* Normal */ && languageVersion < 2 /* ES2015 */) {
- checkExternalEmitHelpers(node, 128 /* Generator */);
- }
+ return restParameter.escapedName;
+ }
+ function isValidDeclarationForTupleLabel(d) {
+ return d.kind === 192 /* NamedTupleMember */ || (ts.isParameter(d) && d.name && ts.isIdentifier(d.name));
+ }
+ function getNameableDeclarationAtPosition(signature, pos) {
+ var paramCount = signature.parameters.length - (signatureHasRestParameter(signature) ? 1 : 0);
+ if (pos < paramCount) {
+ var decl = signature.parameters[pos].valueDeclaration;
+ return decl && isValidDeclarationForTupleLabel(decl) ? decl : undefined;
}
- checkTypeParameters(node.typeParameters);
- ts.forEach(node.parameters, checkParameter);
- // TODO(rbuckton): Should we start checking JSDoc types?
- if (node.type) {
- checkSourceElement(node.type);
+ var restParameter = signature.parameters[paramCount] || unknownSymbol;
+ var restType = getTypeOfSymbol(restParameter);
+ if (isTupleType(restType)) {
+ var associatedNames = restType.target.labeledElementDeclarations;
+ var index = pos - paramCount;
+ return associatedNames && associatedNames[index];
}
- if (produceDiagnostics) {
- checkCollisionWithArgumentsInGeneratedCode(node);
- var returnTypeNode = ts.getEffectiveReturnTypeNode(node);
- if (noImplicitAny && !returnTypeNode) {
- switch (node.kind) {
- case 166 /* ConstructSignature */:
- error(node, ts.Diagnostics.Construct_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type);
- break;
- case 165 /* CallSignature */:
- error(node, ts.Diagnostics.Call_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type);
- break;
- }
- }
- if (returnTypeNode) {
- var functionFlags_1 = ts.getFunctionFlags(node);
- if ((functionFlags_1 & (4 /* Invalid */ | 1 /* Generator */)) === 1 /* Generator */) {
- var returnType = getTypeFromTypeNode(returnTypeNode);
- if (returnType === voidType) {
- error(returnTypeNode, ts.Diagnostics.A_generator_cannot_have_a_void_type_annotation);
- }
- else {
- // Naively, one could check that Generator is assignable to the return type annotation.
- // However, that would not catch the error in the following case.
- //
- // interface BadGenerator extends Iterable, Iterator { }
- // function* g(): BadGenerator { } // Iterable and Iterator have different types!
- //
- var generatorYieldType = getIterationTypeOfGeneratorFunctionReturnType(0 /* Yield */, returnType, (functionFlags_1 & 2 /* Async */) !== 0) || anyType;
- var generatorReturnType = getIterationTypeOfGeneratorFunctionReturnType(1 /* Return */, returnType, (functionFlags_1 & 2 /* Async */) !== 0) || generatorYieldType;
- var generatorNextType = getIterationTypeOfGeneratorFunctionReturnType(2 /* Next */, returnType, (functionFlags_1 & 2 /* Async */) !== 0) || unknownType;
- var generatorInstantiation = createGeneratorReturnType(generatorYieldType, generatorReturnType, generatorNextType, !!(functionFlags_1 & 2 /* Async */));
- checkTypeAssignableTo(generatorInstantiation, returnType, returnTypeNode);
- }
- }
- else if ((functionFlags_1 & 3 /* AsyncGenerator */) === 2 /* Async */) {
- checkAsyncFunctionReturnType(node, returnTypeNode);
- }
- }
- if (node.kind !== 167 /* IndexSignature */ && node.kind !== 300 /* JSDocFunctionType */) {
- registerForUnusedIdentifiersCheck(node);
+ return restParameter.valueDeclaration && isValidDeclarationForTupleLabel(restParameter.valueDeclaration) ? restParameter.valueDeclaration : undefined;
+ }
+ function getTypeAtPosition(signature, pos) {
+ return tryGetTypeAtPosition(signature, pos) || anyType;
+ }
+ function tryGetTypeAtPosition(signature, pos) {
+ var paramCount = signature.parameters.length - (signatureHasRestParameter(signature) ? 1 : 0);
+ if (pos < paramCount) {
+ return getTypeOfParameter(signature.parameters[pos]);
+ }
+ if (signatureHasRestParameter(signature)) {
+ // We want to return the value undefined for an out of bounds parameter position,
+ // so we need to check bounds here before calling getIndexedAccessType (which
+ // otherwise would return the type 'undefined').
+ var restType = getTypeOfSymbol(signature.parameters[paramCount]);
+ var index = pos - paramCount;
+ if (!isTupleType(restType) || restType.target.hasRestElement || index < restType.target.fixedLength) {
+ return getIndexedAccessType(restType, getLiteralType(index));
}
}
+ return undefined;
}
- function checkClassForDuplicateDeclarations(node) {
- var instanceNames = ts.createUnderscoreEscapedMap();
- var staticNames = ts.createUnderscoreEscapedMap();
- // instance and static private identifiers share the same scope
- var privateIdentifiers = ts.createUnderscoreEscapedMap();
- for (var _i = 0, _a = node.members; _i < _a.length; _i++) {
- var member = _a[_i];
- if (member.kind === 162 /* Constructor */) {
- for (var _b = 0, _c = member.parameters; _b < _c.length; _b++) {
- var param = _c[_b];
- if (ts.isParameterPropertyDeclaration(param, member) && !ts.isBindingPattern(param.name)) {
- addName(instanceNames, param.name, param.name.escapedText, 3 /* GetOrSetAccessor */);
- }
- }
- }
- else {
- var isStatic = ts.hasModifier(member, 32 /* Static */);
- var name = member.name;
- if (!name) {
- return;
- }
- var names = ts.isPrivateIdentifier(name) ? privateIdentifiers :
- isStatic ? staticNames :
- instanceNames;
- var memberName = name && ts.getPropertyNameForPropertyNameNode(name);
- if (memberName) {
- switch (member.kind) {
- case 163 /* GetAccessor */:
- addName(names, name, memberName, 1 /* GetAccessor */);
- break;
- case 164 /* SetAccessor */:
- addName(names, name, memberName, 2 /* SetAccessor */);
- break;
- case 159 /* PropertyDeclaration */:
- addName(names, name, memberName, 3 /* GetOrSetAccessor */);
- break;
- case 161 /* MethodDeclaration */:
- addName(names, name, memberName, 8 /* Method */);
- break;
- }
- }
- }
+ function getRestTypeAtPosition(source, pos) {
+ var parameterCount = getParameterCount(source);
+ var minArgumentCount = getMinArgumentCount(source);
+ var restType = getEffectiveRestType(source);
+ if (restType && pos >= parameterCount - 1) {
+ return pos === parameterCount - 1 ? restType : createArrayType(getIndexedAccessType(restType, numberType));
}
- function addName(names, location, name, meaning) {
- var prev = names.get(name);
- if (prev) {
- if (prev & 8 /* Method */) {
- if (meaning !== 8 /* Method */) {
- error(location, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(location));
- }
- }
- else if (prev & meaning) {
- error(location, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(location));
- }
- else {
- names.set(name, prev | meaning);
- }
+ var types = [];
+ var flags = [];
+ var names = [];
+ for (var i = pos; i < parameterCount; i++) {
+ if (!restType || i < parameterCount - 1) {
+ types.push(getTypeAtPosition(source, i));
+ flags.push(i < minArgumentCount ? 1 /* Required */ : 2 /* Optional */);
}
else {
- names.set(name, meaning);
+ types.push(restType);
+ flags.push(8 /* Variadic */);
+ }
+ var name = getNameableDeclarationAtPosition(source, i);
+ if (name) {
+ names.push(name);
}
}
+ return createTupleType(types, flags, /*readonly*/ false, ts.length(names) === ts.length(types) ? names : undefined);
}
- /**
- * Static members being set on a constructor function may conflict with built-in properties
- * of Function. Esp. in ECMAScript 5 there are non-configurable and non-writable
- * built-in properties. This check issues a transpile error when a class has a static
- * member with the same name as a non-writable built-in property.
- *
- * @see http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.3
- * @see http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5
- * @see http://www.ecma-international.org/ecma-262/6.0/#sec-properties-of-the-function-constructor
- * @see http://www.ecma-international.org/ecma-262/6.0/#sec-function-instances
- */
- function checkClassForStaticPropertyNameConflicts(node) {
- for (var _i = 0, _a = node.members; _i < _a.length; _i++) {
- var member = _a[_i];
- var memberNameNode = member.name;
- var isStatic = ts.hasModifier(member, 32 /* Static */);
- if (isStatic && memberNameNode) {
- var memberName = ts.getPropertyNameForPropertyNameNode(memberNameNode);
- switch (memberName) {
- case "name":
- case "length":
- case "caller":
- case "arguments":
- case "prototype":
- var message = ts.Diagnostics.Static_property_0_conflicts_with_built_in_property_Function_0_of_constructor_function_1;
- var className = getNameOfSymbolAsWritten(getSymbolOfNode(node));
- error(memberNameNode, message, memberName, className);
- break;
- }
+ function getParameterCount(signature) {
+ var length = signature.parameters.length;
+ if (signatureHasRestParameter(signature)) {
+ var restType = getTypeOfSymbol(signature.parameters[length - 1]);
+ if (isTupleType(restType)) {
+ return length + restType.target.fixedLength - (restType.target.hasRestElement ? 0 : 1);
}
}
+ return length;
}
- function checkObjectTypeForDuplicateDeclarations(node) {
- var names = ts.createMap();
- for (var _i = 0, _a = node.members; _i < _a.length; _i++) {
- var member = _a[_i];
- if (member.kind === 158 /* PropertySignature */) {
- var memberName = void 0;
- var name = member.name;
- switch (name.kind) {
- case 10 /* StringLiteral */:
- case 8 /* NumericLiteral */:
- memberName = name.text;
- break;
- case 75 /* Identifier */:
- memberName = ts.idText(name);
- break;
- default:
- continue;
+ function getMinArgumentCount(signature, flags) {
+ var strongArityForUntypedJS = flags & 1 /* StrongArityForUntypedJS */;
+ var voidIsNonOptional = flags & 2 /* VoidIsNonOptional */;
+ if (voidIsNonOptional || signature.resolvedMinArgumentCount === undefined) {
+ var minArgumentCount = void 0;
+ if (signatureHasRestParameter(signature)) {
+ var restType = getTypeOfSymbol(signature.parameters[signature.parameters.length - 1]);
+ if (isTupleType(restType)) {
+ var firstOptionalIndex = ts.findIndex(restType.target.elementFlags, function (f) { return !(f & 1 /* Required */); });
+ var requiredCount = firstOptionalIndex < 0 ? restType.target.fixedLength : firstOptionalIndex;
+ if (requiredCount > 0) {
+ minArgumentCount = signature.parameters.length - 1 + requiredCount;
+ }
}
- if (names.get(memberName)) {
- error(ts.getNameOfDeclaration(member.symbol.valueDeclaration), ts.Diagnostics.Duplicate_identifier_0, memberName);
- error(member.name, ts.Diagnostics.Duplicate_identifier_0, memberName);
+ }
+ if (minArgumentCount === undefined) {
+ if (!strongArityForUntypedJS && signature.flags & 16 /* IsUntypedSignatureInJSFile */) {
+ return 0;
}
- else {
- names.set(memberName, true);
+ minArgumentCount = signature.minArgumentCount;
+ }
+ if (voidIsNonOptional) {
+ return minArgumentCount;
+ }
+ for (var i = minArgumentCount - 1; i >= 0; i--) {
+ var type = getTypeAtPosition(signature, i);
+ if (filterType(type, acceptsVoid).flags & 131072 /* Never */) {
+ break;
}
+ minArgumentCount = i;
}
+ signature.resolvedMinArgumentCount = minArgumentCount;
}
+ return signature.resolvedMinArgumentCount;
}
- function checkTypeForDuplicateIndexSignatures(node) {
- if (node.kind === 246 /* InterfaceDeclaration */) {
- var nodeSymbol = getSymbolOfNode(node);
- // in case of merging interface declaration it is possible that we'll enter this check procedure several times for every declaration
- // to prevent this run check only for the first declaration of a given kind
- if (nodeSymbol.declarations.length > 0 && nodeSymbol.declarations[0] !== node) {
- return;
+ function hasEffectiveRestParameter(signature) {
+ if (signatureHasRestParameter(signature)) {
+ var restType = getTypeOfSymbol(signature.parameters[signature.parameters.length - 1]);
+ return !isTupleType(restType) || restType.target.hasRestElement;
+ }
+ return false;
+ }
+ function getEffectiveRestType(signature) {
+ if (signatureHasRestParameter(signature)) {
+ var restType = getTypeOfSymbol(signature.parameters[signature.parameters.length - 1]);
+ if (!isTupleType(restType)) {
+ return restType;
+ }
+ if (restType.target.hasRestElement) {
+ return sliceTupleType(restType, restType.target.fixedLength);
}
}
- // TypeScript 1.0 spec (April 2014)
- // 3.7.4: An object type can contain at most one string index signature and one numeric index signature.
- // 8.5: A class declaration can have at most one string index member declaration and one numeric index member declaration
- var indexSymbol = getIndexSymbol(getSymbolOfNode(node));
- if (indexSymbol) {
- var seenNumericIndexer = false;
- var seenStringIndexer = false;
- for (var _i = 0, _a = indexSymbol.declarations; _i < _a.length; _i++) {
- var decl = _a[_i];
- var declaration = decl;
- if (declaration.parameters.length === 1 && declaration.parameters[0].type) {
- switch (declaration.parameters[0].type.kind) {
- case 143 /* StringKeyword */:
- if (!seenStringIndexer) {
- seenStringIndexer = true;
- }
- else {
- error(declaration, ts.Diagnostics.Duplicate_string_index_signature);
- }
- break;
- case 140 /* NumberKeyword */:
- if (!seenNumericIndexer) {
- seenNumericIndexer = true;
- }
- else {
- error(declaration, ts.Diagnostics.Duplicate_number_index_signature);
- }
- break;
- }
+ return undefined;
+ }
+ function getNonArrayRestType(signature) {
+ var restType = getEffectiveRestType(signature);
+ return restType && !isArrayType(restType) && !isTypeAny(restType) && (getReducedType(restType).flags & 131072 /* Never */) === 0 ? restType : undefined;
+ }
+ function getTypeOfFirstParameterOfSignature(signature) {
+ return getTypeOfFirstParameterOfSignatureWithFallback(signature, neverType);
+ }
+ function getTypeOfFirstParameterOfSignatureWithFallback(signature, fallbackType) {
+ return signature.parameters.length > 0 ? getTypeAtPosition(signature, 0) : fallbackType;
+ }
+ function inferFromAnnotatedParameters(signature, context, inferenceContext) {
+ var len = signature.parameters.length - (signatureHasRestParameter(signature) ? 1 : 0);
+ for (var i = 0; i < len; i++) {
+ var declaration = signature.parameters[i].valueDeclaration;
+ if (declaration.type) {
+ var typeNode = ts.getEffectiveTypeAnnotationNode(declaration);
+ if (typeNode) {
+ inferTypes(inferenceContext.inferences, getTypeFromTypeNode(typeNode), getTypeAtPosition(context, i));
}
}
}
+ var restType = getEffectiveRestType(context);
+ if (restType && restType.flags & 262144 /* TypeParameter */) {
+ // The contextual signature has a generic rest parameter. We first instantiate the contextual
+ // signature (without fixing type parameters) and assign types to contextually typed parameters.
+ var instantiatedContext = instantiateSignature(context, inferenceContext.nonFixingMapper);
+ assignContextualParameterTypes(signature, instantiatedContext);
+ // We then infer from a tuple type representing the parameters that correspond to the contextual
+ // rest parameter.
+ var restPos = getParameterCount(context) - 1;
+ inferTypes(inferenceContext.inferences, getRestTypeAtPosition(signature, restPos), restType);
+ }
}
- function checkPropertyDeclaration(node) {
- // Grammar checking
- if (!checkGrammarDecoratorsAndModifiers(node) && !checkGrammarProperty(node))
- checkGrammarComputedPropertyName(node.name);
- checkVariableLikeDeclaration(node);
- // Private class fields transformation relies on WeakMaps.
- if (ts.isPrivateIdentifier(node.name) && languageVersion < 99 /* ESNext */) {
- for (var lexicalScope = ts.getEnclosingBlockScopeContainer(node); !!lexicalScope; lexicalScope = ts.getEnclosingBlockScopeContainer(lexicalScope)) {
- getNodeLinks(lexicalScope).flags |= 67108864 /* ContainsClassWithPrivateIdentifiers */;
+ function assignContextualParameterTypes(signature, context) {
+ signature.typeParameters = context.typeParameters;
+ if (context.thisParameter) {
+ var parameter = signature.thisParameter;
+ if (!parameter || parameter.valueDeclaration && !parameter.valueDeclaration.type) {
+ if (!parameter) {
+ signature.thisParameter = createSymbolWithType(context.thisParameter, /*type*/ undefined);
+ }
+ assignParameterType(signature.thisParameter, getTypeOfSymbol(context.thisParameter));
+ }
+ }
+ var len = signature.parameters.length - (signatureHasRestParameter(signature) ? 1 : 0);
+ for (var i = 0; i < len; i++) {
+ var parameter = signature.parameters[i];
+ if (!ts.getEffectiveTypeAnnotationNode(parameter.valueDeclaration)) {
+ var contextualParameterType = tryGetTypeAtPosition(context, i);
+ assignParameterType(parameter, contextualParameterType);
+ }
+ }
+ if (signatureHasRestParameter(signature)) {
+ // parameter might be a transient symbol generated by use of `arguments` in the function body.
+ var parameter = ts.last(signature.parameters);
+ if (ts.isTransientSymbol(parameter) || !ts.getEffectiveTypeAnnotationNode(parameter.valueDeclaration)) {
+ var contextualParameterType = getRestTypeAtPosition(context, len);
+ assignParameterType(parameter, contextualParameterType);
}
}
}
- function checkPropertySignature(node) {
- if (ts.isPrivateIdentifier(node.name)) {
- error(node, ts.Diagnostics.Private_identifiers_are_not_allowed_outside_class_bodies);
+ function assignNonContextualParameterTypes(signature) {
+ if (signature.thisParameter) {
+ assignParameterType(signature.thisParameter);
+ }
+ for (var _i = 0, _a = signature.parameters; _i < _a.length; _i++) {
+ var parameter = _a[_i];
+ assignParameterType(parameter);
}
- return checkPropertyDeclaration(node);
}
- function checkMethodDeclaration(node) {
- // Grammar checking
- if (!checkGrammarMethod(node))
- checkGrammarComputedPropertyName(node.name);
- if (ts.isPrivateIdentifier(node.name)) {
- error(node, ts.Diagnostics.A_method_cannot_be_named_with_a_private_identifier);
+ function assignParameterType(parameter, type) {
+ var links = getSymbolLinks(parameter);
+ if (!links.type) {
+ var declaration = parameter.valueDeclaration;
+ links.type = type || getWidenedTypeForVariableLikeDeclaration(declaration, /*includeOptionality*/ true);
+ if (declaration.name.kind !== 78 /* Identifier */) {
+ // if inference didn't come up with anything but unknown, fall back to the binding pattern if present.
+ if (links.type === unknownType) {
+ links.type = getTypeFromBindingPattern(declaration.name);
+ }
+ assignBindingElementTypes(declaration.name);
+ }
}
- // Grammar checking for modifiers is done inside the function checkGrammarFunctionLikeDeclaration
- checkFunctionOrMethodDeclaration(node);
- // Abstract methods cannot have an implementation.
- // Extra checks are to avoid reporting multiple errors relating to the "abstractness" of the node.
- if (ts.hasModifier(node, 128 /* Abstract */) && node.kind === 161 /* MethodDeclaration */ && node.body) {
- error(node, ts.Diagnostics.Method_0_cannot_have_an_implementation_because_it_is_marked_abstract, ts.declarationNameToString(node.name));
+ }
+ // When contextual typing assigns a type to a parameter that contains a binding pattern, we also need to push
+ // the destructured type into the contained binding elements.
+ function assignBindingElementTypes(pattern) {
+ for (var _i = 0, _a = pattern.elements; _i < _a.length; _i++) {
+ var element = _a[_i];
+ if (!ts.isOmittedExpression(element)) {
+ if (element.name.kind === 78 /* Identifier */) {
+ getSymbolLinks(getSymbolOfNode(element)).type = getTypeForBindingElement(element);
+ }
+ else {
+ assignBindingElementTypes(element.name);
+ }
+ }
}
}
- function checkConstructorDeclaration(node) {
- // Grammar check on signature of constructor and modifier of the constructor is done in checkSignatureDeclaration function.
- checkSignatureDeclaration(node);
- // Grammar check for checking only related to constructorDeclaration
- if (!checkGrammarConstructorTypeParameters(node))
- checkGrammarConstructorTypeAnnotation(node);
- checkSourceElement(node.body);
- var symbol = getSymbolOfNode(node);
- var firstDeclaration = ts.getDeclarationOfKind(symbol, node.kind);
- // Only type check the symbol once
- if (node === firstDeclaration) {
- checkFunctionOrConstructorSymbol(symbol);
+ function createPromiseType(promisedType) {
+ // creates a `Promise` type where `T` is the promisedType argument
+ var globalPromiseType = getGlobalPromiseType(/*reportErrors*/ true);
+ if (globalPromiseType !== emptyGenericType) {
+ // if the promised type is itself a promise, get the underlying type; otherwise, fallback to the promised type
+ promisedType = getAwaitedType(promisedType) || unknownType;
+ return createTypeReference(globalPromiseType, [promisedType]);
}
- // exit early in the case of signature - super checks are not relevant to them
- if (ts.nodeIsMissing(node.body)) {
- return;
+ return unknownType;
+ }
+ function createPromiseLikeType(promisedType) {
+ // creates a `PromiseLike` type where `T` is the promisedType argument
+ var globalPromiseLikeType = getGlobalPromiseLikeType(/*reportErrors*/ true);
+ if (globalPromiseLikeType !== emptyGenericType) {
+ // if the promised type is itself a promise, get the underlying type; otherwise, fallback to the promised type
+ promisedType = getAwaitedType(promisedType) || unknownType;
+ return createTypeReference(globalPromiseLikeType, [promisedType]);
}
- if (!produceDiagnostics) {
- return;
+ return unknownType;
+ }
+ function createPromiseReturnType(func, promisedType) {
+ var promiseType = createPromiseType(promisedType);
+ if (promiseType === unknownType) {
+ error(func, ts.isImportCall(func) ?
+ ts.Diagnostics.A_dynamic_import_call_returns_a_Promise_Make_sure_you_have_a_declaration_for_Promise_or_include_ES2015_in_your_lib_option :
+ ts.Diagnostics.An_async_function_or_method_must_return_a_Promise_Make_sure_you_have_a_declaration_for_Promise_or_include_ES2015_in_your_lib_option);
+ return errorType;
}
- function isInstancePropertyWithInitializerOrPrivateIdentifierProperty(n) {
- if (ts.isPrivateIdentifierPropertyDeclaration(n)) {
- return true;
+ else if (!getGlobalPromiseConstructorSymbol(/*reportErrors*/ true)) {
+ error(func, ts.isImportCall(func) ?
+ ts.Diagnostics.A_dynamic_import_call_in_ES5_SlashES3_requires_the_Promise_constructor_Make_sure_you_have_a_declaration_for_the_Promise_constructor_or_include_ES2015_in_your_lib_option :
+ ts.Diagnostics.An_async_function_or_method_in_ES5_SlashES3_requires_the_Promise_constructor_Make_sure_you_have_a_declaration_for_the_Promise_constructor_or_include_ES2015_in_your_lib_option);
+ }
+ return promiseType;
+ }
+ function getReturnTypeFromBody(func, checkMode) {
+ if (!func.body) {
+ return errorType;
+ }
+ var functionFlags = ts.getFunctionFlags(func);
+ var isAsync = (functionFlags & 2 /* Async */) !== 0;
+ var isGenerator = (functionFlags & 1 /* Generator */) !== 0;
+ var returnType;
+ var yieldType;
+ var nextType;
+ var fallbackReturnType = voidType;
+ if (func.body.kind !== 230 /* Block */) { // Async or normal arrow function
+ returnType = checkExpressionCached(func.body, checkMode && checkMode & ~8 /* SkipGenericFunctions */);
+ if (isAsync) {
+ // From within an async function you can return either a non-promise value or a promise. Any
+ // Promise/A+ compatible implementation will always assimilate any foreign promise, so the
+ // return type of the body should be unwrapped to its awaited type, which we will wrap in
+ // the native Promise type later in this function.
+ returnType = checkAwaitedType(returnType, /*errorNode*/ func, ts.Diagnostics.The_return_type_of_an_async_function_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member);
}
- return n.kind === 159 /* PropertyDeclaration */ &&
- !ts.hasModifier(n, 32 /* Static */) &&
- !!n.initializer;
}
- // TS 1.0 spec (April 2014): 8.3.2
- // Constructors of classes with no extends clause may not contain super calls, whereas
- // constructors of derived classes must contain at least one super call somewhere in their function body.
- var containingClassDecl = node.parent;
- if (ts.getClassExtendsHeritageElement(containingClassDecl)) {
- captureLexicalThis(node.parent, containingClassDecl);
- var classExtendsNull = classDeclarationExtendsNull(containingClassDecl);
- var superCall = getSuperCallInConstructor(node);
- if (superCall) {
- if (classExtendsNull) {
- error(superCall, ts.Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null);
- }
- // The first statement in the body of a constructor (excluding prologue directives) must be a super call
- // if both of the following are true:
- // - The containing class is a derived class.
- // - The constructor declares parameter properties
- // or the containing class declares instance member variables with initializers.
- var superCallShouldBeFirst = (compilerOptions.target !== 99 /* ESNext */ || !compilerOptions.useDefineForClassFields) &&
- (ts.some(node.parent.members, isInstancePropertyWithInitializerOrPrivateIdentifierProperty) ||
- ts.some(node.parameters, function (p) { return ts.hasModifier(p, 92 /* ParameterPropertyModifier */); }));
- // Skip past any prologue directives to find the first statement
- // to ensure that it was a super call.
- if (superCallShouldBeFirst) {
- var statements = node.body.statements;
- var superCallStatement = void 0;
- for (var _i = 0, statements_3 = statements; _i < statements_3.length; _i++) {
- var statement = statements_3[_i];
- if (statement.kind === 226 /* ExpressionStatement */ && ts.isSuperCall(statement.expression)) {
- superCallStatement = statement;
- break;
- }
- if (!ts.isPrologueDirective(statement)) {
- break;
- }
- }
- if (!superCallStatement) {
- error(node, ts.Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_parameter_properties_or_private_identifiers);
- }
- }
+ else if (isGenerator) { // Generator or AsyncGenerator function
+ var returnTypes = checkAndAggregateReturnExpressionTypes(func, checkMode);
+ if (!returnTypes) {
+ fallbackReturnType = neverType;
}
- else if (!classExtendsNull) {
- error(node, ts.Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call);
+ else if (returnTypes.length > 0) {
+ returnType = getUnionType(returnTypes, 2 /* Subtype */);
}
+ var _a = checkAndAggregateYieldOperandTypes(func, checkMode), yieldTypes = _a.yieldTypes, nextTypes = _a.nextTypes;
+ yieldType = ts.some(yieldTypes) ? getUnionType(yieldTypes, 2 /* Subtype */) : undefined;
+ nextType = ts.some(nextTypes) ? getIntersectionType(nextTypes) : undefined;
}
- }
- function checkAccessorDeclaration(node) {
- if (produceDiagnostics) {
- // Grammar checking accessors
- if (!checkGrammarFunctionLikeDeclaration(node) && !checkGrammarAccessor(node))
- checkGrammarComputedPropertyName(node.name);
- checkDecorators(node);
- checkSignatureDeclaration(node);
- if (node.kind === 163 /* GetAccessor */) {
- if (!(node.flags & 8388608 /* Ambient */) && ts.nodeIsPresent(node.body) && (node.flags & 256 /* HasImplicitReturn */)) {
- if (!(node.flags & 512 /* HasExplicitReturn */)) {
- error(node.name, ts.Diagnostics.A_get_accessor_must_return_a_value);
- }
- }
- }
- // Do not use hasDynamicName here, because that returns false for well known symbols.
- // We want to perform checkComputedPropertyName for all computed properties, including
- // well known symbols.
- if (node.name.kind === 154 /* ComputedPropertyName */) {
- checkComputedPropertyName(node.name);
+ else { // Async or normal function
+ var types = checkAndAggregateReturnExpressionTypes(func, checkMode);
+ if (!types) {
+ // For an async function, the return type will not be never, but rather a Promise for never.
+ return functionFlags & 2 /* Async */
+ ? createPromiseReturnType(func, neverType) // Async function
+ : neverType; // Normal function
}
- if (ts.isPrivateIdentifier(node.name)) {
- error(node.name, ts.Diagnostics.An_accessor_cannot_be_named_with_a_private_identifier);
+ if (types.length === 0) {
+ // For an async function, the return type will not be void, but rather a Promise for void.
+ return functionFlags & 2 /* Async */
+ ? createPromiseReturnType(func, voidType) // Async function
+ : voidType; // Normal function
}
- if (!hasNonBindableDynamicName(node)) {
- // TypeScript 1.0 spec (April 2014): 8.4.3
- // Accessors for the same member name must specify the same accessibility.
- var otherKind = node.kind === 163 /* GetAccessor */ ? 164 /* SetAccessor */ : 163 /* GetAccessor */;
- var otherAccessor = ts.getDeclarationOfKind(getSymbolOfNode(node), otherKind);
- if (otherAccessor) {
- var nodeFlags = ts.getModifierFlags(node);
- var otherFlags = ts.getModifierFlags(otherAccessor);
- if ((nodeFlags & 28 /* AccessibilityModifier */) !== (otherFlags & 28 /* AccessibilityModifier */)) {
- error(node.name, ts.Diagnostics.Getter_and_setter_accessors_do_not_agree_in_visibility);
- }
- if ((nodeFlags & 128 /* Abstract */) !== (otherFlags & 128 /* Abstract */)) {
- error(node.name, ts.Diagnostics.Accessors_must_both_be_abstract_or_non_abstract);
- }
- // TypeScript 1.0 spec (April 2014): 4.5
- // If both accessors include type annotations, the specified types must be identical.
- checkAccessorDeclarationTypesIdentical(node, otherAccessor, getAnnotatedAccessorType, ts.Diagnostics.get_and_set_accessor_must_have_the_same_type);
- checkAccessorDeclarationTypesIdentical(node, otherAccessor, getThisTypeOfDeclaration, ts.Diagnostics.get_and_set_accessor_must_have_the_same_this_type);
+ // Return a union of the return expression types.
+ returnType = getUnionType(types, 2 /* Subtype */);
+ }
+ if (returnType || yieldType || nextType) {
+ if (yieldType)
+ reportErrorsFromWidening(func, yieldType, 3 /* GeneratorYield */);
+ if (returnType)
+ reportErrorsFromWidening(func, returnType, 1 /* FunctionReturn */);
+ if (nextType)
+ reportErrorsFromWidening(func, nextType, 2 /* GeneratorNext */);
+ if (returnType && isUnitType(returnType) ||
+ yieldType && isUnitType(yieldType) ||
+ nextType && isUnitType(nextType)) {
+ var contextualSignature = getContextualSignatureForFunctionLikeDeclaration(func);
+ var contextualType = !contextualSignature ? undefined :
+ contextualSignature === getSignatureFromDeclaration(func) ? isGenerator ? undefined : returnType :
+ instantiateContextualType(getReturnTypeOfSignature(contextualSignature), func);
+ if (isGenerator) {
+ yieldType = getWidenedLiteralLikeTypeForContextualIterationTypeIfNeeded(yieldType, contextualType, 0 /* Yield */, isAsync);
+ returnType = getWidenedLiteralLikeTypeForContextualIterationTypeIfNeeded(returnType, contextualType, 1 /* Return */, isAsync);
+ nextType = getWidenedLiteralLikeTypeForContextualIterationTypeIfNeeded(nextType, contextualType, 2 /* Next */, isAsync);
+ }
+ else {
+ returnType = getWidenedLiteralLikeTypeForContextualReturnTypeIfNeeded(returnType, contextualType, isAsync);
}
}
- var returnType = getTypeOfAccessors(getSymbolOfNode(node));
- if (node.kind === 163 /* GetAccessor */) {
- checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnType);
- }
+ if (yieldType)
+ yieldType = getWidenedType(yieldType);
+ if (returnType)
+ returnType = getWidenedType(returnType);
+ if (nextType)
+ nextType = getWidenedType(nextType);
}
- checkSourceElement(node.body);
- }
- function checkAccessorDeclarationTypesIdentical(first, second, getAnnotatedType, message) {
- var firstType = getAnnotatedType(first);
- var secondType = getAnnotatedType(second);
- if (firstType && secondType && !isTypeIdenticalTo(firstType, secondType)) {
- error(first, message);
+ if (isGenerator) {
+ return createGeneratorReturnType(yieldType || neverType, returnType || fallbackReturnType, nextType || getContextualIterationType(2 /* Next */, func) || unknownType, isAsync);
+ }
+ else {
+ // From within an async function you can return either a non-promise value or a promise. Any
+ // Promise/A+ compatible implementation will always assimilate any foreign promise, so the
+ // return type of the body is awaited type of the body, wrapped in a native Promise type.
+ return isAsync
+ ? createPromiseType(returnType || fallbackReturnType)
+ : returnType || fallbackReturnType;
}
}
- function checkMissingDeclaration(node) {
- checkDecorators(node);
- }
- function getEffectiveTypeArguments(node, typeParameters) {
- return fillMissingTypeArguments(ts.map(node.typeArguments, getTypeFromTypeNode), typeParameters, getMinTypeArgumentCount(typeParameters), ts.isInJSFile(node));
- }
- function checkTypeArgumentConstraints(node, typeParameters) {
- var typeArguments;
- var mapper;
- var result = true;
- for (var i = 0; i < typeParameters.length; i++) {
- var constraint = getConstraintOfTypeParameter(typeParameters[i]);
- if (constraint) {
- if (!typeArguments) {
- typeArguments = getEffectiveTypeArguments(node, typeParameters);
- mapper = createTypeMapper(typeParameters, typeArguments);
+ function createGeneratorReturnType(yieldType, returnType, nextType, isAsyncGenerator) {
+ var resolver = isAsyncGenerator ? asyncIterationTypesResolver : syncIterationTypesResolver;
+ var globalGeneratorType = resolver.getGlobalGeneratorType(/*reportErrors*/ false);
+ yieldType = resolver.resolveIterationType(yieldType, /*errorNode*/ undefined) || unknownType;
+ returnType = resolver.resolveIterationType(returnType, /*errorNode*/ undefined) || unknownType;
+ nextType = resolver.resolveIterationType(nextType, /*errorNode*/ undefined) || unknownType;
+ if (globalGeneratorType === emptyGenericType) {
+ // Fall back to the global IterableIterator if returnType is assignable to the expected return iteration
+ // type of IterableIterator, and the expected next iteration type of IterableIterator is assignable to
+ // nextType.
+ var globalType = resolver.getGlobalIterableIteratorType(/*reportErrors*/ false);
+ var iterationTypes = globalType !== emptyGenericType ? getIterationTypesOfGlobalIterableType(globalType, resolver) : undefined;
+ var iterableIteratorReturnType = iterationTypes ? iterationTypes.returnType : anyType;
+ var iterableIteratorNextType = iterationTypes ? iterationTypes.nextType : undefinedType;
+ if (isTypeAssignableTo(returnType, iterableIteratorReturnType) &&
+ isTypeAssignableTo(iterableIteratorNextType, nextType)) {
+ if (globalType !== emptyGenericType) {
+ return createTypeFromGenericGlobalType(globalType, [yieldType]);
}
- result = result && checkTypeAssignableTo(typeArguments[i], instantiateType(constraint, mapper), node.typeArguments[i], ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1);
+ // The global IterableIterator type doesn't exist, so report an error
+ resolver.getGlobalIterableIteratorType(/*reportErrors*/ true);
+ return emptyObjectType;
}
+ // The global Generator type doesn't exist, so report an error
+ resolver.getGlobalGeneratorType(/*reportErrors*/ true);
+ return emptyObjectType;
}
- return result;
+ return createTypeFromGenericGlobalType(globalGeneratorType, [yieldType, returnType, nextType]);
}
- function getTypeParametersForTypeReference(node) {
- var type = getTypeFromTypeReference(node);
- if (type !== errorType) {
- var symbol = getNodeLinks(node).resolvedSymbol;
- if (symbol) {
- return symbol.flags & 524288 /* TypeAlias */ && getSymbolLinks(symbol).typeParameters ||
- (ts.getObjectFlags(type) & 4 /* Reference */ ? type.target.localTypeParameters : undefined);
+ function checkAndAggregateYieldOperandTypes(func, checkMode) {
+ var yieldTypes = [];
+ var nextTypes = [];
+ var isAsync = (ts.getFunctionFlags(func) & 2 /* Async */) !== 0;
+ ts.forEachYieldExpression(func.body, function (yieldExpression) {
+ var yieldExpressionType = yieldExpression.expression ? checkExpression(yieldExpression.expression, checkMode) : undefinedWideningType;
+ ts.pushIfUnique(yieldTypes, getYieldedTypeOfYieldExpression(yieldExpression, yieldExpressionType, anyType, isAsync));
+ var nextType;
+ if (yieldExpression.asteriskToken) {
+ var iterationTypes = getIterationTypesOfIterable(yieldExpressionType, isAsync ? 19 /* AsyncYieldStar */ : 17 /* YieldStar */, yieldExpression.expression);
+ nextType = iterationTypes && iterationTypes.nextType;
}
- }
- return undefined;
+ else {
+ nextType = getContextualType(yieldExpression);
+ }
+ if (nextType)
+ ts.pushIfUnique(nextTypes, nextType);
+ });
+ return { yieldTypes: yieldTypes, nextTypes: nextTypes };
}
- function checkTypeReferenceNode(node) {
- checkGrammarTypeArguments(node, node.typeArguments);
- if (node.kind === 169 /* TypeReference */ && node.typeName.jsdocDotPos !== undefined && !ts.isInJSFile(node) && !ts.isInJSDoc(node)) {
- grammarErrorAtPos(node, node.typeName.jsdocDotPos, 1, ts.Diagnostics.JSDoc_types_can_only_be_used_inside_documentation_comments);
+ function getYieldedTypeOfYieldExpression(node, expressionType, sentType, isAsync) {
+ var errorNode = node.expression || node;
+ // A `yield*` expression effectively yields everything that its operand yields
+ var yieldedType = node.asteriskToken ? checkIteratedTypeOrElementType(isAsync ? 19 /* AsyncYieldStar */ : 17 /* YieldStar */, expressionType, sentType, errorNode) : expressionType;
+ return !isAsync ? yieldedType : getAwaitedType(yieldedType, errorNode, node.asteriskToken
+ ? ts.Diagnostics.Type_of_iterated_elements_of_a_yield_Asterisk_operand_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member
+ : ts.Diagnostics.Type_of_yield_operand_in_an_async_generator_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member);
+ }
+ /**
+ * Collect the TypeFacts learned from a typeof switch with
+ * total clauses `witnesses`, and the active clause ranging
+ * from `start` to `end`. Parameter `hasDefault` denotes
+ * whether the active clause contains a default clause.
+ */
+ function getFactsFromTypeofSwitch(start, end, witnesses, hasDefault) {
+ var facts = 0 /* None */;
+ // When in the default we only collect inequality facts
+ // because default is 'in theory' a set of infinite
+ // equalities.
+ if (hasDefault) {
+ // Value is not equal to any types after the active clause.
+ for (var i = end; i < witnesses.length; i++) {
+ facts |= typeofNEFacts.get(witnesses[i]) || 32768 /* TypeofNEHostObject */;
+ }
+ // Remove inequalities for types that appear in the
+ // active clause because they appear before other
+ // types collected so far.
+ for (var i = start; i < end; i++) {
+ facts &= ~(typeofNEFacts.get(witnesses[i]) || 0);
+ }
+ // Add inequalities for types before the active clause unconditionally.
+ for (var i = 0; i < start; i++) {
+ facts |= typeofNEFacts.get(witnesses[i]) || 32768 /* TypeofNEHostObject */;
+ }
}
- ts.forEach(node.typeArguments, checkSourceElement);
- var type = getTypeFromTypeReference(node);
- if (type !== errorType) {
- if (node.typeArguments && produceDiagnostics) {
- var typeParameters = getTypeParametersForTypeReference(node);
- if (typeParameters) {
- checkTypeArgumentConstraints(node, typeParameters);
- }
+ // When in an active clause without default the set of
+ // equalities is finite.
+ else {
+ // Add equalities for all types in the active clause.
+ for (var i = start; i < end; i++) {
+ facts |= typeofEQFacts.get(witnesses[i]) || 128 /* TypeofEQHostObject */;
}
- if (type.flags & 32 /* Enum */ && getNodeLinks(node).resolvedSymbol.flags & 8 /* EnumMember */) {
- error(node, ts.Diagnostics.Enum_type_0_has_members_with_initializers_that_are_not_literals, typeToString(type));
+ // Remove equalities for types that appear before the
+ // active clause.
+ for (var i = 0; i < start; i++) {
+ facts &= ~(typeofEQFacts.get(witnesses[i]) || 0);
}
}
+ return facts;
}
- function getTypeArgumentConstraint(node) {
- var typeReferenceNode = ts.tryCast(node.parent, ts.isTypeReferenceType);
- if (!typeReferenceNode)
- return undefined;
- var typeParameters = getTypeParametersForTypeReference(typeReferenceNode); // TODO: GH#18217
- var constraint = getConstraintOfTypeParameter(typeParameters[typeReferenceNode.typeArguments.indexOf(node)]);
- return constraint && instantiateType(constraint, createTypeMapper(typeParameters, getEffectiveTypeArguments(typeReferenceNode, typeParameters)));
- }
- function checkTypeQuery(node) {
- getTypeFromTypeQueryNode(node);
+ function isExhaustiveSwitchStatement(node) {
+ var links = getNodeLinks(node);
+ return links.isExhaustive !== undefined ? links.isExhaustive : (links.isExhaustive = computeExhaustiveSwitchStatement(node));
}
- function checkTypeLiteral(node) {
- ts.forEach(node.members, checkSourceElement);
- if (produceDiagnostics) {
- var type = getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node);
- checkIndexConstraints(type);
- checkTypeForDuplicateIndexSignatures(node);
- checkObjectTypeForDuplicateDeclarations(node);
+ function computeExhaustiveSwitchStatement(node) {
+ if (node.expression.kind === 211 /* TypeOfExpression */) {
+ var operandType = getTypeOfExpression(node.expression.expression);
+ var witnesses = getSwitchClauseTypeOfWitnesses(node, /*retainDefault*/ false);
+ // notEqualFacts states that the type of the switched value is not equal to every type in the switch.
+ var notEqualFacts_1 = getFactsFromTypeofSwitch(0, 0, witnesses, /*hasDefault*/ true);
+ var type_4 = getBaseConstraintOfType(operandType) || operandType;
+ // Take any/unknown as a special condition. Or maybe we could change `type` to a union containing all primitive types.
+ if (type_4.flags & 3 /* AnyOrUnknown */) {
+ return (556800 /* AllTypeofNE */ & notEqualFacts_1) === 556800 /* AllTypeofNE */;
+ }
+ return !!(filterType(type_4, function (t) { return (getTypeFacts(t) & notEqualFacts_1) === notEqualFacts_1; }).flags & 131072 /* Never */);
+ }
+ var type = getTypeOfExpression(node.expression);
+ if (!isLiteralType(type)) {
+ return false;
+ }
+ var switchTypes = getSwitchClauseTypes(node);
+ if (!switchTypes.length || ts.some(switchTypes, isNeitherUnitTypeNorNever)) {
+ return false;
}
+ return eachTypeContainedIn(mapType(type, getRegularTypeOfLiteralType), switchTypes);
}
- function checkArrayType(node) {
- checkSourceElement(node.elementType);
+ function functionHasImplicitReturn(func) {
+ return func.endFlowNode && isReachableFlowNode(func.endFlowNode);
}
- function checkTupleType(node) {
- var elementTypes = node.elementTypes;
- var seenOptionalElement = false;
- for (var i = 0; i < elementTypes.length; i++) {
- var e = elementTypes[i];
- if (e.kind === 177 /* RestType */) {
- if (i !== elementTypes.length - 1) {
- grammarErrorOnNode(e, ts.Diagnostics.A_rest_element_must_be_last_in_a_tuple_type);
- break;
+ /** NOTE: Return value of `[]` means a different thing than `undefined`. `[]` means func returns `void`, `undefined` means it returns `never`. */
+ function checkAndAggregateReturnExpressionTypes(func, checkMode) {
+ var functionFlags = ts.getFunctionFlags(func);
+ var aggregatedTypes = [];
+ var hasReturnWithNoExpression = functionHasImplicitReturn(func);
+ var hasReturnOfTypeNever = false;
+ ts.forEachReturnStatement(func.body, function (returnStatement) {
+ var expr = returnStatement.expression;
+ if (expr) {
+ var type = checkExpressionCached(expr, checkMode && checkMode & ~8 /* SkipGenericFunctions */);
+ if (functionFlags & 2 /* Async */) {
+ // From within an async function you can return either a non-promise value or a promise. Any
+ // Promise/A+ compatible implementation will always assimilate any foreign promise, so the
+ // return type of the body should be unwrapped to its awaited type, which should be wrapped in
+ // the native Promise type by the caller.
+ type = checkAwaitedType(type, func, ts.Diagnostics.The_return_type_of_an_async_function_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member);
}
- if (!isArrayType(getTypeFromTypeNode(e.type))) {
- error(e, ts.Diagnostics.A_rest_element_type_must_be_an_array_type);
+ if (type.flags & 131072 /* Never */) {
+ hasReturnOfTypeNever = true;
}
+ ts.pushIfUnique(aggregatedTypes, type);
}
- else if (e.kind === 176 /* OptionalType */) {
- seenOptionalElement = true;
- }
- else if (seenOptionalElement) {
- grammarErrorOnNode(e, ts.Diagnostics.A_required_element_cannot_follow_an_optional_element);
- break;
+ else {
+ hasReturnWithNoExpression = true;
}
+ });
+ if (aggregatedTypes.length === 0 && !hasReturnWithNoExpression && (hasReturnOfTypeNever || mayReturnNever(func))) {
+ return undefined;
+ }
+ if (strictNullChecks && aggregatedTypes.length && hasReturnWithNoExpression &&
+ !(isJSConstructor(func) && aggregatedTypes.some(function (t) { return t.symbol === func.symbol; }))) {
+ // Javascript "callable constructors", containing eg `if (!(this instanceof A)) return new A()` should not add undefined
+ ts.pushIfUnique(aggregatedTypes, undefinedType);
}
- ts.forEach(node.elementTypes, checkSourceElement);
+ return aggregatedTypes;
}
- function checkUnionOrIntersectionType(node) {
- ts.forEach(node.types, checkSourceElement);
+ function mayReturnNever(func) {
+ switch (func.kind) {
+ case 208 /* FunctionExpression */:
+ case 209 /* ArrowFunction */:
+ return true;
+ case 165 /* MethodDeclaration */:
+ return func.parent.kind === 200 /* ObjectLiteralExpression */;
+ default:
+ return false;
+ }
}
- function checkIndexedAccessIndexType(type, accessNode) {
- if (!(type.flags & 8388608 /* IndexedAccess */)) {
- return type;
+ /**
+ * TypeScript Specification 1.0 (6.3) - July 2014
+ * An explicitly typed function whose return type isn't the Void type,
+ * the Any type, or a union type containing the Void or Any type as a constituent
+ * must have at least one return statement somewhere in its body.
+ * An exception to this rule is if the function implementation consists of a single 'throw' statement.
+ *
+ * @param returnType - return type of the function, can be undefined if return type is not explicitly specified
+ */
+ function checkAllCodePathsInNonVoidFunctionReturnOrThrow(func, returnType) {
+ if (!produceDiagnostics) {
+ return;
}
- // Check if the index type is assignable to 'keyof T' for the object type.
- var objectType = type.objectType;
- var indexType = type.indexType;
- if (isTypeAssignableTo(indexType, getIndexType(objectType, /*stringsOnly*/ false))) {
- if (accessNode.kind === 195 /* ElementAccessExpression */ && ts.isAssignmentTarget(accessNode) &&
- ts.getObjectFlags(objectType) & 32 /* Mapped */ && getMappedTypeModifiers(objectType) & 1 /* IncludeReadonly */) {
- error(accessNode, ts.Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(objectType));
- }
- return type;
+ var functionFlags = ts.getFunctionFlags(func);
+ var type = returnType && unwrapReturnType(returnType, functionFlags);
+ // Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions.
+ if (type && maybeTypeOfKind(type, 1 /* Any */ | 16384 /* Void */)) {
+ return;
}
- // Check if we're indexing with a numeric type and if either object or index types
- // is a generic type with a constraint that has a numeric index signature.
- var apparentObjectType = getApparentType(objectType);
- if (getIndexInfoOfType(apparentObjectType, 1 /* Number */) && isTypeAssignableToKind(indexType, 296 /* NumberLike */)) {
- return type;
+ // If all we have is a function signature, or an arrow function with an expression body, then there is nothing to check.
+ // also if HasImplicitReturn flag is not set this means that all codepaths in function body end with return or throw
+ if (func.kind === 164 /* MethodSignature */ || ts.nodeIsMissing(func.body) || func.body.kind !== 230 /* Block */ || !functionHasImplicitReturn(func)) {
+ return;
}
- if (isGenericObjectType(objectType)) {
- var propertyName_1 = getPropertyNameFromIndex(indexType, accessNode);
- if (propertyName_1) {
- var propertySymbol = forEachType(apparentObjectType, function (t) { return getPropertyOfType(t, propertyName_1); });
- if (propertySymbol && ts.getDeclarationModifierFlagsFromSymbol(propertySymbol) & 24 /* NonPublicAccessibilityModifier */) {
- error(accessNode, ts.Diagnostics.Private_or_protected_member_0_cannot_be_accessed_on_a_type_parameter, ts.unescapeLeadingUnderscores(propertyName_1));
- return errorType;
- }
- }
+ var hasExplicitReturn = func.flags & 512 /* HasExplicitReturn */;
+ if (type && type.flags & 131072 /* Never */) {
+ error(ts.getEffectiveReturnTypeNode(func), ts.Diagnostics.A_function_returning_never_cannot_have_a_reachable_end_point);
}
- error(accessNode, ts.Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(indexType), typeToString(objectType));
- return errorType;
- }
- function checkIndexedAccessType(node) {
- checkSourceElement(node.objectType);
- checkSourceElement(node.indexType);
- checkIndexedAccessIndexType(getTypeFromIndexedAccessTypeNode(node), node);
- }
- function checkMappedType(node) {
- checkSourceElement(node.typeParameter);
- checkSourceElement(node.type);
- if (!node.type) {
- reportImplicitAny(node, anyType);
+ else if (type && !hasExplicitReturn) {
+ // minimal check: function has syntactic return type annotation and no explicit return statements in the body
+ // this function does not conform to the specification.
+ // NOTE: having returnType !== undefined is a precondition for entering this branch so func.type will always be present
+ error(ts.getEffectiveReturnTypeNode(func), ts.Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value);
}
- var type = getTypeFromMappedTypeNode(node);
- var constraintType = getConstraintTypeFromMappedType(type);
- checkTypeAssignableTo(constraintType, keyofConstraintType, ts.getEffectiveConstraintOfTypeParameter(node.typeParameter));
- }
- function checkThisType(node) {
- getTypeFromThisTypeNode(node);
- }
- function checkTypeOperator(node) {
- checkGrammarTypeOperatorNode(node);
- checkSourceElement(node.type);
- }
- function checkConditionalType(node) {
- ts.forEachChild(node, checkSourceElement);
- }
- function checkInferType(node) {
- if (!ts.findAncestor(node, function (n) { return n.parent && n.parent.kind === 180 /* ConditionalType */ && n.parent.extendsType === n; })) {
- grammarErrorOnNode(node, ts.Diagnostics.infer_declarations_are_only_permitted_in_the_extends_clause_of_a_conditional_type);
+ else if (type && strictNullChecks && !isTypeAssignableTo(undefinedType, type)) {
+ error(ts.getEffectiveReturnTypeNode(func) || func, ts.Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined);
}
- checkSourceElement(node.typeParameter);
- registerForUnusedIdentifiersCheck(node);
- }
- function checkImportType(node) {
- checkSourceElement(node.argument);
- getTypeFromTypeNode(node);
- }
- function isPrivateWithinAmbient(node) {
- return (ts.hasModifier(node, 8 /* Private */) || ts.isPrivateIdentifierPropertyDeclaration(node)) && !!(node.flags & 8388608 /* Ambient */);
- }
- function getEffectiveDeclarationFlags(n, flagsToCheck) {
- var flags = ts.getCombinedModifierFlags(n);
- // children of classes (even ambient classes) should not be marked as ambient or export
- // because those flags have no useful semantics there.
- if (n.parent.kind !== 246 /* InterfaceDeclaration */ &&
- n.parent.kind !== 245 /* ClassDeclaration */ &&
- n.parent.kind !== 214 /* ClassExpression */ &&
- n.flags & 8388608 /* Ambient */) {
- if (!(flags & 2 /* Ambient */) && !(ts.isModuleBlock(n.parent) && ts.isModuleDeclaration(n.parent.parent) && ts.isGlobalScopeAugmentation(n.parent.parent))) {
- // It is nested in an ambient context, which means it is automatically exported
- flags |= 1 /* Export */;
+ else if (compilerOptions.noImplicitReturns) {
+ if (!type) {
+ // If return type annotation is omitted check if function has any explicit return statements.
+ // If it does not have any - its inferred return type is void - don't do any checks.
+ // Otherwise get inferred return type from function body and report error only if it is not void / anytype
+ if (!hasExplicitReturn) {
+ return;
+ }
+ var inferredReturnType = getReturnTypeOfSignature(getSignatureFromDeclaration(func));
+ if (isUnwrappedReturnTypeVoidOrAny(func, inferredReturnType)) {
+ return;
+ }
}
- flags |= 2 /* Ambient */;
+ error(ts.getEffectiveReturnTypeNode(func) || func, ts.Diagnostics.Not_all_code_paths_return_a_value);
}
- return flags & flagsToCheck;
}
- function checkFunctionOrConstructorSymbol(symbol) {
- if (!produceDiagnostics) {
- return;
- }
- function getCanonicalOverload(overloads, implementation) {
- // Consider the canonical set of flags to be the flags of the bodyDeclaration or the first declaration
- // Error on all deviations from this canonical set of flags
- // The caveat is that if some overloads are defined in lib.d.ts, we don't want to
- // report the errors on those. To achieve this, we will say that the implementation is
- // the canonical signature only if it is in the same container as the first overload
- var implementationSharesContainerWithFirstOverload = implementation !== undefined && implementation.parent === overloads[0].parent;
- return implementationSharesContainerWithFirstOverload ? implementation : overloads[0];
- }
- function checkFlagAgreementBetweenOverloads(overloads, implementation, flagsToCheck, someOverloadFlags, allOverloadFlags) {
- // Error if some overloads have a flag that is not shared by all overloads. To find the
- // deviations, we XOR someOverloadFlags with allOverloadFlags
- var someButNotAllOverloadFlags = someOverloadFlags ^ allOverloadFlags;
- if (someButNotAllOverloadFlags !== 0) {
- var canonicalFlags_1 = getEffectiveDeclarationFlags(getCanonicalOverload(overloads, implementation), flagsToCheck);
- ts.forEach(overloads, function (o) {
- var deviation = getEffectiveDeclarationFlags(o, flagsToCheck) ^ canonicalFlags_1;
- if (deviation & 1 /* Export */) {
- error(ts.getNameOfDeclaration(o), ts.Diagnostics.Overload_signatures_must_all_be_exported_or_non_exported);
- }
- else if (deviation & 2 /* Ambient */) {
- error(ts.getNameOfDeclaration(o), ts.Diagnostics.Overload_signatures_must_all_be_ambient_or_non_ambient);
- }
- else if (deviation & (8 /* Private */ | 16 /* Protected */)) {
- error(ts.getNameOfDeclaration(o) || o, ts.Diagnostics.Overload_signatures_must_all_be_public_private_or_protected);
- }
- else if (deviation & 128 /* Abstract */) {
- error(ts.getNameOfDeclaration(o), ts.Diagnostics.Overload_signatures_must_all_be_abstract_or_non_abstract);
+ function checkFunctionExpressionOrObjectLiteralMethod(node, checkMode) {
+ ts.Debug.assert(node.kind !== 165 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node));
+ checkNodeDeferred(node);
+ // The identityMapper object is used to indicate that function expressions are wildcards
+ if (checkMode && checkMode & 4 /* SkipContextSensitive */ && isContextSensitive(node)) {
+ // Skip parameters, return signature with return type that retains noncontextual parts so inferences can still be drawn in an early stage
+ if (!ts.getEffectiveReturnTypeNode(node) && !hasContextSensitiveParameters(node)) {
+ // Return plain anyFunctionType if there is no possibility we'll make inferences from the return type
+ var contextualSignature = getContextualSignature(node);
+ if (contextualSignature && couldContainTypeVariables(getReturnTypeOfSignature(contextualSignature))) {
+ var links = getNodeLinks(node);
+ if (links.contextFreeType) {
+ return links.contextFreeType;
}
- });
+ var returnType = getReturnTypeFromBody(node, checkMode);
+ var returnOnlySignature = createSignature(undefined, undefined, undefined, ts.emptyArray, returnType, /*resolvedTypePredicate*/ undefined, 0, 0 /* None */);
+ var returnOnlyType = createAnonymousType(node.symbol, emptySymbols, [returnOnlySignature], ts.emptyArray, undefined, undefined);
+ returnOnlyType.objectFlags |= 2097152 /* NonInferrableType */;
+ return links.contextFreeType = returnOnlyType;
+ }
}
+ return anyFunctionType;
}
- function checkQuestionTokenAgreementBetweenOverloads(overloads, implementation, someHaveQuestionToken, allHaveQuestionToken) {
- if (someHaveQuestionToken !== allHaveQuestionToken) {
- var canonicalHasQuestionToken_1 = ts.hasQuestionToken(getCanonicalOverload(overloads, implementation));
- ts.forEach(overloads, function (o) {
- var deviation = ts.hasQuestionToken(o) !== canonicalHasQuestionToken_1;
- if (deviation) {
- error(ts.getNameOfDeclaration(o), ts.Diagnostics.Overload_signatures_must_all_be_optional_or_required);
- }
- });
- }
+ // Grammar checking
+ var hasGrammarError = checkGrammarFunctionLikeDeclaration(node);
+ if (!hasGrammarError && node.kind === 208 /* FunctionExpression */) {
+ checkGrammarForGenerator(node);
}
- var flagsToCheck = 1 /* Export */ | 2 /* Ambient */ | 8 /* Private */ | 16 /* Protected */ | 128 /* Abstract */;
- var someNodeFlags = 0 /* None */;
- var allNodeFlags = flagsToCheck;
- var someHaveQuestionToken = false;
- var allHaveQuestionToken = true;
- var hasOverloads = false;
- var bodyDeclaration;
- var lastSeenNonAmbientDeclaration;
- var previousDeclaration;
- var declarations = symbol.declarations;
- var isConstructor = (symbol.flags & 16384 /* Constructor */) !== 0;
- function reportImplementationExpectedError(node) {
- if (node.name && ts.nodeIsMissing(node.name)) {
- return;
- }
- var seen = false;
- var subsequentNode = ts.forEachChild(node.parent, function (c) {
- if (seen) {
- return c;
- }
- else {
- seen = c === node;
+ contextuallyCheckFunctionExpressionOrObjectLiteralMethod(node, checkMode);
+ return getTypeOfSymbol(getSymbolOfNode(node));
+ }
+ function contextuallyCheckFunctionExpressionOrObjectLiteralMethod(node, checkMode) {
+ var links = getNodeLinks(node);
+ // Check if function expression is contextually typed and assign parameter types if so.
+ if (!(links.flags & 1024 /* ContextChecked */)) {
+ var contextualSignature = getContextualSignature(node);
+ // If a type check is started at a function expression that is an argument of a function call, obtaining the
+ // contextual type may recursively get back to here during overload resolution of the call. If so, we will have
+ // already assigned contextual types.
+ if (!(links.flags & 1024 /* ContextChecked */)) {
+ links.flags |= 1024 /* ContextChecked */;
+ var signature = ts.firstOrUndefined(getSignaturesOfType(getTypeOfSymbol(getSymbolOfNode(node)), 0 /* Call */));
+ if (!signature) {
+ return;
}
- });
- // We may be here because of some extra nodes between overloads that could not be parsed into a valid node.
- // In this case the subsequent node is not really consecutive (.pos !== node.end), and we must ignore it here.
- if (subsequentNode && subsequentNode.pos === node.end) {
- if (subsequentNode.kind === node.kind) {
- var errorNode_1 = subsequentNode.name || subsequentNode;
- var subsequentName = subsequentNode.name;
- if (node.name && subsequentName && (
- // both are private identifiers
- ts.isPrivateIdentifier(node.name) && ts.isPrivateIdentifier(subsequentName) && node.name.escapedText === subsequentName.escapedText ||
- // Both are computed property names
- // TODO: GH#17345: These are methods, so handle computed name case. (`Always allowing computed property names is *not* the correct behavior!)
- ts.isComputedPropertyName(node.name) && ts.isComputedPropertyName(subsequentName) ||
- // Both are literal property names that are the same.
- ts.isPropertyNameLiteral(node.name) && ts.isPropertyNameLiteral(subsequentName) &&
- ts.getEscapedTextOfIdentifierOrLiteral(node.name) === ts.getEscapedTextOfIdentifierOrLiteral(subsequentName))) {
- var reportError = (node.kind === 161 /* MethodDeclaration */ || node.kind === 160 /* MethodSignature */) &&
- ts.hasModifier(node, 32 /* Static */) !== ts.hasModifier(subsequentNode, 32 /* Static */);
- // we can get here in two cases
- // 1. mixed static and instance class members
- // 2. something with the same name was defined before the set of overloads that prevents them from merging
- // here we'll report error only for the first case since for second we should already report error in binder
- if (reportError) {
- var diagnostic = ts.hasModifier(node, 32 /* Static */) ? ts.Diagnostics.Function_overload_must_be_static : ts.Diagnostics.Function_overload_must_not_be_static;
- error(errorNode_1, diagnostic);
+ if (isContextSensitive(node)) {
+ if (contextualSignature) {
+ var inferenceContext = getInferenceContext(node);
+ if (checkMode && checkMode & 2 /* Inferential */) {
+ inferFromAnnotatedParameters(signature, contextualSignature, inferenceContext);
}
- return;
+ var instantiatedContextualSignature = inferenceContext ?
+ instantiateSignature(contextualSignature, inferenceContext.mapper) : contextualSignature;
+ assignContextualParameterTypes(signature, instantiatedContextualSignature);
}
- if (ts.nodeIsPresent(subsequentNode.body)) {
- error(errorNode_1, ts.Diagnostics.Function_implementation_name_must_be_0, ts.declarationNameToString(node.name));
- return;
+ else {
+ // Force resolution of all parameter types such that the absence of a contextual type is consistently reflected.
+ assignNonContextualParameterTypes(signature);
}
}
- }
- var errorNode = node.name || node;
- if (isConstructor) {
- error(errorNode, ts.Diagnostics.Constructor_implementation_is_missing);
- }
- else {
- // Report different errors regarding non-consecutive blocks of declarations depending on whether
- // the node in question is abstract.
- if (ts.hasModifier(node, 128 /* Abstract */)) {
- error(errorNode, ts.Diagnostics.All_declarations_of_an_abstract_method_must_be_consecutive);
- }
- else {
- error(errorNode, ts.Diagnostics.Function_implementation_is_missing_or_not_immediately_following_the_declaration);
+ if (contextualSignature && !getReturnTypeFromAnnotation(node) && !signature.resolvedReturnType) {
+ var returnType = getReturnTypeFromBody(node, checkMode);
+ if (!signature.resolvedReturnType) {
+ signature.resolvedReturnType = returnType;
+ }
}
+ checkSignatureDeclaration(node);
}
}
- var duplicateFunctionDeclaration = false;
- var multipleConstructorImplementation = false;
- var hasNonAmbientClass = false;
- for (var _i = 0, declarations_4 = declarations; _i < declarations_4.length; _i++) {
- var current = declarations_4[_i];
- var node = current;
- var inAmbientContext = node.flags & 8388608 /* Ambient */;
- var inAmbientContextOrInterface = node.parent.kind === 246 /* InterfaceDeclaration */ || node.parent.kind === 173 /* TypeLiteral */ || inAmbientContext;
- if (inAmbientContextOrInterface) {
- // check if declarations are consecutive only if they are non-ambient
- // 1. ambient declarations can be interleaved
- // i.e. this is legal
- // declare function foo();
- // declare function bar();
- // declare function foo();
- // 2. mixing ambient and non-ambient declarations is a separate error that will be reported - do not want to report an extra one
- previousDeclaration = undefined;
+ }
+ function checkFunctionExpressionOrObjectLiteralMethodDeferred(node) {
+ ts.Debug.assert(node.kind !== 165 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node));
+ var functionFlags = ts.getFunctionFlags(node);
+ var returnType = getReturnTypeFromAnnotation(node);
+ checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnType);
+ if (node.body) {
+ if (!ts.getEffectiveReturnTypeNode(node)) {
+ // There are some checks that are only performed in getReturnTypeFromBody, that may produce errors
+ // we need. An example is the noImplicitAny errors resulting from widening the return expression
+ // of a function. Because checking of function expression bodies is deferred, there was never an
+ // appropriate time to do this during the main walk of the file (see the comment at the top of
+ // checkFunctionExpressionBodies). So it must be done now.
+ getReturnTypeOfSignature(getSignatureFromDeclaration(node));
}
- if ((node.kind === 245 /* ClassDeclaration */ || node.kind === 214 /* ClassExpression */) && !inAmbientContext) {
- hasNonAmbientClass = true;
+ if (node.body.kind === 230 /* Block */) {
+ checkSourceElement(node.body);
}
- if (node.kind === 244 /* FunctionDeclaration */ || node.kind === 161 /* MethodDeclaration */ || node.kind === 160 /* MethodSignature */ || node.kind === 162 /* Constructor */) {
- var currentNodeFlags = getEffectiveDeclarationFlags(node, flagsToCheck);
- someNodeFlags |= currentNodeFlags;
- allNodeFlags &= currentNodeFlags;
- someHaveQuestionToken = someHaveQuestionToken || ts.hasQuestionToken(node);
- allHaveQuestionToken = allHaveQuestionToken && ts.hasQuestionToken(node);
- if (ts.nodeIsPresent(node.body) && bodyDeclaration) {
- if (isConstructor) {
- multipleConstructorImplementation = true;
+ else {
+ // From within an async function you can return either a non-promise value or a promise. Any
+ // Promise/A+ compatible implementation will always assimilate any foreign promise, so we
+ // should not be checking assignability of a promise to the return type. Instead, we need to
+ // check assignability of the awaited type of the expression body against the promised type of
+ // its return type annotation.
+ var exprType = checkExpression(node.body);
+ var returnOrPromisedType = returnType && unwrapReturnType(returnType, functionFlags);
+ if (returnOrPromisedType) {
+ if ((functionFlags & 3 /* AsyncGenerator */) === 2 /* Async */) { // Async function
+ var awaitedType = checkAwaitedType(exprType, node.body, ts.Diagnostics.The_return_type_of_an_async_function_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member);
+ checkTypeAssignableToAndOptionallyElaborate(awaitedType, returnOrPromisedType, node.body, node.body);
}
- else {
- duplicateFunctionDeclaration = true;
+ else { // Normal function
+ checkTypeAssignableToAndOptionallyElaborate(exprType, returnOrPromisedType, node.body, node.body);
}
}
- else if (previousDeclaration && previousDeclaration.parent === node.parent && previousDeclaration.end !== node.pos) {
- reportImplementationExpectedError(previousDeclaration);
+ }
+ }
+ }
+ function checkArithmeticOperandType(operand, type, diagnostic, isAwaitValid) {
+ if (isAwaitValid === void 0) { isAwaitValid = false; }
+ if (!isTypeAssignableTo(type, numberOrBigIntType)) {
+ var awaitedType = isAwaitValid && getAwaitedTypeOfPromise(type);
+ errorAndMaybeSuggestAwait(operand, !!awaitedType && isTypeAssignableTo(awaitedType, numberOrBigIntType), diagnostic);
+ return false;
+ }
+ return true;
+ }
+ function isReadonlyAssignmentDeclaration(d) {
+ if (!ts.isCallExpression(d)) {
+ return false;
+ }
+ if (!ts.isBindableObjectDefinePropertyCall(d)) {
+ return false;
+ }
+ var objectLitType = checkExpressionCached(d.arguments[2]);
+ var valueType = getTypeOfPropertyOfType(objectLitType, "value");
+ if (valueType) {
+ var writableProp = getPropertyOfType(objectLitType, "writable");
+ var writableType = writableProp && getTypeOfSymbol(writableProp);
+ if (!writableType || writableType === falseType || writableType === regularFalseType) {
+ return true;
+ }
+ // We include this definition whereupon we walk back and check the type at the declaration because
+ // The usual definition of `Object.defineProperty` will _not_ cause literal types to be preserved in the
+ // argument types, should the type be contextualized by the call itself.
+ if (writableProp && writableProp.valueDeclaration && ts.isPropertyAssignment(writableProp.valueDeclaration)) {
+ var initializer = writableProp.valueDeclaration.initializer;
+ var rawOriginalType = checkExpression(initializer);
+ if (rawOriginalType === falseType || rawOriginalType === regularFalseType) {
+ return true;
}
- if (ts.nodeIsPresent(node.body)) {
- if (!bodyDeclaration) {
- bodyDeclaration = node;
- }
+ }
+ return false;
+ }
+ var setProp = getPropertyOfType(objectLitType, "set");
+ return !setProp;
+ }
+ function isReadonlySymbol(symbol) {
+ // The following symbols are considered read-only:
+ // Properties with a 'readonly' modifier
+ // Variables declared with 'const'
+ // Get accessors without matching set accessors
+ // Enum members
+ // Object.defineProperty assignments with writable false or no setter
+ // Unions and intersections of the above (unions and intersections eagerly set isReadonly on creation)
+ return !!(ts.getCheckFlags(symbol) & 8 /* Readonly */ ||
+ symbol.flags & 4 /* Property */ && ts.getDeclarationModifierFlagsFromSymbol(symbol) & 64 /* Readonly */ ||
+ symbol.flags & 3 /* Variable */ && getDeclarationNodeFlagsFromSymbol(symbol) & 2 /* Const */ ||
+ symbol.flags & 98304 /* Accessor */ && !(symbol.flags & 65536 /* SetAccessor */) ||
+ symbol.flags & 8 /* EnumMember */ ||
+ ts.some(symbol.declarations, isReadonlyAssignmentDeclaration));
+ }
+ function isAssignmentToReadonlyEntity(expr, symbol, assignmentKind) {
+ var _a, _b;
+ if (assignmentKind === 0 /* None */) {
+ // no assigment means it doesn't matter whether the entity is readonly
+ return false;
+ }
+ if (isReadonlySymbol(symbol)) {
+ // Allow assignments to readonly properties within constructors of the same class declaration.
+ if (symbol.flags & 4 /* Property */ &&
+ ts.isAccessExpression(expr) &&
+ expr.expression.kind === 107 /* ThisKeyword */) {
+ // Look for if this is the constructor for the class that `symbol` is a property of.
+ var ctor = ts.getContainingFunction(expr);
+ if (!(ctor && (ctor.kind === 166 /* Constructor */ || isJSConstructor(ctor)))) {
+ return true;
}
- else {
- hasOverloads = true;
+ if (symbol.valueDeclaration) {
+ var isAssignmentDeclaration_1 = ts.isBinaryExpression(symbol.valueDeclaration);
+ var isLocalPropertyDeclaration = ctor.parent === symbol.valueDeclaration.parent;
+ var isLocalParameterProperty = ctor === symbol.valueDeclaration.parent;
+ var isLocalThisPropertyAssignment = isAssignmentDeclaration_1 && ((_a = symbol.parent) === null || _a === void 0 ? void 0 : _a.valueDeclaration) === ctor.parent;
+ var isLocalThisPropertyAssignmentConstructorFunction = isAssignmentDeclaration_1 && ((_b = symbol.parent) === null || _b === void 0 ? void 0 : _b.valueDeclaration) === ctor;
+ var isWriteableSymbol = isLocalPropertyDeclaration
+ || isLocalParameterProperty
+ || isLocalThisPropertyAssignment
+ || isLocalThisPropertyAssignmentConstructorFunction;
+ return !isWriteableSymbol;
}
- previousDeclaration = node;
- if (!inAmbientContextOrInterface) {
- lastSeenNonAmbientDeclaration = node;
+ }
+ return true;
+ }
+ if (ts.isAccessExpression(expr)) {
+ // references through namespace import should be readonly
+ var node = ts.skipParentheses(expr.expression);
+ if (node.kind === 78 /* Identifier */) {
+ var symbol_2 = getNodeLinks(node).resolvedSymbol;
+ if (symbol_2.flags & 2097152 /* Alias */) {
+ var declaration = getDeclarationOfAliasSymbol(symbol_2);
+ return !!declaration && declaration.kind === 263 /* NamespaceImport */;
}
}
}
- if (multipleConstructorImplementation) {
- ts.forEach(declarations, function (declaration) {
- error(declaration, ts.Diagnostics.Multiple_constructor_implementations_are_not_allowed);
- });
+ return false;
+ }
+ function checkReferenceExpression(expr, invalidReferenceMessage, invalidOptionalChainMessage) {
+ // References are combinations of identifiers, parentheses, and property accesses.
+ var node = ts.skipOuterExpressions(expr, 6 /* Assertions */ | 1 /* Parentheses */);
+ if (node.kind !== 78 /* Identifier */ && !ts.isAccessExpression(node)) {
+ error(expr, invalidReferenceMessage);
+ return false;
}
- if (duplicateFunctionDeclaration) {
- ts.forEach(declarations, function (declaration) {
- error(ts.getNameOfDeclaration(declaration), ts.Diagnostics.Duplicate_function_implementation);
- });
+ if (node.flags & 32 /* OptionalChain */) {
+ error(expr, invalidOptionalChainMessage);
+ return false;
}
- if (hasNonAmbientClass && !isConstructor && symbol.flags & 16 /* Function */) {
- // A non-ambient class cannot be an implementation for a non-constructor function/class merge
- // TODO: The below just replicates our older error from when classes and functions were
- // entirely unable to merge - a more helpful message like "Class declaration cannot implement overload list"
- // might be warranted. :shrug:
- ts.forEach(declarations, function (declaration) {
- addDuplicateDeclarationError(declaration, ts.Diagnostics.Duplicate_identifier_0, ts.symbolName(symbol), declarations);
- });
+ return true;
+ }
+ function checkDeleteExpression(node) {
+ checkExpression(node.expression);
+ var expr = ts.skipParentheses(node.expression);
+ if (!ts.isAccessExpression(expr)) {
+ error(expr, ts.Diagnostics.The_operand_of_a_delete_operator_must_be_a_property_reference);
+ return booleanType;
}
- // Abstract methods can't have an implementation -- in particular, they don't need one.
- if (lastSeenNonAmbientDeclaration && !lastSeenNonAmbientDeclaration.body &&
- !ts.hasModifier(lastSeenNonAmbientDeclaration, 128 /* Abstract */) && !lastSeenNonAmbientDeclaration.questionToken) {
- reportImplementationExpectedError(lastSeenNonAmbientDeclaration);
+ if (ts.isPropertyAccessExpression(expr) && ts.isPrivateIdentifier(expr.name)) {
+ error(expr, ts.Diagnostics.The_operand_of_a_delete_operator_cannot_be_a_private_identifier);
}
- if (hasOverloads) {
- checkFlagAgreementBetweenOverloads(declarations, bodyDeclaration, flagsToCheck, someNodeFlags, allNodeFlags);
- checkQuestionTokenAgreementBetweenOverloads(declarations, bodyDeclaration, someHaveQuestionToken, allHaveQuestionToken);
- if (bodyDeclaration) {
- var signatures = getSignaturesOfSymbol(symbol);
- var bodySignature = getSignatureFromDeclaration(bodyDeclaration);
- for (var _a = 0, signatures_10 = signatures; _a < signatures_10.length; _a++) {
- var signature = signatures_10[_a];
- if (!isImplementationCompatibleWithOverload(bodySignature, signature)) {
- ts.addRelatedInfo(error(signature.declaration, ts.Diagnostics.This_overload_signature_is_not_compatible_with_its_implementation_signature), ts.createDiagnosticForNode(bodyDeclaration, ts.Diagnostics.The_implementation_signature_is_declared_here));
- break;
- }
- }
+ var links = getNodeLinks(expr);
+ var symbol = getExportSymbolOfValueSymbolIfExported(links.resolvedSymbol);
+ if (symbol) {
+ if (isReadonlySymbol(symbol)) {
+ error(expr, ts.Diagnostics.The_operand_of_a_delete_operator_cannot_be_a_read_only_property);
}
+ checkDeleteExpressionMustBeOptional(expr, getTypeOfSymbol(symbol));
}
+ return booleanType;
}
- function checkExportsOnMergedDeclarations(node) {
- if (!produceDiagnostics) {
- return;
- }
- // if localSymbol is defined on node then node itself is exported - check is required
- var symbol = node.localSymbol;
- if (!symbol) {
- // local symbol is undefined => this declaration is non-exported.
- // however symbol might contain other declarations that are exported
- symbol = getSymbolOfNode(node);
- if (!symbol.exportSymbol) {
- // this is a pure local symbol (all declarations are non-exported) - no need to check anything
- return;
- }
- }
- // run the check only for the first declaration in the list
- if (ts.getDeclarationOfKind(symbol, node.kind) !== node) {
- return;
+ function checkDeleteExpressionMustBeOptional(expr, type) {
+ var AnyOrUnknownOrNeverFlags = 3 /* AnyOrUnknown */ | 131072 /* Never */;
+ if (strictNullChecks && !(type.flags & AnyOrUnknownOrNeverFlags) && !(getFalsyFlags(type) & 32768 /* Undefined */)) {
+ error(expr, ts.Diagnostics.The_operand_of_a_delete_operator_must_be_optional);
}
- var exportedDeclarationSpaces = 0 /* None */;
- var nonExportedDeclarationSpaces = 0 /* None */;
- var defaultExportedDeclarationSpaces = 0 /* None */;
- for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) {
- var d = _a[_i];
- var declarationSpaces = getDeclarationSpaces(d);
- var effectiveDeclarationFlags = getEffectiveDeclarationFlags(d, 1 /* Export */ | 512 /* Default */);
- if (effectiveDeclarationFlags & 1 /* Export */) {
- if (effectiveDeclarationFlags & 512 /* Default */) {
- defaultExportedDeclarationSpaces |= declarationSpaces;
+ }
+ function checkTypeOfExpression(node) {
+ checkExpression(node.expression);
+ return typeofType;
+ }
+ function checkVoidExpression(node) {
+ checkExpression(node.expression);
+ return undefinedWideningType;
+ }
+ function checkAwaitExpression(node) {
+ // Grammar checking
+ if (produceDiagnostics) {
+ if (!(node.flags & 32768 /* AwaitContext */)) {
+ if (ts.isInTopLevelContext(node)) {
+ var sourceFile = ts.getSourceFileOfNode(node);
+ if (!hasParseDiagnostics(sourceFile)) {
+ var span = void 0;
+ if (!ts.isEffectiveExternalModule(sourceFile, compilerOptions)) {
+ if (!span)
+ span = ts.getSpanOfTokenAtPosition(sourceFile, node.pos);
+ var diagnostic = ts.createFileDiagnostic(sourceFile, span.start, span.length, ts.Diagnostics.await_expressions_are_only_allowed_at_the_top_level_of_a_file_when_that_file_is_a_module_but_this_file_has_no_imports_or_exports_Consider_adding_an_empty_export_to_make_this_file_a_module);
+ diagnostics.add(diagnostic);
+ }
+ if ((moduleKind !== ts.ModuleKind.ESNext && moduleKind !== ts.ModuleKind.System) || languageVersion < 4 /* ES2017 */) {
+ span = ts.getSpanOfTokenAtPosition(sourceFile, node.pos);
+ var diagnostic = ts.createFileDiagnostic(sourceFile, span.start, span.length, ts.Diagnostics.Top_level_await_expressions_are_only_allowed_when_the_module_option_is_set_to_esnext_or_system_and_the_target_option_is_set_to_es2017_or_higher);
+ diagnostics.add(diagnostic);
+ }
+ }
}
else {
- exportedDeclarationSpaces |= declarationSpaces;
+ // use of 'await' in non-async function
+ var sourceFile = ts.getSourceFileOfNode(node);
+ if (!hasParseDiagnostics(sourceFile)) {
+ var span = ts.getSpanOfTokenAtPosition(sourceFile, node.pos);
+ var diagnostic = ts.createFileDiagnostic(sourceFile, span.start, span.length, ts.Diagnostics.await_expressions_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules);
+ var func = ts.getContainingFunction(node);
+ if (func && func.kind !== 166 /* Constructor */ && (ts.getFunctionFlags(func) & 2 /* Async */) === 0) {
+ var relatedInfo = ts.createDiagnosticForNode(func, ts.Diagnostics.Did_you_mean_to_mark_this_function_as_async);
+ ts.addRelatedInfo(diagnostic, relatedInfo);
+ }
+ diagnostics.add(diagnostic);
+ }
}
}
- else {
- nonExportedDeclarationSpaces |= declarationSpaces;
+ if (isInParameterInitializerBeforeContainingFunction(node)) {
+ error(node, ts.Diagnostics.await_expressions_cannot_be_used_in_a_parameter_initializer);
}
}
- // Spaces for anything not declared a 'default export'.
- var nonDefaultExportedDeclarationSpaces = exportedDeclarationSpaces | nonExportedDeclarationSpaces;
- var commonDeclarationSpacesForExportsAndLocals = exportedDeclarationSpaces & nonExportedDeclarationSpaces;
- var commonDeclarationSpacesForDefaultAndNonDefault = defaultExportedDeclarationSpaces & nonDefaultExportedDeclarationSpaces;
- if (commonDeclarationSpacesForExportsAndLocals || commonDeclarationSpacesForDefaultAndNonDefault) {
- // declaration spaces for exported and non-exported declarations intersect
- for (var _b = 0, _c = symbol.declarations; _b < _c.length; _b++) {
- var d = _c[_b];
- var declarationSpaces = getDeclarationSpaces(d);
- var name = ts.getNameOfDeclaration(d);
- // Only error on the declarations that contributed to the intersecting spaces.
- if (declarationSpaces & commonDeclarationSpacesForDefaultAndNonDefault) {
- error(name, ts.Diagnostics.Merged_declaration_0_cannot_include_a_default_export_declaration_Consider_adding_a_separate_export_default_0_declaration_instead, ts.declarationNameToString(name));
+ var operandType = checkExpression(node.expression);
+ var awaitedType = checkAwaitedType(operandType, node, ts.Diagnostics.Type_of_await_operand_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member);
+ if (awaitedType === operandType && awaitedType !== errorType && !(operandType.flags & 3 /* AnyOrUnknown */)) {
+ addErrorOrSuggestion(/*isError*/ false, ts.createDiagnosticForNode(node, ts.Diagnostics.await_has_no_effect_on_the_type_of_this_expression));
+ }
+ return awaitedType;
+ }
+ function checkPrefixUnaryExpression(node) {
+ var operandType = checkExpression(node.operand);
+ if (operandType === silentNeverType) {
+ return silentNeverType;
+ }
+ switch (node.operand.kind) {
+ case 8 /* NumericLiteral */:
+ switch (node.operator) {
+ case 40 /* MinusToken */:
+ return getFreshTypeOfLiteralType(getLiteralType(-node.operand.text));
+ case 39 /* PlusToken */:
+ return getFreshTypeOfLiteralType(getLiteralType(+node.operand.text));
}
- else if (declarationSpaces & commonDeclarationSpacesForExportsAndLocals) {
- error(name, ts.Diagnostics.Individual_declarations_in_merged_declaration_0_must_be_all_exported_or_all_local, ts.declarationNameToString(name));
+ break;
+ case 9 /* BigIntLiteral */:
+ if (node.operator === 40 /* MinusToken */) {
+ return getFreshTypeOfLiteralType(getLiteralType({
+ negative: true,
+ base10Value: ts.parsePseudoBigInt(node.operand.text)
+ }));
}
- }
}
- function getDeclarationSpaces(decl) {
- var d = decl;
- switch (d.kind) {
- case 246 /* InterfaceDeclaration */:
- case 247 /* TypeAliasDeclaration */:
- // A jsdoc typedef and callback are, by definition, type aliases.
- // falls through
- case 322 /* JSDocTypedefTag */:
- case 315 /* JSDocCallbackTag */:
- case 316 /* JSDocEnumTag */:
- return 2 /* ExportType */;
- case 249 /* ModuleDeclaration */:
- return ts.isAmbientModule(d) || ts.getModuleInstanceState(d) !== 0 /* NonInstantiated */
- ? 4 /* ExportNamespace */ | 1 /* ExportValue */
- : 4 /* ExportNamespace */;
- case 245 /* ClassDeclaration */:
- case 248 /* EnumDeclaration */:
- case 284 /* EnumMember */:
- return 2 /* ExportType */ | 1 /* ExportValue */;
- case 290 /* SourceFile */:
- return 2 /* ExportType */ | 1 /* ExportValue */ | 4 /* ExportNamespace */;
- case 259 /* ExportAssignment */:
- // Export assigned entity name expressions act as aliases and should fall through, otherwise they export values
- if (!ts.isEntityNameExpression(d.expression)) {
- return 1 /* ExportValue */;
+ switch (node.operator) {
+ case 39 /* PlusToken */:
+ case 40 /* MinusToken */:
+ case 54 /* TildeToken */:
+ checkNonNullType(operandType, node.operand);
+ if (maybeTypeOfKind(operandType, 12288 /* ESSymbolLike */)) {
+ error(node.operand, ts.Diagnostics.The_0_operator_cannot_be_applied_to_type_symbol, ts.tokenToString(node.operator));
+ }
+ if (node.operator === 39 /* PlusToken */) {
+ if (maybeTypeOfKind(operandType, 2112 /* BigIntLike */)) {
+ error(node.operand, ts.Diagnostics.Operator_0_cannot_be_applied_to_type_1, ts.tokenToString(node.operator), typeToString(getBaseTypeOfLiteralType(operandType)));
}
- d = d.expression;
- // The below options all declare an Alias, which is allowed to merge with other values within the importing module.
- // falls through
- case 253 /* ImportEqualsDeclaration */:
- case 256 /* NamespaceImport */:
- case 255 /* ImportClause */:
- var result_10 = 0 /* None */;
- var target = resolveAlias(getSymbolOfNode(d));
- ts.forEach(target.declarations, function (d) { result_10 |= getDeclarationSpaces(d); });
- return result_10;
- case 242 /* VariableDeclaration */:
- case 191 /* BindingElement */:
- case 244 /* FunctionDeclaration */:
- case 258 /* ImportSpecifier */: // https://github.com/Microsoft/TypeScript/pull/7591
- case 75 /* Identifier */: // https://github.com/microsoft/TypeScript/issues/36098
- // Identifiers are used as declarations of assignment declarations whose parents may be
- // SyntaxKind.CallExpression - `Object.defineProperty(thing, "aField", {value: 42});`
- // SyntaxKind.ElementAccessExpression - `thing["aField"] = 42;` or `thing["aField"];` (with a doc comment on it)
- // or SyntaxKind.PropertyAccessExpression - `thing.aField = 42;`
- // all of which are pretty much always values, or at least imply a value meaning.
- // It may be apprpriate to treat these as aliases in the future.
- return 1 /* ExportValue */;
- default:
- return ts.Debug.failBadSyntaxKind(d);
- }
+ return numberType;
+ }
+ return getUnaryResultType(operandType);
+ case 53 /* ExclamationToken */:
+ checkTruthinessExpression(node.operand);
+ var facts = getTypeFacts(operandType) & (4194304 /* Truthy */ | 8388608 /* Falsy */);
+ return facts === 4194304 /* Truthy */ ? falseType :
+ facts === 8388608 /* Falsy */ ? trueType :
+ booleanType;
+ case 45 /* PlusPlusToken */:
+ case 46 /* MinusMinusToken */:
+ var ok = checkArithmeticOperandType(node.operand, checkNonNullType(operandType, node.operand), ts.Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_bigint_or_an_enum_type);
+ if (ok) {
+ // run check only if former checks succeeded to avoid reporting cascading errors
+ checkReferenceExpression(node.operand, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_or_a_property_access, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_may_not_be_an_optional_property_access);
+ }
+ return getUnaryResultType(operandType);
}
+ return errorType;
}
- function getAwaitedTypeOfPromise(type, errorNode, diagnosticMessage, arg0) {
- var promisedType = getPromisedTypeOfPromise(type, errorNode);
- return promisedType && getAwaitedType(promisedType, errorNode, diagnosticMessage, arg0);
- }
- /**
- * Gets the "promised type" of a promise.
- * @param type The type of the promise.
- * @remarks The "promised type" of a type is the type of the "value" parameter of the "onfulfilled" callback.
- */
- function getPromisedTypeOfPromise(type, errorNode) {
- //
- // { // type
- // then( // thenFunction
- // onfulfilled: ( // onfulfilledParameterType
- // value: T // valueParameterType
- // ) => any
- // ): any;
- // }
- //
- if (isTypeAny(type)) {
- return undefined;
+ function checkPostfixUnaryExpression(node) {
+ var operandType = checkExpression(node.operand);
+ if (operandType === silentNeverType) {
+ return silentNeverType;
}
- var typeAsPromise = type;
- if (typeAsPromise.promisedTypeOfPromise) {
- return typeAsPromise.promisedTypeOfPromise;
+ var ok = checkArithmeticOperandType(node.operand, checkNonNullType(operandType, node.operand), ts.Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_bigint_or_an_enum_type);
+ if (ok) {
+ // run check only if former checks succeeded to avoid reporting cascading errors
+ checkReferenceExpression(node.operand, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_or_a_property_access, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_may_not_be_an_optional_property_access);
}
- if (isReferenceToType(type, getGlobalPromiseType(/*reportErrors*/ false))) {
- return typeAsPromise.promisedTypeOfPromise = getTypeArguments(type)[0];
+ return getUnaryResultType(operandType);
+ }
+ function getUnaryResultType(operandType) {
+ if (maybeTypeOfKind(operandType, 2112 /* BigIntLike */)) {
+ return isTypeAssignableToKind(operandType, 3 /* AnyOrUnknown */) || maybeTypeOfKind(operandType, 296 /* NumberLike */)
+ ? numberOrBigIntType
+ : bigintType;
}
- var thenFunction = getTypeOfPropertyOfType(type, "then"); // TODO: GH#18217
- if (isTypeAny(thenFunction)) {
- return undefined;
+ // If it's not a bigint type, implicit coercion will result in a number
+ return numberType;
+ }
+ // Return true if type might be of the given kind. A union or intersection type might be of a given
+ // kind if at least one constituent type is of the given kind.
+ function maybeTypeOfKind(type, kind) {
+ if (type.flags & kind) {
+ return true;
}
- var thenSignatures = thenFunction ? getSignaturesOfType(thenFunction, 0 /* Call */) : ts.emptyArray;
- if (thenSignatures.length === 0) {
- if (errorNode) {
- error(errorNode, ts.Diagnostics.A_promise_must_have_a_then_method);
+ if (type.flags & 3145728 /* UnionOrIntersection */) {
+ var types = type.types;
+ for (var _i = 0, types_20 = types; _i < types_20.length; _i++) {
+ var t = types_20[_i];
+ if (maybeTypeOfKind(t, kind)) {
+ return true;
+ }
}
- return undefined;
}
- var onfulfilledParameterType = getTypeWithFacts(getUnionType(ts.map(thenSignatures, getTypeOfFirstParameterOfSignature)), 2097152 /* NEUndefinedOrNull */);
- if (isTypeAny(onfulfilledParameterType)) {
- return undefined;
+ return false;
+ }
+ function isTypeAssignableToKind(source, kind, strict) {
+ if (source.flags & kind) {
+ return true;
}
- var onfulfilledParameterSignatures = getSignaturesOfType(onfulfilledParameterType, 0 /* Call */);
- if (onfulfilledParameterSignatures.length === 0) {
- if (errorNode) {
- error(errorNode, ts.Diagnostics.The_first_parameter_of_the_then_method_of_a_promise_must_be_a_callback);
- }
- return undefined;
+ if (strict && source.flags & (3 /* AnyOrUnknown */ | 16384 /* Void */ | 32768 /* Undefined */ | 65536 /* Null */)) {
+ return false;
}
- return typeAsPromise.promisedTypeOfPromise = getUnionType(ts.map(onfulfilledParameterSignatures, getTypeOfFirstParameterOfSignature), 2 /* Subtype */);
+ return !!(kind & 296 /* NumberLike */) && isTypeAssignableTo(source, numberType) ||
+ !!(kind & 2112 /* BigIntLike */) && isTypeAssignableTo(source, bigintType) ||
+ !!(kind & 402653316 /* StringLike */) && isTypeAssignableTo(source, stringType) ||
+ !!(kind & 528 /* BooleanLike */) && isTypeAssignableTo(source, booleanType) ||
+ !!(kind & 16384 /* Void */) && isTypeAssignableTo(source, voidType) ||
+ !!(kind & 131072 /* Never */) && isTypeAssignableTo(source, neverType) ||
+ !!(kind & 65536 /* Null */) && isTypeAssignableTo(source, nullType) ||
+ !!(kind & 32768 /* Undefined */) && isTypeAssignableTo(source, undefinedType) ||
+ !!(kind & 4096 /* ESSymbol */) && isTypeAssignableTo(source, esSymbolType) ||
+ !!(kind & 67108864 /* NonPrimitive */) && isTypeAssignableTo(source, nonPrimitiveType);
}
- /**
- * Gets the "awaited type" of a type.
- * @param type The type to await.
- * @remarks The "awaited type" of an expression is its "promised type" if the expression is a
- * Promise-like type; otherwise, it is the type of the expression. This is used to reflect
- * The runtime behavior of the `await` keyword.
- */
- function checkAwaitedType(type, errorNode, diagnosticMessage, arg0) {
- var awaitedType = getAwaitedType(type, errorNode, diagnosticMessage, arg0);
- return awaitedType || errorType;
+ function allTypesAssignableToKind(source, kind, strict) {
+ return source.flags & 1048576 /* Union */ ?
+ ts.every(source.types, function (subType) { return allTypesAssignableToKind(subType, kind, strict); }) :
+ isTypeAssignableToKind(source, kind, strict);
}
- /**
- * Determines whether a type has a callable `then` member.
- */
- function isThenableType(type) {
- var thenFunction = getTypeOfPropertyOfType(type, "then");
- return !!thenFunction && getSignaturesOfType(getTypeWithFacts(thenFunction, 2097152 /* NEUndefinedOrNull */), 0 /* Call */).length > 0;
+ function isConstEnumObjectType(type) {
+ return !!(ts.getObjectFlags(type) & 16 /* Anonymous */) && !!type.symbol && isConstEnumSymbol(type.symbol);
}
- /**
- * Gets the "awaited type" of a type.
- *
- * The "awaited type" of an expression is its "promised type" if the expression is a
- * Promise-like type; otherwise, it is the type of the expression. If the "promised
- * type" is itself a Promise-like, the "promised type" is recursively unwrapped until a
- * non-promise type is found.
- *
- * This is used to reflect the runtime behavior of the `await` keyword.
- */
- function getAwaitedType(type, errorNode, diagnosticMessage, arg0) {
- if (isTypeAny(type)) {
- return type;
+ function isConstEnumSymbol(symbol) {
+ return (symbol.flags & 128 /* ConstEnum */) !== 0;
+ }
+ function checkInstanceOfExpression(left, right, leftType, rightType) {
+ if (leftType === silentNeverType || rightType === silentNeverType) {
+ return silentNeverType;
}
- var typeAsAwaitable = type;
- if (typeAsAwaitable.awaitedTypeOfType) {
- return typeAsAwaitable.awaitedTypeOfType;
+ // TypeScript 1.0 spec (April 2014): 4.15.4
+ // The instanceof operator requires the left operand to be of type Any, an object type, or a type parameter type,
+ // and the right operand to be of type Any, a subtype of the 'Function' interface type, or have a call or construct signature.
+ // The result is always of the Boolean primitive type.
+ // NOTE: do not raise error if leftType is unknown as related error was already reported
+ if (!isTypeAny(leftType) &&
+ allTypesAssignableToKind(leftType, 131068 /* Primitive */)) {
+ error(left, ts.Diagnostics.The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
}
- // For a union, get a union of the awaited types of each constituent.
- //
- return typeAsAwaitable.awaitedTypeOfType =
- mapType(type, errorNode ? function (constituentType) { return getAwaitedTypeWorker(constituentType, errorNode, diagnosticMessage, arg0); } : getAwaitedTypeWorker);
+ // NOTE: do not raise error if right is unknown as related error was already reported
+ if (!(isTypeAny(rightType) || typeHasCallOrConstructSignatures(rightType) || isTypeSubtypeOf(rightType, globalFunctionType))) {
+ error(right, ts.Diagnostics.The_right_hand_side_of_an_instanceof_expression_must_be_of_type_any_or_of_a_type_assignable_to_the_Function_interface_type);
+ }
+ return booleanType;
}
- function getAwaitedTypeWorker(type, errorNode, diagnosticMessage, arg0) {
- var typeAsAwaitable = type;
- if (typeAsAwaitable.awaitedTypeOfType) {
- return typeAsAwaitable.awaitedTypeOfType;
+ function checkInExpression(left, right, leftType, rightType) {
+ if (leftType === silentNeverType || rightType === silentNeverType) {
+ return silentNeverType;
}
- var promisedType = getPromisedTypeOfPromise(type);
- if (promisedType) {
- if (type.id === promisedType.id || awaitedTypeStack.lastIndexOf(promisedType.id) >= 0) {
- // Verify that we don't have a bad actor in the form of a promise whose
- // promised type is the same as the promise type, or a mutually recursive
- // promise. If so, we return undefined as we cannot guess the shape. If this
- // were the actual case in the JavaScript, this Promise would never resolve.
- //
- // An example of a bad actor with a singly-recursive promise type might
- // be:
- //
- // interface BadPromise {
- // then(
- // onfulfilled: (value: BadPromise) => any,
- // onrejected: (error: any) => any): BadPromise;
- // }
- //
- // The above interface will pass the PromiseLike check, and return a
- // promised type of `BadPromise`. Since this is a self reference, we
- // don't want to keep recursing ad infinitum.
- //
- // An example of a bad actor in the form of a mutually-recursive
- // promise type might be:
- //
- // interface BadPromiseA {
- // then(
- // onfulfilled: (value: BadPromiseB) => any,
- // onrejected: (error: any) => any): BadPromiseB;
- // }
- //
- // interface BadPromiseB {
- // then(
- // onfulfilled: (value: BadPromiseA) => any,
- // onrejected: (error: any) => any): BadPromiseA;
- // }
- //
- if (errorNode) {
- error(errorNode, ts.Diagnostics.Type_is_referenced_directly_or_indirectly_in_the_fulfillment_callback_of_its_own_then_method);
+ leftType = checkNonNullType(leftType, left);
+ rightType = checkNonNullType(rightType, right);
+ // TypeScript 1.0 spec (April 2014): 4.15.5
+ // The in operator requires the left operand to be of type Any, the String primitive type, or the Number primitive type,
+ // and the right operand to be of type Any, an object type, or a type parameter type.
+ // The result is always of the Boolean primitive type.
+ if (!(allTypesAssignableToKind(leftType, 402653316 /* StringLike */ | 296 /* NumberLike */ | 12288 /* ESSymbolLike */) ||
+ isTypeAssignableToKind(leftType, 4194304 /* Index */ | 134217728 /* TemplateLiteral */ | 268435456 /* StringMapping */ | 262144 /* TypeParameter */))) {
+ error(left, ts.Diagnostics.The_left_hand_side_of_an_in_expression_must_be_of_type_any_string_number_or_symbol);
+ }
+ if (!allTypesAssignableToKind(rightType, 67108864 /* NonPrimitive */ | 58982400 /* InstantiableNonPrimitive */)) {
+ error(right, ts.Diagnostics.The_right_hand_side_of_an_in_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
+ }
+ return booleanType;
+ }
+ function checkObjectLiteralAssignment(node, sourceType, rightIsThis) {
+ var properties = node.properties;
+ if (strictNullChecks && properties.length === 0) {
+ return checkNonNullType(sourceType, node);
+ }
+ for (var i = 0; i < properties.length; i++) {
+ checkObjectLiteralDestructuringPropertyAssignment(node, sourceType, i, properties, rightIsThis);
+ }
+ return sourceType;
+ }
+ /** Note: If property cannot be a SpreadAssignment, then allProperties does not need to be provided */
+ function checkObjectLiteralDestructuringPropertyAssignment(node, objectLiteralType, propertyIndex, allProperties, rightIsThis) {
+ if (rightIsThis === void 0) { rightIsThis = false; }
+ var properties = node.properties;
+ var property = properties[propertyIndex];
+ if (property.kind === 288 /* PropertyAssignment */ || property.kind === 289 /* ShorthandPropertyAssignment */) {
+ var name = property.name;
+ var exprType = getLiteralTypeFromPropertyName(name);
+ if (isTypeUsableAsPropertyName(exprType)) {
+ var text = getPropertyNameFromType(exprType);
+ var prop = getPropertyOfType(objectLiteralType, text);
+ if (prop) {
+ markPropertyAsReferenced(prop, property, rightIsThis);
+ checkPropertyAccessibility(property, /*isSuper*/ false, objectLiteralType, prop);
}
- return undefined;
- }
- // Keep track of the type we're about to unwrap to avoid bad recursive promise types.
- // See the comments above for more information.
- awaitedTypeStack.push(type.id);
- var awaitedType = getAwaitedType(promisedType, errorNode, diagnosticMessage, arg0);
- awaitedTypeStack.pop();
- if (!awaitedType) {
- return undefined;
}
- return typeAsAwaitable.awaitedTypeOfType = awaitedType;
+ var elementType = getIndexedAccessType(objectLiteralType, exprType, /*noUncheckedIndexedAccessCandidate*/ undefined, name, /*aliasSymbol*/ undefined, /*aliasTypeArguments*/ undefined, 16 /* ExpressionPosition */);
+ var type = getFlowTypeOfDestructuring(property, elementType);
+ return checkDestructuringAssignment(property.kind === 289 /* ShorthandPropertyAssignment */ ? property : property.initializer, type);
}
- // The type was not a promise, so it could not be unwrapped any further.
- // As long as the type does not have a callable "then" property, it is
- // safe to return the type; otherwise, an error is reported and we return
- // undefined.
- //
- // An example of a non-promise "thenable" might be:
- //
- // await { then(): void {} }
- //
- // The "thenable" does not match the minimal definition for a promise. When
- // a Promise/A+-compatible or ES6 promise tries to adopt this value, the promise
- // will never settle. We treat this as an error to help flag an early indicator
- // of a runtime problem. If the user wants to return this value from an async
- // function, they would need to wrap it in some other value. If they want it to
- // be treated as a promise, they can cast to .
- if (isThenableType(type)) {
- if (errorNode) {
- if (!diagnosticMessage)
- return ts.Debug.fail();
- error(errorNode, diagnosticMessage, arg0);
+ else if (property.kind === 290 /* SpreadAssignment */) {
+ if (propertyIndex < properties.length - 1) {
+ error(property, ts.Diagnostics.A_rest_element_must_be_last_in_a_destructuring_pattern);
+ }
+ else {
+ if (languageVersion < 99 /* ESNext */) {
+ checkExternalEmitHelpers(property, 4 /* Rest */);
+ }
+ var nonRestNames = [];
+ if (allProperties) {
+ for (var _i = 0, allProperties_1 = allProperties; _i < allProperties_1.length; _i++) {
+ var otherProperty = allProperties_1[_i];
+ if (!ts.isSpreadAssignment(otherProperty)) {
+ nonRestNames.push(otherProperty.name);
+ }
+ }
+ }
+ var type = getRestType(objectLiteralType, nonRestNames, objectLiteralType.symbol);
+ checkGrammarForDisallowedTrailingComma(allProperties, ts.Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma);
+ return checkDestructuringAssignment(property.expression, type);
}
- return undefined;
}
- return typeAsAwaitable.awaitedTypeOfType = type;
+ else {
+ error(property, ts.Diagnostics.Property_assignment_expected);
+ }
}
- /**
- * Checks the return type of an async function to ensure it is a compatible
- * Promise implementation.
- *
- * This checks that an async function has a valid Promise-compatible return type.
- * An async function has a valid Promise-compatible return type if the resolved value
- * of the return type has a construct signature that takes in an `initializer` function
- * that in turn supplies a `resolve` function as one of its arguments and results in an
- * object with a callable `then` signature.
- *
- * @param node The signature to check
- */
- function checkAsyncFunctionReturnType(node, returnTypeNode) {
- // As part of our emit for an async function, we will need to emit the entity name of
- // the return type annotation as an expression. To meet the necessary runtime semantics
- // for __awaiter, we must also check that the type of the declaration (e.g. the static
- // side or "constructor" of the promise type) is compatible `PromiseConstructorLike`.
- //
- // An example might be (from lib.es6.d.ts):
- //
- // interface Promise { ... }
- // interface PromiseConstructor {
- // new (...): Promise;
- // }
- // declare var Promise: PromiseConstructor;
- //
- // When an async function declares a return type annotation of `Promise`, we
- // need to get the type of the `Promise` variable declaration above, which would
- // be `PromiseConstructor`.
- //
- // The same case applies to a class:
- //
- // declare class Promise {
- // constructor(...);
- // then(...): Promise;
- // }
- //
- var returnType = getTypeFromTypeNode(returnTypeNode);
- if (languageVersion >= 2 /* ES2015 */) {
- if (returnType === errorType) {
- return;
- }
- var globalPromiseType = getGlobalPromiseType(/*reportErrors*/ true);
- if (globalPromiseType !== emptyGenericType && !isReferenceToType(returnType, globalPromiseType)) {
- // The promise type was not a valid type reference to the global promise type, so we
- // report an error and return the unknown type.
- error(returnTypeNode, ts.Diagnostics.The_return_type_of_an_async_function_or_method_must_be_the_global_Promise_T_type);
- return;
+ function checkArrayLiteralAssignment(node, sourceType, checkMode) {
+ var elements = node.elements;
+ if (languageVersion < 2 /* ES2015 */ && compilerOptions.downlevelIteration) {
+ checkExternalEmitHelpers(node, 512 /* Read */);
+ }
+ // This elementType will be used if the specific property corresponding to this index is not
+ // present (aka the tuple element property). This call also checks that the parentType is in
+ // fact an iterable or array (depending on target language).
+ var possiblyOutOfBoundsType = checkIteratedTypeOrElementType(65 /* Destructuring */ | 128 /* PossiblyOutOfBounds */, sourceType, undefinedType, node) || errorType;
+ var inBoundsType = compilerOptions.noUncheckedIndexedAccess ? undefined : possiblyOutOfBoundsType;
+ for (var i = 0; i < elements.length; i++) {
+ var type = possiblyOutOfBoundsType;
+ if (node.elements[i].kind === 220 /* SpreadElement */) {
+ type = inBoundsType = inBoundsType !== null && inBoundsType !== void 0 ? inBoundsType : (checkIteratedTypeOrElementType(65 /* Destructuring */, sourceType, undefinedType, node) || errorType);
}
+ checkArrayLiteralDestructuringElementAssignment(node, sourceType, i, type, checkMode);
}
- else {
- // Always mark the type node as referenced if it points to a value
- markTypeNodeAsReferenced(returnTypeNode);
- if (returnType === errorType) {
- return;
+ return sourceType;
+ }
+ function checkArrayLiteralDestructuringElementAssignment(node, sourceType, elementIndex, elementType, checkMode) {
+ var elements = node.elements;
+ var element = elements[elementIndex];
+ if (element.kind !== 222 /* OmittedExpression */) {
+ if (element.kind !== 220 /* SpreadElement */) {
+ var indexType = getLiteralType(elementIndex);
+ if (isArrayLikeType(sourceType)) {
+ // We create a synthetic expression so that getIndexedAccessType doesn't get confused
+ // when the element is a SyntaxKind.ElementAccessExpression.
+ var accessFlags = 16 /* ExpressionPosition */ | (hasDefaultValue(element) ? 8 /* NoTupleBoundsCheck */ : 0);
+ var elementType_2 = getIndexedAccessTypeOrUndefined(sourceType, indexType, /*noUncheckedIndexedAccessCandidate*/ undefined, createSyntheticExpression(element, indexType), accessFlags) || errorType;
+ var assignedType = hasDefaultValue(element) ? getTypeWithFacts(elementType_2, 524288 /* NEUndefined */) : elementType_2;
+ var type = getFlowTypeOfDestructuring(element, assignedType);
+ return checkDestructuringAssignment(element, type, checkMode);
+ }
+ return checkDestructuringAssignment(element, elementType, checkMode);
}
- var promiseConstructorName = ts.getEntityNameFromTypeNode(returnTypeNode);
- if (promiseConstructorName === undefined) {
- error(returnTypeNode, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type_in_ES5_SlashES3_because_it_does_not_refer_to_a_Promise_compatible_constructor_value, typeToString(returnType));
- return;
+ if (elementIndex < elements.length - 1) {
+ error(element, ts.Diagnostics.A_rest_element_must_be_last_in_a_destructuring_pattern);
}
- var promiseConstructorSymbol = resolveEntityName(promiseConstructorName, 111551 /* Value */, /*ignoreErrors*/ true);
- var promiseConstructorType = promiseConstructorSymbol ? getTypeOfSymbol(promiseConstructorSymbol) : errorType;
- if (promiseConstructorType === errorType) {
- if (promiseConstructorName.kind === 75 /* Identifier */ && promiseConstructorName.escapedText === "Promise" && getTargetType(returnType) === getGlobalPromiseType(/*reportErrors*/ false)) {
- error(returnTypeNode, ts.Diagnostics.An_async_function_or_method_in_ES5_SlashES3_requires_the_Promise_constructor_Make_sure_you_have_a_declaration_for_the_Promise_constructor_or_include_ES2015_in_your_lib_option);
+ else {
+ var restExpression = element.expression;
+ if (restExpression.kind === 216 /* BinaryExpression */ && restExpression.operatorToken.kind === 62 /* EqualsToken */) {
+ error(restExpression.operatorToken, ts.Diagnostics.A_rest_element_cannot_have_an_initializer);
}
else {
- error(returnTypeNode, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type_in_ES5_SlashES3_because_it_does_not_refer_to_a_Promise_compatible_constructor_value, ts.entityNameToString(promiseConstructorName));
+ checkGrammarForDisallowedTrailingComma(node.elements, ts.Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma);
+ var type = everyType(sourceType, isTupleType) ?
+ mapType(sourceType, function (t) { return sliceTupleType(t, elementIndex); }) :
+ createArrayType(elementType);
+ return checkDestructuringAssignment(restExpression, type, checkMode);
}
- return;
- }
- var globalPromiseConstructorLikeType = getGlobalPromiseConstructorLikeType(/*reportErrors*/ true);
- if (globalPromiseConstructorLikeType === emptyObjectType) {
- // If we couldn't resolve the global PromiseConstructorLike type we cannot verify
- // compatibility with __awaiter.
- error(returnTypeNode, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type_in_ES5_SlashES3_because_it_does_not_refer_to_a_Promise_compatible_constructor_value, ts.entityNameToString(promiseConstructorName));
- return;
- }
- if (!checkTypeAssignableTo(promiseConstructorType, globalPromiseConstructorLikeType, returnTypeNode, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type_in_ES5_SlashES3_because_it_does_not_refer_to_a_Promise_compatible_constructor_value)) {
- return;
- }
- // Verify there is no local declaration that could collide with the promise constructor.
- var rootName = promiseConstructorName && ts.getFirstIdentifier(promiseConstructorName);
- var collidingSymbol = getSymbol(node.locals, rootName.escapedText, 111551 /* Value */);
- if (collidingSymbol) {
- error(collidingSymbol.valueDeclaration, ts.Diagnostics.Duplicate_identifier_0_Compiler_uses_declaration_1_to_support_async_functions, ts.idText(rootName), ts.entityNameToString(promiseConstructorName));
- return;
}
}
- checkAwaitedType(returnType, node, ts.Diagnostics.The_return_type_of_an_async_function_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member);
+ return undefined;
}
- /** Check a decorator */
- function checkDecorator(node) {
- var signature = getResolvedSignature(node);
- var returnType = getReturnTypeOfSignature(signature);
- if (returnType.flags & 1 /* Any */) {
- return;
+ function checkDestructuringAssignment(exprOrAssignment, sourceType, checkMode, rightIsThis) {
+ var target;
+ if (exprOrAssignment.kind === 289 /* ShorthandPropertyAssignment */) {
+ var prop = exprOrAssignment;
+ if (prop.objectAssignmentInitializer) {
+ // In strict null checking mode, if a default value of a non-undefined type is specified, remove
+ // undefined from the final type.
+ if (strictNullChecks &&
+ !(getFalsyFlags(checkExpression(prop.objectAssignmentInitializer)) & 32768 /* Undefined */)) {
+ sourceType = getTypeWithFacts(sourceType, 524288 /* NEUndefined */);
+ }
+ checkBinaryLikeExpression(prop.name, prop.equalsToken, prop.objectAssignmentInitializer, checkMode);
+ }
+ target = exprOrAssignment.name;
}
- var expectedReturnType;
- var headMessage = getDiagnosticHeadMessageForDecoratorResolution(node);
- var errorInfo;
- switch (node.parent.kind) {
- case 245 /* ClassDeclaration */:
- var classSymbol = getSymbolOfNode(node.parent);
- var classConstructorType = getTypeOfSymbol(classSymbol);
- expectedReturnType = getUnionType([classConstructorType, voidType]);
- break;
- case 156 /* Parameter */:
- expectedReturnType = voidType;
- errorInfo = ts.chainDiagnosticMessages(
- /*details*/ undefined, ts.Diagnostics.The_return_type_of_a_parameter_decorator_function_must_be_either_void_or_any);
- break;
- case 159 /* PropertyDeclaration */:
- expectedReturnType = voidType;
- errorInfo = ts.chainDiagnosticMessages(
- /*details*/ undefined, ts.Diagnostics.The_return_type_of_a_property_decorator_function_must_be_either_void_or_any);
- break;
- case 161 /* MethodDeclaration */:
- case 163 /* GetAccessor */:
- case 164 /* SetAccessor */:
- var methodType = getTypeOfNode(node.parent);
- var descriptorType = createTypedPropertyDescriptorType(methodType);
- expectedReturnType = getUnionType([descriptorType, voidType]);
- break;
- default:
- return ts.Debug.fail();
+ else {
+ target = exprOrAssignment;
}
- checkTypeAssignableTo(returnType, expectedReturnType, node, headMessage, function () { return errorInfo; });
- }
- /**
- * If a TypeNode can be resolved to a value symbol imported from an external module, it is
- * marked as referenced to prevent import elision.
- */
- function markTypeNodeAsReferenced(node) {
- markEntityNameOrEntityExpressionAsReference(node && ts.getEntityNameFromTypeNode(node));
+ if (target.kind === 216 /* BinaryExpression */ && target.operatorToken.kind === 62 /* EqualsToken */) {
+ checkBinaryExpression(target, checkMode);
+ target = target.left;
+ }
+ if (target.kind === 200 /* ObjectLiteralExpression */) {
+ return checkObjectLiteralAssignment(target, sourceType, rightIsThis);
+ }
+ if (target.kind === 199 /* ArrayLiteralExpression */) {
+ return checkArrayLiteralAssignment(target, sourceType, checkMode);
+ }
+ return checkReferenceAssignment(target, sourceType, checkMode);
}
- function markEntityNameOrEntityExpressionAsReference(typeName) {
- if (!typeName)
- return;
- var rootName = ts.getFirstIdentifier(typeName);
- var meaning = (typeName.kind === 75 /* Identifier */ ? 788968 /* Type */ : 1920 /* Namespace */) | 2097152 /* Alias */;
- var rootSymbol = resolveName(rootName, rootName.escapedText, meaning, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isRefernce*/ true);
- if (rootSymbol
- && rootSymbol.flags & 2097152 /* Alias */
- && symbolIsValue(rootSymbol)
- && !isConstEnumOrConstEnumOnlyModule(resolveAlias(rootSymbol))
- && !getTypeOnlyAliasDeclaration(rootSymbol)) {
- markAliasSymbolAsReferenced(rootSymbol);
+ function checkReferenceAssignment(target, sourceType, checkMode) {
+ var targetType = checkExpression(target, checkMode);
+ var error = target.parent.kind === 290 /* SpreadAssignment */ ?
+ ts.Diagnostics.The_target_of_an_object_rest_assignment_must_be_a_variable_or_a_property_access :
+ ts.Diagnostics.The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access;
+ var optionalError = target.parent.kind === 290 /* SpreadAssignment */ ?
+ ts.Diagnostics.The_target_of_an_object_rest_assignment_may_not_be_an_optional_property_access :
+ ts.Diagnostics.The_left_hand_side_of_an_assignment_expression_may_not_be_an_optional_property_access;
+ if (checkReferenceExpression(target, error, optionalError)) {
+ checkTypeAssignableToAndOptionallyElaborate(sourceType, targetType, target, target);
+ }
+ if (ts.isPrivateIdentifierPropertyAccessExpression(target)) {
+ checkExternalEmitHelpers(target.parent, 2097152 /* ClassPrivateFieldSet */);
}
+ return sourceType;
}
/**
- * This function marks the type used for metadata decorator as referenced if it is import
- * from external module.
- * This is different from markTypeNodeAsReferenced because it tries to simplify type nodes in
- * union and intersection type
- * @param node
+ * This is a *shallow* check: An expression is side-effect-free if the
+ * evaluation of the expression *itself* cannot produce side effects.
+ * For example, x++ / 3 is side-effect free because the / operator
+ * does not have side effects.
+ * The intent is to "smell test" an expression for correctness in positions where
+ * its value is discarded (e.g. the left side of the comma operator).
*/
- function markDecoratorMedataDataTypeNodeAsReferenced(node) {
- var entityName = getEntityNameForDecoratorMetadata(node);
- if (entityName && ts.isEntityName(entityName)) {
- markEntityNameOrEntityExpressionAsReference(entityName);
+ function isSideEffectFree(node) {
+ node = ts.skipParentheses(node);
+ switch (node.kind) {
+ case 78 /* Identifier */:
+ case 10 /* StringLiteral */:
+ case 13 /* RegularExpressionLiteral */:
+ case 205 /* TaggedTemplateExpression */:
+ case 218 /* TemplateExpression */:
+ case 14 /* NoSubstitutionTemplateLiteral */:
+ case 8 /* NumericLiteral */:
+ case 9 /* BigIntLiteral */:
+ case 109 /* TrueKeyword */:
+ case 94 /* FalseKeyword */:
+ case 103 /* NullKeyword */:
+ case 150 /* UndefinedKeyword */:
+ case 208 /* FunctionExpression */:
+ case 221 /* ClassExpression */:
+ case 209 /* ArrowFunction */:
+ case 199 /* ArrayLiteralExpression */:
+ case 200 /* ObjectLiteralExpression */:
+ case 211 /* TypeOfExpression */:
+ case 225 /* NonNullExpression */:
+ case 274 /* JsxSelfClosingElement */:
+ case 273 /* JsxElement */:
+ return true;
+ case 217 /* ConditionalExpression */:
+ return isSideEffectFree(node.whenTrue) &&
+ isSideEffectFree(node.whenFalse);
+ case 216 /* BinaryExpression */:
+ if (ts.isAssignmentOperator(node.operatorToken.kind)) {
+ return false;
+ }
+ return isSideEffectFree(node.left) &&
+ isSideEffectFree(node.right);
+ case 214 /* PrefixUnaryExpression */:
+ case 215 /* PostfixUnaryExpression */:
+ // Unary operators ~, !, +, and - have no side effects.
+ // The rest do.
+ switch (node.operator) {
+ case 53 /* ExclamationToken */:
+ case 39 /* PlusToken */:
+ case 40 /* MinusToken */:
+ case 54 /* TildeToken */:
+ return true;
+ }
+ return false;
+ // Some forms listed here for clarity
+ case 212 /* VoidExpression */: // Explicit opt-out
+ case 206 /* TypeAssertionExpression */: // Not SEF, but can produce useful type warnings
+ case 224 /* AsExpression */: // Not SEF, but can produce useful type warnings
+ default:
+ return false;
}
}
- function getEntityNameForDecoratorMetadata(node) {
- if (node) {
- switch (node.kind) {
- case 179 /* IntersectionType */:
- case 178 /* UnionType */:
- return getEntityNameForDecoratorMetadataFromTypeList(node.types);
- case 180 /* ConditionalType */:
- return getEntityNameForDecoratorMetadataFromTypeList([node.trueType, node.falseType]);
- case 182 /* ParenthesizedType */:
- return getEntityNameForDecoratorMetadata(node.type);
- case 169 /* TypeReference */:
- return node.typeName;
+ function isTypeEqualityComparableTo(source, target) {
+ return (target.flags & 98304 /* Nullable */) !== 0 || isTypeComparableTo(source, target);
+ }
+ var CheckBinaryExpressionState;
+ (function (CheckBinaryExpressionState) {
+ CheckBinaryExpressionState[CheckBinaryExpressionState["MaybeCheckLeft"] = 0] = "MaybeCheckLeft";
+ CheckBinaryExpressionState[CheckBinaryExpressionState["CheckRight"] = 1] = "CheckRight";
+ CheckBinaryExpressionState[CheckBinaryExpressionState["FinishCheck"] = 2] = "FinishCheck";
+ })(CheckBinaryExpressionState || (CheckBinaryExpressionState = {}));
+ function checkBinaryExpression(node, checkMode) {
+ var workStacks = {
+ expr: [node],
+ state: [0 /* MaybeCheckLeft */],
+ leftType: [undefined]
+ };
+ var stackIndex = 0;
+ var lastResult;
+ while (stackIndex >= 0) {
+ node = workStacks.expr[stackIndex];
+ switch (workStacks.state[stackIndex]) {
+ case 0 /* MaybeCheckLeft */: {
+ if (ts.isInJSFile(node) && ts.getAssignedExpandoInitializer(node)) {
+ finishInvocation(checkExpression(node.right, checkMode));
+ break;
+ }
+ checkGrammarNullishCoalesceWithLogicalExpression(node);
+ var operator = node.operatorToken.kind;
+ if (operator === 62 /* EqualsToken */ && (node.left.kind === 200 /* ObjectLiteralExpression */ || node.left.kind === 199 /* ArrayLiteralExpression */)) {
+ finishInvocation(checkDestructuringAssignment(node.left, checkExpression(node.right, checkMode), checkMode, node.right.kind === 107 /* ThisKeyword */));
+ break;
+ }
+ advanceState(1 /* CheckRight */);
+ maybeCheckExpression(node.left);
+ break;
+ }
+ case 1 /* CheckRight */: {
+ var leftType = lastResult;
+ workStacks.leftType[stackIndex] = leftType;
+ var operator = node.operatorToken.kind;
+ if (operator === 55 /* AmpersandAmpersandToken */ || operator === 56 /* BarBarToken */ || operator === 60 /* QuestionQuestionToken */) {
+ checkTruthinessOfType(leftType, node.left);
+ }
+ advanceState(2 /* FinishCheck */);
+ maybeCheckExpression(node.right);
+ break;
+ }
+ case 2 /* FinishCheck */: {
+ var leftType = workStacks.leftType[stackIndex];
+ var rightType = lastResult;
+ finishInvocation(checkBinaryLikeExpressionWorker(node.left, node.operatorToken, node.right, leftType, rightType, node));
+ break;
+ }
+ default: return ts.Debug.fail("Invalid state " + workStacks.state[stackIndex] + " for checkBinaryExpression");
}
}
- }
- function getEntityNameForDecoratorMetadataFromTypeList(types) {
- var commonEntityName;
- for (var _i = 0, types_20 = types; _i < types_20.length; _i++) {
- var typeNode = types_20[_i];
- while (typeNode.kind === 182 /* ParenthesizedType */) {
- typeNode = typeNode.type; // Skip parens if need be
+ return lastResult;
+ function finishInvocation(result) {
+ lastResult = result;
+ stackIndex--;
+ }
+ /**
+ * Note that `advanceState` sets the _current_ head state, and that `maybeCheckExpression` potentially pushes on a new
+ * head state; so `advanceState` must be called before any `maybeCheckExpression` during a state's execution.
+ */
+ function advanceState(nextState) {
+ workStacks.state[stackIndex] = nextState;
+ }
+ function maybeCheckExpression(node) {
+ if (ts.isBinaryExpression(node)) {
+ stackIndex++;
+ workStacks.expr[stackIndex] = node;
+ workStacks.state[stackIndex] = 0 /* MaybeCheckLeft */;
+ workStacks.leftType[stackIndex] = undefined;
}
- if (typeNode.kind === 137 /* NeverKeyword */) {
- continue; // Always elide `never` from the union/intersection if possible
+ else {
+ lastResult = checkExpression(node, checkMode);
}
- if (!strictNullChecks && (typeNode.kind === 100 /* NullKeyword */ || typeNode.kind === 146 /* UndefinedKeyword */)) {
- continue; // Elide null and undefined from unions for metadata, just like what we did prior to the implementation of strict null checks
+ }
+ }
+ function checkGrammarNullishCoalesceWithLogicalExpression(node) {
+ var left = node.left, operatorToken = node.operatorToken, right = node.right;
+ if (operatorToken.kind === 60 /* QuestionQuestionToken */) {
+ if (ts.isBinaryExpression(left) && (left.operatorToken.kind === 56 /* BarBarToken */ || left.operatorToken.kind === 55 /* AmpersandAmpersandToken */)) {
+ grammarErrorOnNode(left, ts.Diagnostics._0_and_1_operations_cannot_be_mixed_without_parentheses, ts.tokenToString(left.operatorToken.kind), ts.tokenToString(operatorToken.kind));
}
- var individualEntityName = getEntityNameForDecoratorMetadata(typeNode);
- if (!individualEntityName) {
- // Individual is something like string number
- // So it would be serialized to either that type or object
- // Safe to return here
- return undefined;
+ if (ts.isBinaryExpression(right) && (right.operatorToken.kind === 56 /* BarBarToken */ || right.operatorToken.kind === 55 /* AmpersandAmpersandToken */)) {
+ grammarErrorOnNode(right, ts.Diagnostics._0_and_1_operations_cannot_be_mixed_without_parentheses, ts.tokenToString(right.operatorToken.kind), ts.tokenToString(operatorToken.kind));
}
- if (commonEntityName) {
- // Note this is in sync with the transformation that happens for type node.
- // Keep this in sync with serializeUnionOrIntersectionType
- // Verify if they refer to same entity and is identifier
- // return undefined if they dont match because we would emit object
- if (!ts.isIdentifier(commonEntityName) ||
- !ts.isIdentifier(individualEntityName) ||
- commonEntityName.escapedText !== individualEntityName.escapedText) {
- return undefined;
+ }
+ }
+ // Note that this and `checkBinaryExpression` above should behave mostly the same, except this elides some
+ // expression-wide checks and does not use a work stack to fold nested binary expressions into the same callstack frame
+ function checkBinaryLikeExpression(left, operatorToken, right, checkMode, errorNode) {
+ var operator = operatorToken.kind;
+ if (operator === 62 /* EqualsToken */ && (left.kind === 200 /* ObjectLiteralExpression */ || left.kind === 199 /* ArrayLiteralExpression */)) {
+ return checkDestructuringAssignment(left, checkExpression(right, checkMode), checkMode, right.kind === 107 /* ThisKeyword */);
+ }
+ var leftType;
+ if (operator === 55 /* AmpersandAmpersandToken */ || operator === 56 /* BarBarToken */ || operator === 60 /* QuestionQuestionToken */) {
+ leftType = checkTruthinessExpression(left, checkMode);
+ }
+ else {
+ leftType = checkExpression(left, checkMode);
+ }
+ var rightType = checkExpression(right, checkMode);
+ return checkBinaryLikeExpressionWorker(left, operatorToken, right, leftType, rightType, errorNode);
+ }
+ function checkBinaryLikeExpressionWorker(left, operatorToken, right, leftType, rightType, errorNode) {
+ var operator = operatorToken.kind;
+ switch (operator) {
+ case 41 /* AsteriskToken */:
+ case 42 /* AsteriskAsteriskToken */:
+ case 65 /* AsteriskEqualsToken */:
+ case 66 /* AsteriskAsteriskEqualsToken */:
+ case 43 /* SlashToken */:
+ case 67 /* SlashEqualsToken */:
+ case 44 /* PercentToken */:
+ case 68 /* PercentEqualsToken */:
+ case 40 /* MinusToken */:
+ case 64 /* MinusEqualsToken */:
+ case 47 /* LessThanLessThanToken */:
+ case 69 /* LessThanLessThanEqualsToken */:
+ case 48 /* GreaterThanGreaterThanToken */:
+ case 70 /* GreaterThanGreaterThanEqualsToken */:
+ case 49 /* GreaterThanGreaterThanGreaterThanToken */:
+ case 71 /* GreaterThanGreaterThanGreaterThanEqualsToken */:
+ case 51 /* BarToken */:
+ case 73 /* BarEqualsToken */:
+ case 52 /* CaretToken */:
+ case 77 /* CaretEqualsToken */:
+ case 50 /* AmpersandToken */:
+ case 72 /* AmpersandEqualsToken */:
+ if (leftType === silentNeverType || rightType === silentNeverType) {
+ return silentNeverType;
+ }
+ leftType = checkNonNullType(leftType, left);
+ rightType = checkNonNullType(rightType, right);
+ var suggestedOperator = void 0;
+ // if a user tries to apply a bitwise operator to 2 boolean operands
+ // try and return them a helpful suggestion
+ if ((leftType.flags & 528 /* BooleanLike */) &&
+ (rightType.flags & 528 /* BooleanLike */) &&
+ (suggestedOperator = getSuggestedBooleanOperator(operatorToken.kind)) !== undefined) {
+ error(errorNode || operatorToken, ts.Diagnostics.The_0_operator_is_not_allowed_for_boolean_types_Consider_using_1_instead, ts.tokenToString(operatorToken.kind), ts.tokenToString(suggestedOperator));
+ return numberType;
+ }
+ else {
+ // otherwise just check each operand separately and report errors as normal
+ var leftOk = checkArithmeticOperandType(left, leftType, ts.Diagnostics.The_left_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type, /*isAwaitValid*/ true);
+ var rightOk = checkArithmeticOperandType(right, rightType, ts.Diagnostics.The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type, /*isAwaitValid*/ true);
+ var resultType_1;
+ // If both are any or unknown, allow operation; assume it will resolve to number
+ if ((isTypeAssignableToKind(leftType, 3 /* AnyOrUnknown */) && isTypeAssignableToKind(rightType, 3 /* AnyOrUnknown */)) ||
+ // Or, if neither could be bigint, implicit coercion results in a number result
+ !(maybeTypeOfKind(leftType, 2112 /* BigIntLike */) || maybeTypeOfKind(rightType, 2112 /* BigIntLike */))) {
+ resultType_1 = numberType;
+ }
+ // At least one is assignable to bigint, so check that both are
+ else if (bothAreBigIntLike(leftType, rightType)) {
+ switch (operator) {
+ case 49 /* GreaterThanGreaterThanGreaterThanToken */:
+ case 71 /* GreaterThanGreaterThanGreaterThanEqualsToken */:
+ reportOperatorError();
+ break;
+ case 42 /* AsteriskAsteriskToken */:
+ case 66 /* AsteriskAsteriskEqualsToken */:
+ if (languageVersion < 3 /* ES2016 */) {
+ error(errorNode, ts.Diagnostics.Exponentiation_cannot_be_performed_on_bigint_values_unless_the_target_option_is_set_to_es2016_or_later);
+ }
+ }
+ resultType_1 = bigintType;
+ }
+ // Exactly one of leftType/rightType is assignable to bigint
+ else {
+ reportOperatorError(bothAreBigIntLike);
+ resultType_1 = errorType;
+ }
+ if (leftOk && rightOk) {
+ checkAssignmentOperator(resultType_1);
+ }
+ return resultType_1;
+ }
+ case 39 /* PlusToken */:
+ case 63 /* PlusEqualsToken */:
+ if (leftType === silentNeverType || rightType === silentNeverType) {
+ return silentNeverType;
+ }
+ if (!isTypeAssignableToKind(leftType, 402653316 /* StringLike */) && !isTypeAssignableToKind(rightType, 402653316 /* StringLike */)) {
+ leftType = checkNonNullType(leftType, left);
+ rightType = checkNonNullType(rightType, right);
+ }
+ var resultType = void 0;
+ if (isTypeAssignableToKind(leftType, 296 /* NumberLike */, /*strict*/ true) && isTypeAssignableToKind(rightType, 296 /* NumberLike */, /*strict*/ true)) {
+ // Operands of an enum type are treated as having the primitive type Number.
+ // If both operands are of the Number primitive type, the result is of the Number primitive type.
+ resultType = numberType;
+ }
+ else if (isTypeAssignableToKind(leftType, 2112 /* BigIntLike */, /*strict*/ true) && isTypeAssignableToKind(rightType, 2112 /* BigIntLike */, /*strict*/ true)) {
+ // If both operands are of the BigInt primitive type, the result is of the BigInt primitive type.
+ resultType = bigintType;
+ }
+ else if (isTypeAssignableToKind(leftType, 402653316 /* StringLike */, /*strict*/ true) || isTypeAssignableToKind(rightType, 402653316 /* StringLike */, /*strict*/ true)) {
+ // If one or both operands are of the String primitive type, the result is of the String primitive type.
+ resultType = stringType;
+ }
+ else if (isTypeAny(leftType) || isTypeAny(rightType)) {
+ // Otherwise, the result is of type Any.
+ // NOTE: unknown type here denotes error type. Old compiler treated this case as any type so do we.
+ resultType = leftType === errorType || rightType === errorType ? errorType : anyType;
+ }
+ // Symbols are not allowed at all in arithmetic expressions
+ if (resultType && !checkForDisallowedESSymbolOperand(operator)) {
+ return resultType;
+ }
+ if (!resultType) {
+ // Types that have a reasonably good chance of being a valid operand type.
+ // If both types have an awaited type of one of these, we'll assume the user
+ // might be missing an await without doing an exhaustive check that inserting
+ // await(s) will actually be a completely valid binary expression.
+ var closeEnoughKind_1 = 296 /* NumberLike */ | 2112 /* BigIntLike */ | 402653316 /* StringLike */ | 3 /* AnyOrUnknown */;
+ reportOperatorError(function (left, right) {
+ return isTypeAssignableToKind(left, closeEnoughKind_1) &&
+ isTypeAssignableToKind(right, closeEnoughKind_1);
+ });
+ return anyType;
+ }
+ if (operator === 63 /* PlusEqualsToken */) {
+ checkAssignmentOperator(resultType);
+ }
+ return resultType;
+ case 29 /* LessThanToken */:
+ case 31 /* GreaterThanToken */:
+ case 32 /* LessThanEqualsToken */:
+ case 33 /* GreaterThanEqualsToken */:
+ if (checkForDisallowedESSymbolOperand(operator)) {
+ leftType = getBaseTypeOfLiteralType(checkNonNullType(leftType, left));
+ rightType = getBaseTypeOfLiteralType(checkNonNullType(rightType, right));
+ reportOperatorErrorUnless(function (left, right) {
+ return isTypeComparableTo(left, right) || isTypeComparableTo(right, left) || (isTypeAssignableTo(left, numberOrBigIntType) && isTypeAssignableTo(right, numberOrBigIntType));
+ });
+ }
+ return booleanType;
+ case 34 /* EqualsEqualsToken */:
+ case 35 /* ExclamationEqualsToken */:
+ case 36 /* EqualsEqualsEqualsToken */:
+ case 37 /* ExclamationEqualsEqualsToken */:
+ reportOperatorErrorUnless(function (left, right) { return isTypeEqualityComparableTo(left, right) || isTypeEqualityComparableTo(right, left); });
+ return booleanType;
+ case 101 /* InstanceOfKeyword */:
+ return checkInstanceOfExpression(left, right, leftType, rightType);
+ case 100 /* InKeyword */:
+ return checkInExpression(left, right, leftType, rightType);
+ case 55 /* AmpersandAmpersandToken */:
+ case 75 /* AmpersandAmpersandEqualsToken */: {
+ var resultType_2 = getTypeFacts(leftType) & 4194304 /* Truthy */ ?
+ getUnionType([extractDefinitelyFalsyTypes(strictNullChecks ? leftType : getBaseTypeOfLiteralType(rightType)), rightType]) :
+ leftType;
+ if (operator === 75 /* AmpersandAmpersandEqualsToken */) {
+ checkAssignmentOperator(rightType);
+ }
+ return resultType_2;
+ }
+ case 56 /* BarBarToken */:
+ case 74 /* BarBarEqualsToken */: {
+ var resultType_3 = getTypeFacts(leftType) & 8388608 /* Falsy */ ?
+ getUnionType([removeDefinitelyFalsyTypes(leftType), rightType], 2 /* Subtype */) :
+ leftType;
+ if (operator === 74 /* BarBarEqualsToken */) {
+ checkAssignmentOperator(rightType);
}
+ return resultType_3;
}
- else {
- commonEntityName = individualEntityName;
+ case 60 /* QuestionQuestionToken */:
+ case 76 /* QuestionQuestionEqualsToken */: {
+ var resultType_4 = getTypeFacts(leftType) & 262144 /* EQUndefinedOrNull */ ?
+ getUnionType([getNonNullableType(leftType), rightType], 2 /* Subtype */) :
+ leftType;
+ if (operator === 76 /* QuestionQuestionEqualsToken */) {
+ checkAssignmentOperator(rightType);
+ }
+ return resultType_4;
}
+ case 62 /* EqualsToken */:
+ var declKind = ts.isBinaryExpression(left.parent) ? ts.getAssignmentDeclarationKind(left.parent) : 0 /* None */;
+ checkAssignmentDeclaration(declKind, rightType);
+ if (isAssignmentDeclaration(declKind)) {
+ if (!(rightType.flags & 524288 /* Object */) ||
+ declKind !== 2 /* ModuleExports */ &&
+ declKind !== 6 /* Prototype */ &&
+ !isEmptyObjectType(rightType) &&
+ !isFunctionObjectType(rightType) &&
+ !(ts.getObjectFlags(rightType) & 1 /* Class */)) {
+ // don't check assignability of module.exports=, C.prototype=, or expando types because they will necessarily be incomplete
+ checkAssignmentOperator(rightType);
+ }
+ return leftType;
+ }
+ else {
+ checkAssignmentOperator(rightType);
+ return getRegularTypeOfObjectLiteral(rightType);
+ }
+ case 27 /* CommaToken */:
+ if (!compilerOptions.allowUnreachableCode && isSideEffectFree(left) && !isEvalNode(right)) {
+ var sf = ts.getSourceFileOfNode(left);
+ var sourceText = sf.text;
+ var start_3 = ts.skipTrivia(sourceText, left.pos);
+ var isInDiag2657 = sf.parseDiagnostics.some(function (diag) {
+ if (diag.code !== ts.Diagnostics.JSX_expressions_must_have_one_parent_element.code)
+ return false;
+ return ts.textSpanContainsPosition(diag, start_3);
+ });
+ if (!isInDiag2657)
+ error(left, ts.Diagnostics.Left_side_of_comma_operator_is_unused_and_has_no_side_effects);
+ }
+ return rightType;
+ default:
+ return ts.Debug.fail();
}
- return commonEntityName;
- }
- function getParameterTypeNodeForDecoratorCheck(node) {
- var typeNode = ts.getEffectiveTypeAnnotationNode(node);
- return ts.isRestParameter(node) ? ts.getRestParameterElementType(typeNode) : typeNode;
- }
- /** Check the decorators of a node */
- function checkDecorators(node) {
- if (!node.decorators) {
- return;
- }
- // skip this check for nodes that cannot have decorators. These should have already had an error reported by
- // checkGrammarDecorators.
- if (!ts.nodeCanBeDecorated(node, node.parent, node.parent.parent)) {
- return;
- }
- if (!compilerOptions.experimentalDecorators) {
- error(node, ts.Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalDecorators_option_in_your_tsconfig_or_jsconfig_to_remove_this_warning);
- }
- var firstDecorator = node.decorators[0];
- checkExternalEmitHelpers(firstDecorator, 8 /* Decorate */);
- if (node.kind === 156 /* Parameter */) {
- checkExternalEmitHelpers(firstDecorator, 32 /* Param */);
+ function bothAreBigIntLike(left, right) {
+ return isTypeAssignableToKind(left, 2112 /* BigIntLike */) && isTypeAssignableToKind(right, 2112 /* BigIntLike */);
}
- if (compilerOptions.emitDecoratorMetadata) {
- checkExternalEmitHelpers(firstDecorator, 16 /* Metadata */);
- // we only need to perform these checks if we are emitting serialized type metadata for the target of a decorator.
- switch (node.kind) {
- case 245 /* ClassDeclaration */:
- var constructor = ts.getFirstConstructorWithBody(node);
- if (constructor) {
- for (var _i = 0, _a = constructor.parameters; _i < _a.length; _i++) {
- var parameter = _a[_i];
- markDecoratorMedataDataTypeNodeAsReferenced(getParameterTypeNodeForDecoratorCheck(parameter));
+ function checkAssignmentDeclaration(kind, rightType) {
+ if (kind === 2 /* ModuleExports */) {
+ for (var _i = 0, _a = getPropertiesOfObjectType(rightType); _i < _a.length; _i++) {
+ var prop = _a[_i];
+ var propType = getTypeOfSymbol(prop);
+ if (propType.symbol && propType.symbol.flags & 32 /* Class */) {
+ var name = prop.escapedName;
+ var symbol = resolveName(prop.valueDeclaration, name, 788968 /* Type */, undefined, name, /*isUse*/ false);
+ if (symbol && symbol.declarations.some(ts.isJSDocTypedefTag)) {
+ addDuplicateDeclarationErrorsForSymbols(symbol, ts.Diagnostics.Duplicate_identifier_0, ts.unescapeLeadingUnderscores(name), prop);
+ addDuplicateDeclarationErrorsForSymbols(prop, ts.Diagnostics.Duplicate_identifier_0, ts.unescapeLeadingUnderscores(name), symbol);
}
}
- break;
- case 163 /* GetAccessor */:
- case 164 /* SetAccessor */:
- var otherKind = node.kind === 163 /* GetAccessor */ ? 164 /* SetAccessor */ : 163 /* GetAccessor */;
- var otherAccessor = ts.getDeclarationOfKind(getSymbolOfNode(node), otherKind);
- markDecoratorMedataDataTypeNodeAsReferenced(getAnnotatedAccessorTypeNode(node) || otherAccessor && getAnnotatedAccessorTypeNode(otherAccessor));
- break;
- case 161 /* MethodDeclaration */:
- for (var _b = 0, _c = node.parameters; _b < _c.length; _b++) {
- var parameter = _c[_b];
- markDecoratorMedataDataTypeNodeAsReferenced(getParameterTypeNodeForDecoratorCheck(parameter));
- }
- markDecoratorMedataDataTypeNodeAsReferenced(ts.getEffectiveReturnTypeNode(node));
- break;
- case 159 /* PropertyDeclaration */:
- markDecoratorMedataDataTypeNodeAsReferenced(ts.getEffectiveTypeAnnotationNode(node));
- break;
- case 156 /* Parameter */:
- markDecoratorMedataDataTypeNodeAsReferenced(getParameterTypeNodeForDecoratorCheck(node));
- var containingSignature = node.parent;
- for (var _d = 0, _e = containingSignature.parameters; _d < _e.length; _d++) {
- var parameter = _e[_d];
- markDecoratorMedataDataTypeNodeAsReferenced(getParameterTypeNodeForDecoratorCheck(parameter));
- }
- break;
+ }
}
}
- ts.forEach(node.decorators, checkDecorator);
- }
- function checkFunctionDeclaration(node) {
- if (produceDiagnostics) {
- checkFunctionOrMethodDeclaration(node);
- checkGrammarForGenerator(node);
- checkCollisionWithRequireExportsInGeneratedCode(node, node.name);
- checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name);
- }
- }
- function checkJSDocTypeAliasTag(node) {
- if (!node.typeExpression) {
- // If the node had `@property` tags, `typeExpression` would have been set to the first property tag.
- error(node.name, ts.Diagnostics.JSDoc_typedef_tag_should_either_have_a_type_annotation_or_be_followed_by_property_or_member_tags);
+ function isEvalNode(node) {
+ return node.kind === 78 /* Identifier */ && node.escapedText === "eval";
}
- if (node.name) {
- checkTypeNameIsReserved(node.name, ts.Diagnostics.Type_alias_name_cannot_be_0);
+ // Return true if there was no error, false if there was an error.
+ function checkForDisallowedESSymbolOperand(operator) {
+ var offendingSymbolOperand = maybeTypeOfKind(leftType, 12288 /* ESSymbolLike */) ? left :
+ maybeTypeOfKind(rightType, 12288 /* ESSymbolLike */) ? right :
+ undefined;
+ if (offendingSymbolOperand) {
+ error(offendingSymbolOperand, ts.Diagnostics.The_0_operator_cannot_be_applied_to_type_symbol, ts.tokenToString(operator));
+ return false;
+ }
+ return true;
}
- checkSourceElement(node.typeExpression);
- }
- function checkJSDocTemplateTag(node) {
- checkSourceElement(node.constraint);
- for (var _i = 0, _a = node.typeParameters; _i < _a.length; _i++) {
- var tp = _a[_i];
- checkSourceElement(tp);
+ function getSuggestedBooleanOperator(operator) {
+ switch (operator) {
+ case 51 /* BarToken */:
+ case 73 /* BarEqualsToken */:
+ return 56 /* BarBarToken */;
+ case 52 /* CaretToken */:
+ case 77 /* CaretEqualsToken */:
+ return 37 /* ExclamationEqualsEqualsToken */;
+ case 50 /* AmpersandToken */:
+ case 72 /* AmpersandEqualsToken */:
+ return 55 /* AmpersandAmpersandToken */;
+ default:
+ return undefined;
+ }
}
- }
- function checkJSDocTypeTag(node) {
- checkSourceElement(node.typeExpression);
- }
- function checkJSDocParameterTag(node) {
- checkSourceElement(node.typeExpression);
- if (!ts.getParameterSymbolFromJSDoc(node)) {
- var decl = ts.getHostSignatureFromJSDoc(node);
- // don't issue an error for invalid hosts -- just functions --
- // and give a better error message when the host function mentions `arguments`
- // but the tag doesn't have an array type
- if (decl) {
- var i = ts.getJSDocTags(decl).filter(ts.isJSDocParameterTag).indexOf(node);
- if (i > -1 && i < decl.parameters.length && ts.isBindingPattern(decl.parameters[i].name)) {
- return;
- }
- if (!containsArgumentsReference(decl)) {
- if (ts.isQualifiedName(node.name)) {
- error(node.name, ts.Diagnostics.Qualified_name_0_is_not_allowed_without_a_leading_param_object_1, ts.entityNameToString(node.name), ts.entityNameToString(node.name.left));
- }
- else {
- error(node.name, ts.Diagnostics.JSDoc_param_tag_has_name_0_but_there_is_no_parameter_with_that_name, ts.idText(node.name));
- }
- }
- else if (ts.findLast(ts.getJSDocTags(decl), ts.isJSDocParameterTag) === node &&
- node.typeExpression && node.typeExpression.type &&
- !isArrayType(getTypeFromTypeNode(node.typeExpression.type))) {
- error(node.name, ts.Diagnostics.JSDoc_param_tag_has_name_0_but_there_is_no_parameter_with_that_name_It_would_match_arguments_if_it_had_an_array_type, ts.idText(node.name.kind === 153 /* QualifiedName */ ? node.name.right : node.name));
+ function checkAssignmentOperator(valueType) {
+ if (produceDiagnostics && ts.isAssignmentOperator(operator)) {
+ // TypeScript 1.0 spec (April 2014): 4.17
+ // An assignment of the form
+ // VarExpr = ValueExpr
+ // requires VarExpr to be classified as a reference
+ // A compound assignment furthermore requires VarExpr to be classified as a reference (section 4.1)
+ // and the type of the non-compound operation to be assignable to the type of VarExpr.
+ if (checkReferenceExpression(left, ts.Diagnostics.The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access, ts.Diagnostics.The_left_hand_side_of_an_assignment_expression_may_not_be_an_optional_property_access)
+ && (!ts.isIdentifier(left) || ts.unescapeLeadingUnderscores(left.escapedText) !== "exports")) {
+ // to avoid cascading errors check assignability only if 'isReference' check succeeded and no errors were reported
+ checkTypeAssignableToAndOptionallyElaborate(valueType, leftType, left, right);
}
}
}
- }
- function checkJSDocPropertyTag(node) {
- checkSourceElement(node.typeExpression);
- }
- function checkJSDocFunctionType(node) {
- if (produceDiagnostics && !node.type && !ts.isJSDocConstructSignature(node)) {
- reportImplicitAny(node, anyType);
- }
- checkSignatureDeclaration(node);
- }
- function checkJSDocImplementsTag(node) {
- var classLike = ts.getEffectiveJSDocHost(node);
- if (!classLike || !ts.isClassDeclaration(classLike) && !ts.isClassExpression(classLike)) {
- error(classLike, ts.Diagnostics.JSDoc_0_is_not_attached_to_a_class, ts.idText(node.tagName));
+ function isAssignmentDeclaration(kind) {
+ var _a;
+ switch (kind) {
+ case 2 /* ModuleExports */:
+ return true;
+ case 1 /* ExportsProperty */:
+ case 5 /* Property */:
+ case 6 /* Prototype */:
+ case 3 /* PrototypeProperty */:
+ case 4 /* ThisProperty */:
+ var symbol = getSymbolOfNode(left);
+ var init = ts.getAssignedExpandoInitializer(right);
+ return !!init && ts.isObjectLiteralExpression(init) &&
+ !!((_a = symbol === null || symbol === void 0 ? void 0 : symbol.exports) === null || _a === void 0 ? void 0 : _a.size);
+ default:
+ return false;
+ }
}
- }
- function checkJSDocAugmentsTag(node) {
- var classLike = ts.getEffectiveJSDocHost(node);
- if (!classLike || !ts.isClassDeclaration(classLike) && !ts.isClassExpression(classLike)) {
- error(classLike, ts.Diagnostics.JSDoc_0_is_not_attached_to_a_class, ts.idText(node.tagName));
- return;
+ /**
+ * Returns true if an error is reported
+ */
+ function reportOperatorErrorUnless(typesAreCompatible) {
+ if (!typesAreCompatible(leftType, rightType)) {
+ reportOperatorError(typesAreCompatible);
+ return true;
+ }
+ return false;
}
- var augmentsTags = ts.getJSDocTags(classLike).filter(ts.isJSDocAugmentsTag);
- ts.Debug.assert(augmentsTags.length > 0);
- if (augmentsTags.length > 1) {
- error(augmentsTags[1], ts.Diagnostics.Class_declarations_cannot_have_more_than_one_augments_or_extends_tag);
+ function reportOperatorError(isRelated) {
+ var _a;
+ var wouldWorkWithAwait = false;
+ var errNode = errorNode || operatorToken;
+ if (isRelated) {
+ var awaitedLeftType = getAwaitedType(leftType);
+ var awaitedRightType = getAwaitedType(rightType);
+ wouldWorkWithAwait = !(awaitedLeftType === leftType && awaitedRightType === rightType)
+ && !!(awaitedLeftType && awaitedRightType)
+ && isRelated(awaitedLeftType, awaitedRightType);
+ }
+ var effectiveLeft = leftType;
+ var effectiveRight = rightType;
+ if (!wouldWorkWithAwait && isRelated) {
+ _a = getBaseTypesIfUnrelated(leftType, rightType, isRelated), effectiveLeft = _a[0], effectiveRight = _a[1];
+ }
+ var _b = getTypeNamesForErrorDisplay(effectiveLeft, effectiveRight), leftStr = _b[0], rightStr = _b[1];
+ if (!tryGiveBetterPrimaryError(errNode, wouldWorkWithAwait, leftStr, rightStr)) {
+ errorAndMaybeSuggestAwait(errNode, wouldWorkWithAwait, ts.Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2, ts.tokenToString(operatorToken.kind), leftStr, rightStr);
+ }
}
- var name = getIdentifierFromEntityNameExpression(node.class.expression);
- var extend = ts.getClassExtendsHeritageElement(classLike);
- if (extend) {
- var className = getIdentifierFromEntityNameExpression(extend.expression);
- if (className && name.escapedText !== className.escapedText) {
- error(name, ts.Diagnostics.JSDoc_0_1_does_not_match_the_extends_2_clause, ts.idText(node.tagName), ts.idText(name), ts.idText(className));
+ function tryGiveBetterPrimaryError(errNode, maybeMissingAwait, leftStr, rightStr) {
+ var typeName;
+ switch (operatorToken.kind) {
+ case 36 /* EqualsEqualsEqualsToken */:
+ case 34 /* EqualsEqualsToken */:
+ typeName = "false";
+ break;
+ case 37 /* ExclamationEqualsEqualsToken */:
+ case 35 /* ExclamationEqualsToken */:
+ typeName = "true";
}
+ if (typeName) {
+ return errorAndMaybeSuggestAwait(errNode, maybeMissingAwait, ts.Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap, typeName, leftStr, rightStr);
+ }
+ return undefined;
}
}
- function getIdentifierFromEntityNameExpression(node) {
- switch (node.kind) {
- case 75 /* Identifier */:
- return node;
- case 194 /* PropertyAccessExpression */:
- return node.name;
- default:
- return undefined;
+ function getBaseTypesIfUnrelated(leftType, rightType, isRelated) {
+ var effectiveLeft = leftType;
+ var effectiveRight = rightType;
+ var leftBase = getBaseTypeOfLiteralType(leftType);
+ var rightBase = getBaseTypeOfLiteralType(rightType);
+ if (!isRelated(leftBase, rightBase)) {
+ effectiveLeft = leftBase;
+ effectiveRight = rightBase;
}
+ return [effectiveLeft, effectiveRight];
}
- function checkFunctionOrMethodDeclaration(node) {
- checkDecorators(node);
- checkSignatureDeclaration(node);
- var functionFlags = ts.getFunctionFlags(node);
- // Do not use hasDynamicName here, because that returns false for well known symbols.
- // We want to perform checkComputedPropertyName for all computed properties, including
- // well known symbols.
- if (node.name && node.name.kind === 154 /* ComputedPropertyName */) {
- // This check will account for methods in class/interface declarations,
- // as well as accessors in classes/object literals
- checkComputedPropertyName(node.name);
- }
- if (!hasNonBindableDynamicName(node)) {
- // first we want to check the local symbol that contain this declaration
- // - if node.localSymbol !== undefined - this is current declaration is exported and localSymbol points to the local symbol
- // - if node.localSymbol === undefined - this node is non-exported so we can just pick the result of getSymbolOfNode
- var symbol = getSymbolOfNode(node);
- var localSymbol = node.localSymbol || symbol;
- // Since the javascript won't do semantic analysis like typescript,
- // if the javascript file comes before the typescript file and both contain same name functions,
- // checkFunctionOrConstructorSymbol wouldn't be called if we didnt ignore javascript function.
- var firstDeclaration = ts.find(localSymbol.declarations,
- // Get first non javascript function declaration
- function (declaration) { return declaration.kind === node.kind && !(declaration.flags & 131072 /* JavaScriptFile */); });
- // Only type check the symbol once
- if (node === firstDeclaration) {
- checkFunctionOrConstructorSymbol(localSymbol);
+ function checkYieldExpression(node) {
+ // Grammar checking
+ if (produceDiagnostics) {
+ if (!(node.flags & 8192 /* YieldContext */)) {
+ grammarErrorOnFirstToken(node, ts.Diagnostics.A_yield_expression_is_only_allowed_in_a_generator_body);
}
- if (symbol.parent) {
- // run check once for the first declaration
- if (ts.getDeclarationOfKind(symbol, node.kind) === node) {
- // run check on export symbol to check that modifiers agree across all exported declarations
- checkFunctionOrConstructorSymbol(symbol);
- }
+ if (isInParameterInitializerBeforeContainingFunction(node)) {
+ error(node, ts.Diagnostics.yield_expressions_cannot_be_used_in_a_parameter_initializer);
}
}
- var body = node.kind === 160 /* MethodSignature */ ? undefined : node.body;
- checkSourceElement(body);
- checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, getReturnTypeFromAnnotation(node));
- if (produceDiagnostics && !ts.getEffectiveReturnTypeNode(node)) {
- // Report an implicit any error if there is no body, no explicit return type, and node is not a private method
- // in an ambient context
- if (ts.nodeIsMissing(body) && !isPrivateWithinAmbient(node)) {
- reportImplicitAny(node, anyType);
+ var func = ts.getContainingFunction(node);
+ if (!func)
+ return anyType;
+ var functionFlags = ts.getFunctionFlags(func);
+ if (!(functionFlags & 1 /* Generator */)) {
+ // If the user's code is syntactically correct, the func should always have a star. After all, we are in a yield context.
+ return anyType;
+ }
+ var isAsync = (functionFlags & 2 /* Async */) !== 0;
+ if (node.asteriskToken) {
+ // Async generator functions prior to ESNext require the __await, __asyncDelegator,
+ // and __asyncValues helpers
+ if (isAsync && languageVersion < 99 /* ESNext */) {
+ checkExternalEmitHelpers(node, 53248 /* AsyncDelegatorIncludes */);
}
- if (functionFlags & 1 /* Generator */ && ts.nodeIsPresent(body)) {
- // A generator with a body and no type annotation can still cause errors. It can error if the
- // yielded values have no common supertype, or it can give an implicit any error if it has no
- // yielded values. The only way to trigger these errors is to try checking its return type.
- getReturnTypeOfSignature(getSignatureFromDeclaration(node));
+ // Generator functions prior to ES2015 require the __values helper
+ if (!isAsync && languageVersion < 2 /* ES2015 */ && compilerOptions.downlevelIteration) {
+ checkExternalEmitHelpers(node, 256 /* Values */);
}
}
- // A js function declaration can have a @type tag instead of a return type node, but that type must have a call signature
- if (ts.isInJSFile(node)) {
- var typeTag = ts.getJSDocTypeTag(node);
- if (typeTag && typeTag.typeExpression && !getContextualCallSignature(getTypeFromTypeNode(typeTag.typeExpression), node)) {
- error(typeTag, ts.Diagnostics.The_type_of_a_function_declaration_must_match_the_function_s_signature);
- }
+ // There is no point in doing an assignability check if the function
+ // has no explicit return type because the return type is directly computed
+ // from the yield expressions.
+ var returnType = getReturnTypeFromAnnotation(func);
+ var iterationTypes = returnType && getIterationTypesOfGeneratorFunctionReturnType(returnType, isAsync);
+ var signatureYieldType = iterationTypes && iterationTypes.yieldType || anyType;
+ var signatureNextType = iterationTypes && iterationTypes.nextType || anyType;
+ var resolvedSignatureNextType = isAsync ? getAwaitedType(signatureNextType) || anyType : signatureNextType;
+ var yieldExpressionType = node.expression ? checkExpression(node.expression) : undefinedWideningType;
+ var yieldedType = getYieldedTypeOfYieldExpression(node, yieldExpressionType, resolvedSignatureNextType, isAsync);
+ if (returnType && yieldedType) {
+ checkTypeAssignableToAndOptionallyElaborate(yieldedType, signatureYieldType, node.expression || node, node.expression);
+ }
+ if (node.asteriskToken) {
+ var use = isAsync ? 19 /* AsyncYieldStar */ : 17 /* YieldStar */;
+ return getIterationTypeOfIterable(use, 1 /* Return */, yieldExpressionType, node.expression)
+ || anyType;
+ }
+ else if (returnType) {
+ return getIterationTypeOfGeneratorFunctionReturnType(2 /* Next */, returnType, isAsync)
+ || anyType;
}
+ return getContextualIterationType(2 /* Next */, func) || anyType;
}
- function registerForUnusedIdentifiersCheck(node) {
- // May be in a call such as getTypeOfNode that happened to call this. But potentiallyUnusedIdentifiers is only defined in the scope of `checkSourceFile`.
- if (produceDiagnostics) {
- var sourceFile = ts.getSourceFileOfNode(node);
- var potentiallyUnusedIdentifiers = allPotentiallyUnusedIdentifiers.get(sourceFile.path);
- if (!potentiallyUnusedIdentifiers) {
- potentiallyUnusedIdentifiers = [];
- allPotentiallyUnusedIdentifiers.set(sourceFile.path, potentiallyUnusedIdentifiers);
+ function checkConditionalExpression(node, checkMode) {
+ var type = checkTruthinessExpression(node.condition);
+ checkTestingKnownTruthyCallableType(node.condition, node.whenTrue, type);
+ var type1 = checkExpression(node.whenTrue, checkMode);
+ var type2 = checkExpression(node.whenFalse, checkMode);
+ return getUnionType([type1, type2], 2 /* Subtype */);
+ }
+ function checkTemplateExpression(node) {
+ var texts = [node.head.text];
+ var types = [];
+ for (var _i = 0, _a = node.templateSpans; _i < _a.length; _i++) {
+ var span = _a[_i];
+ var type = checkExpression(span.expression);
+ if (maybeTypeOfKind(type, 12288 /* ESSymbolLike */)) {
+ error(span.expression, ts.Diagnostics.Implicit_conversion_of_a_symbol_to_a_string_will_fail_at_runtime_Consider_wrapping_this_expression_in_String);
}
- // TODO: GH#22580
- // Debug.assert(addToSeen(seenPotentiallyUnusedIdentifiers, getNodeId(node)), "Adding potentially-unused identifier twice");
- potentiallyUnusedIdentifiers.push(node);
+ texts.push(span.literal.text);
+ types.push(isTypeAssignableTo(type, templateConstraintType) ? type : stringType);
}
+ return isConstContext(node) ? getTemplateLiteralType(texts, types) : stringType;
}
- function checkUnusedIdentifiers(potentiallyUnusedIdentifiers, addDiagnostic) {
- for (var _i = 0, potentiallyUnusedIdentifiers_1 = potentiallyUnusedIdentifiers; _i < potentiallyUnusedIdentifiers_1.length; _i++) {
- var node = potentiallyUnusedIdentifiers_1[_i];
- switch (node.kind) {
- case 245 /* ClassDeclaration */:
- case 214 /* ClassExpression */:
- checkUnusedClassMembers(node, addDiagnostic);
- checkUnusedTypeParameters(node, addDiagnostic);
- break;
- case 290 /* SourceFile */:
- case 249 /* ModuleDeclaration */:
- case 223 /* Block */:
- case 251 /* CaseBlock */:
- case 230 /* ForStatement */:
- case 231 /* ForInStatement */:
- case 232 /* ForOfStatement */:
- checkUnusedLocalsAndParameters(node, addDiagnostic);
- break;
- case 162 /* Constructor */:
- case 201 /* FunctionExpression */:
- case 244 /* FunctionDeclaration */:
- case 202 /* ArrowFunction */:
- case 161 /* MethodDeclaration */:
- case 163 /* GetAccessor */:
- case 164 /* SetAccessor */:
- if (node.body) { // Don't report unused parameters in overloads
- checkUnusedLocalsAndParameters(node, addDiagnostic);
- }
- checkUnusedTypeParameters(node, addDiagnostic);
- break;
- case 160 /* MethodSignature */:
- case 165 /* CallSignature */:
- case 166 /* ConstructSignature */:
- case 170 /* FunctionType */:
- case 171 /* ConstructorType */:
- case 247 /* TypeAliasDeclaration */:
- case 246 /* InterfaceDeclaration */:
- checkUnusedTypeParameters(node, addDiagnostic);
- break;
- case 181 /* InferType */:
- checkUnusedInferTypeParameter(node, addDiagnostic);
- break;
- default:
- ts.Debug.assertNever(node, "Node should not have been registered for unused identifiers check");
+ function getContextNode(node) {
+ if (node.kind === 281 /* JsxAttributes */ && !ts.isJsxSelfClosingElement(node.parent)) {
+ return node.parent.parent; // Needs to be the root JsxElement, so it encompasses the attributes _and_ the children (which are essentially part of the attributes)
+ }
+ return node;
+ }
+ function checkExpressionWithContextualType(node, contextualType, inferenceContext, checkMode) {
+ var context = getContextNode(node);
+ var saveContextualType = context.contextualType;
+ var saveInferenceContext = context.inferenceContext;
+ try {
+ context.contextualType = contextualType;
+ context.inferenceContext = inferenceContext;
+ var type = checkExpression(node, checkMode | 1 /* Contextual */ | (inferenceContext ? 2 /* Inferential */ : 0));
+ // We strip literal freshness when an appropriate contextual type is present such that contextually typed
+ // literals always preserve their literal types (otherwise they might widen during type inference). An alternative
+ // here would be to not mark contextually typed literals as fresh in the first place.
+ var result = maybeTypeOfKind(type, 2944 /* Literal */) && isLiteralOfContextualType(type, instantiateContextualType(contextualType, node)) ?
+ getRegularTypeOfLiteralType(type) : type;
+ return result;
+ }
+ finally {
+ // In the event our operation is canceled or some other exception occurs, reset the contextual type
+ // so that we do not accidentally hold onto an instance of the checker, as a Type created in the services layer
+ // may hold onto the checker that created it.
+ context.contextualType = saveContextualType;
+ context.inferenceContext = saveInferenceContext;
+ }
+ }
+ function checkExpressionCached(node, checkMode) {
+ var links = getNodeLinks(node);
+ if (!links.resolvedType) {
+ if (checkMode && checkMode !== 0 /* Normal */) {
+ return checkExpression(node, checkMode);
}
+ // When computing a type that we're going to cache, we need to ignore any ongoing control flow
+ // analysis because variables may have transient types in indeterminable states. Moving flowLoopStart
+ // to the top of the stack ensures all transient types are computed from a known point.
+ var saveFlowLoopStart = flowLoopStart;
+ var saveFlowTypeCache = flowTypeCache;
+ flowLoopStart = flowLoopCount;
+ flowTypeCache = undefined;
+ links.resolvedType = checkExpression(node, checkMode);
+ flowTypeCache = saveFlowTypeCache;
+ flowLoopStart = saveFlowLoopStart;
}
+ return links.resolvedType;
}
- function errorUnusedLocal(declaration, name, addDiagnostic) {
- var node = ts.getNameOfDeclaration(declaration) || declaration;
- var message = isTypeDeclaration(declaration) ? ts.Diagnostics._0_is_declared_but_never_used : ts.Diagnostics._0_is_declared_but_its_value_is_never_read;
- addDiagnostic(declaration, 0 /* Local */, ts.createDiagnosticForNode(node, message, name));
+ function isTypeAssertion(node) {
+ node = ts.skipParentheses(node);
+ return node.kind === 206 /* TypeAssertionExpression */ || node.kind === 224 /* AsExpression */;
}
- function isIdentifierThatStartsWithUnderscore(node) {
- return ts.isIdentifier(node) && ts.idText(node).charCodeAt(0) === 95 /* _ */;
+ function checkDeclarationInitializer(declaration, contextualType) {
+ var initializer = ts.getEffectiveInitializer(declaration);
+ var type = getQuickTypeOfExpression(initializer) ||
+ (contextualType ? checkExpressionWithContextualType(initializer, contextualType, /*inferenceContext*/ undefined, 0 /* Normal */) : checkExpressionCached(initializer));
+ return ts.isParameter(declaration) && declaration.name.kind === 197 /* ArrayBindingPattern */ &&
+ isTupleType(type) && !type.target.hasRestElement && getTypeReferenceArity(type) < declaration.name.elements.length ?
+ padTupleType(type, declaration.name) : type;
}
- function checkUnusedClassMembers(node, addDiagnostic) {
- for (var _i = 0, _a = node.members; _i < _a.length; _i++) {
- var member = _a[_i];
- switch (member.kind) {
- case 161 /* MethodDeclaration */:
- case 159 /* PropertyDeclaration */:
- case 163 /* GetAccessor */:
- case 164 /* SetAccessor */:
- if (member.kind === 164 /* SetAccessor */ && member.symbol.flags & 32768 /* GetAccessor */) {
- // Already would have reported an error on the getter.
- break;
- }
- var symbol = getSymbolOfNode(member);
- if (!symbol.isReferenced
- && (ts.hasModifier(member, 8 /* Private */) || ts.isNamedDeclaration(member) && ts.isPrivateIdentifier(member.name))
- && !(member.flags & 8388608 /* Ambient */)) {
- addDiagnostic(member, 0 /* Local */, ts.createDiagnosticForNode(member.name, ts.Diagnostics._0_is_declared_but_its_value_is_never_read, symbolToString(symbol)));
- }
- break;
- case 162 /* Constructor */:
- for (var _b = 0, _c = member.parameters; _b < _c.length; _b++) {
- var parameter = _c[_b];
- if (!parameter.symbol.isReferenced && ts.hasModifier(parameter, 8 /* Private */)) {
- addDiagnostic(parameter, 0 /* Local */, ts.createDiagnosticForNode(parameter.name, ts.Diagnostics.Property_0_is_declared_but_its_value_is_never_read, ts.symbolName(parameter.symbol)));
- }
- }
- break;
- case 167 /* IndexSignature */:
- case 222 /* SemicolonClassElement */:
- // Can't be private
- break;
- default:
- ts.Debug.fail();
+ function padTupleType(type, pattern) {
+ var patternElements = pattern.elements;
+ var elementTypes = getTypeArguments(type).slice();
+ var elementFlags = type.target.elementFlags.slice();
+ for (var i = getTypeReferenceArity(type); i < patternElements.length; i++) {
+ var e = patternElements[i];
+ if (i < patternElements.length - 1 || !(e.kind === 198 /* BindingElement */ && e.dotDotDotToken)) {
+ elementTypes.push(!ts.isOmittedExpression(e) && hasDefaultValue(e) ? getTypeFromBindingElement(e, /*includePatternInType*/ false, /*reportErrors*/ false) : anyType);
+ elementFlags.push(2 /* Optional */);
+ if (!ts.isOmittedExpression(e) && !hasDefaultValue(e)) {
+ reportImplicitAny(e, anyType);
+ }
}
}
+ return createTupleType(elementTypes, elementFlags, type.target.readonly);
}
- function checkUnusedInferTypeParameter(node, addDiagnostic) {
- var typeParameter = node.typeParameter;
- if (isTypeParameterUnused(typeParameter)) {
- addDiagnostic(node, 1 /* Parameter */, ts.createDiagnosticForNode(node, ts.Diagnostics._0_is_declared_but_its_value_is_never_read, ts.idText(typeParameter.name)));
+ function widenTypeInferredFromInitializer(declaration, type) {
+ var widened = ts.getCombinedNodeFlags(declaration) & 2 /* Const */ || ts.isDeclarationReadonly(declaration) ? type : getWidenedLiteralType(type);
+ if (ts.isInJSFile(declaration)) {
+ if (widened.flags & 98304 /* Nullable */) {
+ reportImplicitAny(declaration, anyType);
+ return anyType;
+ }
+ else if (isEmptyArrayLiteralType(widened)) {
+ reportImplicitAny(declaration, anyArrayType);
+ return anyArrayType;
+ }
}
+ return widened;
}
- function checkUnusedTypeParameters(node, addDiagnostic) {
- // Only report errors on the last declaration for the type parameter container;
- // this ensures that all uses have been accounted for.
- if (ts.last(getSymbolOfNode(node).declarations) !== node)
- return;
- var typeParameters = ts.getEffectiveTypeParameterDeclarations(node);
- var seenParentsWithEveryUnused = new ts.NodeSet();
- for (var _i = 0, typeParameters_3 = typeParameters; _i < typeParameters_3.length; _i++) {
- var typeParameter = typeParameters_3[_i];
- if (!isTypeParameterUnused(typeParameter))
- continue;
- var name = ts.idText(typeParameter.name);
- var parent = typeParameter.parent;
- if (parent.kind !== 181 /* InferType */ && parent.typeParameters.every(isTypeParameterUnused)) {
- if (seenParentsWithEveryUnused.tryAdd(parent)) {
- var range = ts.isJSDocTemplateTag(parent)
- // Whole @template tag
- ? ts.rangeOfNode(parent)
- // Include the `<>` in the error message
- : ts.rangeOfTypeParameters(parent.typeParameters);
- var only = parent.typeParameters.length === 1;
- var message = only ? ts.Diagnostics._0_is_declared_but_its_value_is_never_read : ts.Diagnostics.All_type_parameters_are_unused;
- var arg0 = only ? name : undefined;
- addDiagnostic(typeParameter, 1 /* Parameter */, ts.createFileDiagnostic(ts.getSourceFileOfNode(parent), range.pos, range.end - range.pos, message, arg0));
- }
+ function isLiteralOfContextualType(candidateType, contextualType) {
+ if (contextualType) {
+ if (contextualType.flags & 3145728 /* UnionOrIntersection */) {
+ var types = contextualType.types;
+ return ts.some(types, function (t) { return isLiteralOfContextualType(candidateType, t); });
}
- else {
- addDiagnostic(typeParameter, 1 /* Parameter */, ts.createDiagnosticForNode(typeParameter, ts.Diagnostics._0_is_declared_but_its_value_is_never_read, name));
+ if (contextualType.flags & 58982400 /* InstantiableNonPrimitive */) {
+ // If the contextual type is a type variable constrained to a primitive type, consider
+ // this a literal context for literals of that primitive type. For example, given a
+ // type parameter 'T extends string', infer string literal types for T.
+ var constraint = getBaseConstraintOfType(contextualType) || unknownType;
+ return maybeTypeOfKind(constraint, 4 /* String */) && maybeTypeOfKind(candidateType, 128 /* StringLiteral */) ||
+ maybeTypeOfKind(constraint, 8 /* Number */) && maybeTypeOfKind(candidateType, 256 /* NumberLiteral */) ||
+ maybeTypeOfKind(constraint, 64 /* BigInt */) && maybeTypeOfKind(candidateType, 2048 /* BigIntLiteral */) ||
+ maybeTypeOfKind(constraint, 4096 /* ESSymbol */) && maybeTypeOfKind(candidateType, 8192 /* UniqueESSymbol */) ||
+ isLiteralOfContextualType(candidateType, constraint);
}
+ // If the contextual type is a literal of a particular primitive type, we consider this a
+ // literal context for all literals of that primitive type.
+ return !!(contextualType.flags & (128 /* StringLiteral */ | 4194304 /* Index */ | 134217728 /* TemplateLiteral */ | 268435456 /* StringMapping */) && maybeTypeOfKind(candidateType, 128 /* StringLiteral */) ||
+ contextualType.flags & 256 /* NumberLiteral */ && maybeTypeOfKind(candidateType, 256 /* NumberLiteral */) ||
+ contextualType.flags & 2048 /* BigIntLiteral */ && maybeTypeOfKind(candidateType, 2048 /* BigIntLiteral */) ||
+ contextualType.flags & 512 /* BooleanLiteral */ && maybeTypeOfKind(candidateType, 512 /* BooleanLiteral */) ||
+ contextualType.flags & 8192 /* UniqueESSymbol */ && maybeTypeOfKind(candidateType, 8192 /* UniqueESSymbol */));
}
+ return false;
}
- function isTypeParameterUnused(typeParameter) {
- return !(getMergedSymbol(typeParameter.symbol).isReferenced & 262144 /* TypeParameter */) && !isIdentifierThatStartsWithUnderscore(typeParameter.name);
+ function isConstContext(node) {
+ var parent = node.parent;
+ return ts.isAssertionExpression(parent) && ts.isConstTypeReference(parent.type) ||
+ (ts.isParenthesizedExpression(parent) || ts.isArrayLiteralExpression(parent) || ts.isSpreadElement(parent)) && isConstContext(parent) ||
+ (ts.isPropertyAssignment(parent) || ts.isShorthandPropertyAssignment(parent) || ts.isTemplateSpan(parent)) && isConstContext(parent.parent);
}
- function addToGroup(map, key, value, getKey) {
- var keyString = String(getKey(key));
- var group = map.get(keyString);
- if (group) {
- group[1].push(value);
- }
- else {
- map.set(keyString, [key, [value]]);
- }
+ function checkExpressionForMutableLocation(node, checkMode, contextualType, forceTuple) {
+ var type = checkExpression(node, checkMode, forceTuple);
+ return isConstContext(node) ? getRegularTypeOfLiteralType(type) :
+ isTypeAssertion(node) ? type :
+ getWidenedLiteralLikeTypeForContextualType(type, instantiateContextualType(arguments.length === 2 ? getContextualType(node) : contextualType, node));
}
- function tryGetRootParameterDeclaration(node) {
- return ts.tryCast(ts.getRootDeclaration(node), ts.isParameter);
+ function checkPropertyAssignment(node, checkMode) {
+ // Do not use hasDynamicName here, because that returns false for well known symbols.
+ // We want to perform checkComputedPropertyName for all computed properties, including
+ // well known symbols.
+ if (node.name.kind === 158 /* ComputedPropertyName */) {
+ checkComputedPropertyName(node.name);
+ }
+ return checkExpressionForMutableLocation(node.initializer, checkMode);
}
- function isValidUnusedLocalDeclaration(declaration) {
- if (ts.isBindingElement(declaration) && isIdentifierThatStartsWithUnderscore(declaration.name)) {
- return !!ts.findAncestor(declaration.parent, function (ancestor) {
- return ts.isArrayBindingPattern(ancestor) || ts.isVariableDeclaration(ancestor) || ts.isVariableDeclarationList(ancestor) ? false :
- ts.isForOfStatement(ancestor) ? true : "quit";
- });
+ function checkObjectLiteralMethod(node, checkMode) {
+ // Grammar checking
+ checkGrammarMethod(node);
+ // Do not use hasDynamicName here, because that returns false for well known symbols.
+ // We want to perform checkComputedPropertyName for all computed properties, including
+ // well known symbols.
+ if (node.name.kind === 158 /* ComputedPropertyName */) {
+ checkComputedPropertyName(node.name);
}
- return ts.isAmbientModule(declaration) ||
- (ts.isVariableDeclaration(declaration) && ts.isForInOrOfStatement(declaration.parent.parent) || isImportedDeclaration(declaration)) && isIdentifierThatStartsWithUnderscore(declaration.name);
+ var uninstantiatedType = checkFunctionExpressionOrObjectLiteralMethod(node, checkMode);
+ return instantiateTypeWithSingleGenericCallSignature(node, uninstantiatedType, checkMode);
}
- function checkUnusedLocalsAndParameters(nodeWithLocals, addDiagnostic) {
- // Ideally we could use the ImportClause directly as a key, but must wait until we have full ES6 maps. So must store key along with value.
- var unusedImports = ts.createMap();
- var unusedDestructures = ts.createMap();
- var unusedVariables = ts.createMap();
- nodeWithLocals.locals.forEach(function (local) {
- // If it's purely a type parameter, ignore, will be checked in `checkUnusedTypeParameters`.
- // If it's a type parameter merged with a parameter, check if the parameter-side is used.
- if (local.flags & 262144 /* TypeParameter */ ? !(local.flags & 3 /* Variable */ && !(local.isReferenced & 3 /* Variable */)) : local.isReferenced || local.exportSymbol) {
- return;
- }
- for (var _i = 0, _a = local.declarations; _i < _a.length; _i++) {
- var declaration = _a[_i];
- if (isValidUnusedLocalDeclaration(declaration)) {
- continue;
- }
- if (isImportedDeclaration(declaration)) {
- addToGroup(unusedImports, importClauseFromImported(declaration), declaration, getNodeId);
- }
- else if (ts.isBindingElement(declaration) && ts.isObjectBindingPattern(declaration.parent)) {
- // In `{ a, ...b }, `a` is considered used since it removes a property from `b`. `b` may still be unused though.
- var lastElement = ts.last(declaration.parent.elements);
- if (declaration === lastElement || !ts.last(declaration.parent.elements).dotDotDotToken) {
- addToGroup(unusedDestructures, declaration.parent, declaration, getNodeId);
- }
- }
- else if (ts.isVariableDeclaration(declaration)) {
- addToGroup(unusedVariables, declaration.parent, declaration, getNodeId);
- }
- else {
- var parameter = local.valueDeclaration && tryGetRootParameterDeclaration(local.valueDeclaration);
- var name = local.valueDeclaration && ts.getNameOfDeclaration(local.valueDeclaration);
- if (parameter && name) {
- if (!ts.isParameterPropertyDeclaration(parameter, parameter.parent) && !ts.parameterIsThisKeyword(parameter) && !isIdentifierThatStartsWithUnderscore(name)) {
- addDiagnostic(parameter, 1 /* Parameter */, ts.createDiagnosticForNode(name, ts.Diagnostics._0_is_declared_but_its_value_is_never_read, ts.symbolName(local)));
+ function instantiateTypeWithSingleGenericCallSignature(node, type, checkMode) {
+ if (checkMode && checkMode & (2 /* Inferential */ | 8 /* SkipGenericFunctions */)) {
+ var callSignature = getSingleSignature(type, 0 /* Call */, /*allowMembers*/ true);
+ var constructSignature = getSingleSignature(type, 1 /* Construct */, /*allowMembers*/ true);
+ var signature = callSignature || constructSignature;
+ if (signature && signature.typeParameters) {
+ var contextualType = getApparentTypeOfContextualType(node, 2 /* NoConstraints */);
+ if (contextualType) {
+ var contextualSignature = getSingleSignature(getNonNullableType(contextualType), callSignature ? 0 /* Call */ : 1 /* Construct */, /*allowMembers*/ false);
+ if (contextualSignature && !contextualSignature.typeParameters) {
+ if (checkMode & 8 /* SkipGenericFunctions */) {
+ skippedGenericFunction(node, checkMode);
+ return anyFunctionType;
}
- }
- else {
- errorUnusedLocal(declaration, ts.symbolName(local), addDiagnostic);
+ var context = getInferenceContext(node);
+ // We have an expression that is an argument of a generic function for which we are performing
+ // type argument inference. The expression is of a function type with a single generic call
+ // signature and a contextual function type with a single non-generic call signature. Now check
+ // if the outer function returns a function type with a single non-generic call signature and
+ // if some of the outer function type parameters have no inferences so far. If so, we can
+ // potentially add inferred type parameters to the outer function return type.
+ var returnType = context.signature && getReturnTypeOfSignature(context.signature);
+ var returnSignature = returnType && getSingleCallOrConstructSignature(returnType);
+ if (returnSignature && !returnSignature.typeParameters && !ts.every(context.inferences, hasInferenceCandidates)) {
+ // Instantiate the signature with its own type parameters as type arguments, possibly
+ // renaming the type parameters to ensure they have unique names.
+ var uniqueTypeParameters = getUniqueTypeParameters(context, signature.typeParameters);
+ var instantiatedSignature = getSignatureInstantiationWithoutFillingInTypeArguments(signature, uniqueTypeParameters);
+ // Infer from the parameters of the instantiated signature to the parameters of the
+ // contextual signature starting with an empty set of inference candidates.
+ var inferences_3 = ts.map(context.inferences, function (info) { return createInferenceInfo(info.typeParameter); });
+ applyToParameterTypes(instantiatedSignature, contextualSignature, function (source, target) {
+ inferTypes(inferences_3, source, target, /*priority*/ 0, /*contravariant*/ true);
+ });
+ if (ts.some(inferences_3, hasInferenceCandidates)) {
+ // We have inference candidates, indicating that one or more type parameters are referenced
+ // in the parameter types of the contextual signature. Now also infer from the return type.
+ applyToReturnTypes(instantiatedSignature, contextualSignature, function (source, target) {
+ inferTypes(inferences_3, source, target);
+ });
+ // If the type parameters for which we produced candidates do not have any inferences yet,
+ // we adopt the new inference candidates and add the type parameters of the expression type
+ // to the set of inferred type parameters for the outer function return type.
+ if (!hasOverlappingInferences(context.inferences, inferences_3)) {
+ mergeInferences(context.inferences, inferences_3);
+ context.inferredTypeParameters = ts.concatenate(context.inferredTypeParameters, uniqueTypeParameters);
+ return getOrCreateTypeFromSignature(instantiatedSignature);
+ }
+ }
+ }
+ return getOrCreateTypeFromSignature(instantiateSignatureInContextOf(signature, contextualSignature, context));
}
}
}
- });
- unusedImports.forEach(function (_a) {
- var importClause = _a[0], unuseds = _a[1];
- var importDecl = importClause.parent;
- var nDeclarations = (importClause.name ? 1 : 0) +
- (importClause.namedBindings ?
- (importClause.namedBindings.kind === 256 /* NamespaceImport */ ? 1 : importClause.namedBindings.elements.length)
- : 0);
- if (nDeclarations === unuseds.length) {
- addDiagnostic(importDecl, 0 /* Local */, unuseds.length === 1
- ? ts.createDiagnosticForNode(importDecl, ts.Diagnostics._0_is_declared_but_its_value_is_never_read, ts.idText(ts.first(unuseds).name))
- : ts.createDiagnosticForNode(importDecl, ts.Diagnostics.All_imports_in_import_declaration_are_unused));
+ }
+ return type;
+ }
+ function skippedGenericFunction(node, checkMode) {
+ if (checkMode & 2 /* Inferential */) {
+ // We have skipped a generic function during inferential typing. Obtain the inference context and
+ // indicate this has occurred such that we know a second pass of inference is be needed.
+ var context = getInferenceContext(node);
+ context.flags |= 4 /* SkippedGenericFunction */;
+ }
+ }
+ function hasInferenceCandidates(info) {
+ return !!(info.candidates || info.contraCandidates);
+ }
+ function hasOverlappingInferences(a, b) {
+ for (var i = 0; i < a.length; i++) {
+ if (hasInferenceCandidates(a[i]) && hasInferenceCandidates(b[i])) {
+ return true;
}
- else {
- for (var _i = 0, unuseds_1 = unuseds; _i < unuseds_1.length; _i++) {
- var unused = unuseds_1[_i];
- errorUnusedLocal(unused, ts.idText(unused.name), addDiagnostic);
- }
+ }
+ return false;
+ }
+ function mergeInferences(target, source) {
+ for (var i = 0; i < target.length; i++) {
+ if (!hasInferenceCandidates(target[i]) && hasInferenceCandidates(source[i])) {
+ target[i] = source[i];
}
- });
- unusedDestructures.forEach(function (_a) {
- var bindingPattern = _a[0], bindingElements = _a[1];
- var kind = tryGetRootParameterDeclaration(bindingPattern.parent) ? 1 /* Parameter */ : 0 /* Local */;
- if (bindingPattern.elements.length === bindingElements.length) {
- if (bindingElements.length === 1 && bindingPattern.parent.kind === 242 /* VariableDeclaration */ && bindingPattern.parent.parent.kind === 243 /* VariableDeclarationList */) {
- addToGroup(unusedVariables, bindingPattern.parent.parent, bindingPattern.parent, getNodeId);
- }
- else {
- addDiagnostic(bindingPattern, kind, bindingElements.length === 1
- ? ts.createDiagnosticForNode(bindingPattern, ts.Diagnostics._0_is_declared_but_its_value_is_never_read, bindingNameText(ts.first(bindingElements).name))
- : ts.createDiagnosticForNode(bindingPattern, ts.Diagnostics.All_destructured_elements_are_unused));
- }
+ }
+ }
+ function getUniqueTypeParameters(context, typeParameters) {
+ var result = [];
+ var oldTypeParameters;
+ var newTypeParameters;
+ for (var _i = 0, typeParameters_2 = typeParameters; _i < typeParameters_2.length; _i++) {
+ var tp = typeParameters_2[_i];
+ var name = tp.symbol.escapedName;
+ if (hasTypeParameterByName(context.inferredTypeParameters, name) || hasTypeParameterByName(result, name)) {
+ var newName = getUniqueTypeParameterName(ts.concatenate(context.inferredTypeParameters, result), name);
+ var symbol = createSymbol(262144 /* TypeParameter */, newName);
+ var newTypeParameter = createTypeParameter(symbol);
+ newTypeParameter.target = tp;
+ oldTypeParameters = ts.append(oldTypeParameters, tp);
+ newTypeParameters = ts.append(newTypeParameters, newTypeParameter);
+ result.push(newTypeParameter);
}
else {
- for (var _i = 0, bindingElements_1 = bindingElements; _i < bindingElements_1.length; _i++) {
- var e = bindingElements_1[_i];
- addDiagnostic(e, kind, ts.createDiagnosticForNode(e, ts.Diagnostics._0_is_declared_but_its_value_is_never_read, bindingNameText(e.name)));
- }
+ result.push(tp);
}
- });
- unusedVariables.forEach(function (_a) {
- var declarationList = _a[0], declarations = _a[1];
- if (declarationList.declarations.length === declarations.length) {
- addDiagnostic(declarationList, 0 /* Local */, declarations.length === 1
- ? ts.createDiagnosticForNode(ts.first(declarations).name, ts.Diagnostics._0_is_declared_but_its_value_is_never_read, bindingNameText(ts.first(declarations).name))
- : ts.createDiagnosticForNode(declarationList.parent.kind === 225 /* VariableStatement */ ? declarationList.parent : declarationList, ts.Diagnostics.All_variables_are_unused));
+ }
+ if (newTypeParameters) {
+ var mapper = createTypeMapper(oldTypeParameters, newTypeParameters);
+ for (var _a = 0, newTypeParameters_1 = newTypeParameters; _a < newTypeParameters_1.length; _a++) {
+ var tp = newTypeParameters_1[_a];
+ tp.mapper = mapper;
}
- else {
- for (var _i = 0, declarations_5 = declarations; _i < declarations_5.length; _i++) {
- var decl = declarations_5[_i];
- addDiagnostic(decl, 0 /* Local */, ts.createDiagnosticForNode(decl, ts.Diagnostics._0_is_declared_but_its_value_is_never_read, bindingNameText(decl.name)));
- }
+ }
+ return result;
+ }
+ function hasTypeParameterByName(typeParameters, name) {
+ return ts.some(typeParameters, function (tp) { return tp.symbol.escapedName === name; });
+ }
+ function getUniqueTypeParameterName(typeParameters, baseName) {
+ var len = baseName.length;
+ while (len > 1 && baseName.charCodeAt(len - 1) >= 48 /* _0 */ && baseName.charCodeAt(len - 1) <= 57 /* _9 */)
+ len--;
+ var s = baseName.slice(0, len);
+ for (var index = 1; true; index++) {
+ var augmentedName = (s + index);
+ if (!hasTypeParameterByName(typeParameters, augmentedName)) {
+ return augmentedName;
}
- });
+ }
}
- function bindingNameText(name) {
- switch (name.kind) {
- case 75 /* Identifier */:
- return ts.idText(name);
- case 190 /* ArrayBindingPattern */:
- case 189 /* ObjectBindingPattern */:
- return bindingNameText(ts.cast(ts.first(name.elements), ts.isBindingElement).name);
- default:
- return ts.Debug.assertNever(name);
+ function getReturnTypeOfSingleNonGenericCallSignature(funcType) {
+ var signature = getSingleCallSignature(funcType);
+ if (signature && !signature.typeParameters) {
+ return getReturnTypeOfSignature(signature);
}
}
- function isImportedDeclaration(node) {
- return node.kind === 255 /* ImportClause */ || node.kind === 258 /* ImportSpecifier */ || node.kind === 256 /* NamespaceImport */;
+ function getReturnTypeOfSingleNonGenericSignatureOfCallChain(expr) {
+ var funcType = checkExpression(expr.expression);
+ var nonOptionalType = getOptionalExpressionType(funcType, expr.expression);
+ var returnType = getReturnTypeOfSingleNonGenericCallSignature(funcType);
+ return returnType && propagateOptionalTypeMarker(returnType, expr, nonOptionalType !== funcType);
}
- function importClauseFromImported(decl) {
- return decl.kind === 255 /* ImportClause */ ? decl : decl.kind === 256 /* NamespaceImport */ ? decl.parent : decl.parent.parent;
+ /**
+ * Returns the type of an expression. Unlike checkExpression, this function is simply concerned
+ * with computing the type and may not fully check all contained sub-expressions for errors.
+ */
+ function getTypeOfExpression(node) {
+ // Don't bother caching types that require no flow analysis and are quick to compute.
+ var quickType = getQuickTypeOfExpression(node);
+ if (quickType) {
+ return quickType;
+ }
+ // If a type has been cached for the node, return it.
+ if (node.flags & 67108864 /* TypeCached */ && flowTypeCache) {
+ var cachedType = flowTypeCache[getNodeId(node)];
+ if (cachedType) {
+ return cachedType;
+ }
+ }
+ var startInvocationCount = flowInvocationCount;
+ var type = checkExpression(node);
+ // If control flow analysis was required to determine the type, it is worth caching.
+ if (flowInvocationCount !== startInvocationCount) {
+ var cache = flowTypeCache || (flowTypeCache = []);
+ cache[getNodeId(node)] = type;
+ ts.setNodeFlags(node, node.flags | 67108864 /* TypeCached */);
+ }
+ return type;
}
- function checkBlock(node) {
- // Grammar checking for SyntaxKind.Block
- if (node.kind === 223 /* Block */) {
- checkGrammarStatementInAmbientContext(node);
+ function getQuickTypeOfExpression(node) {
+ var expr = ts.skipParentheses(node);
+ // Optimize for the common case of a call to a function with a single non-generic call
+ // signature where we can just fetch the return type without checking the arguments.
+ if (ts.isCallExpression(expr) && expr.expression.kind !== 105 /* SuperKeyword */ && !ts.isRequireCall(expr, /*checkArgumentIsStringLiteralLike*/ true) && !isSymbolOrSymbolForCall(expr)) {
+ var type = ts.isCallChain(expr) ? getReturnTypeOfSingleNonGenericSignatureOfCallChain(expr) :
+ getReturnTypeOfSingleNonGenericCallSignature(checkNonNullExpression(expr.expression));
+ if (type) {
+ return type;
+ }
}
- if (ts.isFunctionOrModuleBlock(node)) {
- var saveFlowAnalysisDisabled = flowAnalysisDisabled;
- ts.forEach(node.statements, checkSourceElement);
- flowAnalysisDisabled = saveFlowAnalysisDisabled;
+ else if (ts.isAssertionExpression(expr) && !ts.isConstTypeReference(expr.type)) {
+ return getTypeFromTypeNode(expr.type);
}
- else {
- ts.forEach(node.statements, checkSourceElement);
+ else if (node.kind === 8 /* NumericLiteral */ || node.kind === 10 /* StringLiteral */ ||
+ node.kind === 109 /* TrueKeyword */ || node.kind === 94 /* FalseKeyword */) {
+ return checkExpression(node);
}
- if (node.locals) {
- registerForUnusedIdentifiersCheck(node);
+ return undefined;
+ }
+ /**
+ * Returns the type of an expression. Unlike checkExpression, this function is simply concerned
+ * with computing the type and may not fully check all contained sub-expressions for errors.
+ * It is intended for uses where you know there is no contextual type,
+ * and requesting the contextual type might cause a circularity or other bad behaviour.
+ * It sets the contextual type of the node to any before calling getTypeOfExpression.
+ */
+ function getContextFreeTypeOfExpression(node) {
+ var links = getNodeLinks(node);
+ if (links.contextFreeType) {
+ return links.contextFreeType;
+ }
+ var saveContextualType = node.contextualType;
+ node.contextualType = anyType;
+ try {
+ var type = links.contextFreeType = checkExpression(node, 4 /* SkipContextSensitive */);
+ return type;
+ }
+ finally {
+ // In the event our operation is canceled or some other exception occurs, reset the contextual type
+ // so that we do not accidentally hold onto an instance of the checker, as a Type created in the services layer
+ // may hold onto the checker that created it.
+ node.contextualType = saveContextualType;
}
}
- function checkCollisionWithArgumentsInGeneratedCode(node) {
- // no rest parameters \ declaration context \ overload - no codegen impact
- if (languageVersion >= 2 /* ES2015 */ || compilerOptions.noEmit || !ts.hasRestParameter(node) || node.flags & 8388608 /* Ambient */ || ts.nodeIsMissing(node.body)) {
- return;
+ function checkExpression(node, checkMode, forceTuple) {
+ ts.tracing.push("check" /* Check */, "checkExpression", { kind: node.kind, pos: node.pos, end: node.end });
+ var saveCurrentNode = currentNode;
+ currentNode = node;
+ instantiationCount = 0;
+ var uninstantiatedType = checkExpressionWorker(node, checkMode, forceTuple);
+ var type = instantiateTypeWithSingleGenericCallSignature(node, uninstantiatedType, checkMode);
+ if (isConstEnumObjectType(type)) {
+ checkConstEnumAccess(node, type);
}
- ts.forEach(node.parameters, function (p) {
- if (p.name && !ts.isBindingPattern(p.name) && p.name.escapedText === argumentsSymbol.escapedName) {
- error(p, ts.Diagnostics.Duplicate_identifier_arguments_Compiler_uses_arguments_to_initialize_rest_parameters);
+ currentNode = saveCurrentNode;
+ ts.tracing.pop();
+ return type;
+ }
+ function checkConstEnumAccess(node, type) {
+ // enum object type for const enums are only permitted in:
+ // - 'left' in property access
+ // - 'object' in indexed access
+ // - target in rhs of import statement
+ var ok = (node.parent.kind === 201 /* PropertyAccessExpression */ && node.parent.expression === node) ||
+ (node.parent.kind === 202 /* ElementAccessExpression */ && node.parent.expression === node) ||
+ ((node.kind === 78 /* Identifier */ || node.kind === 157 /* QualifiedName */) && isInRightSideOfImportOrExportAssignment(node) ||
+ (node.parent.kind === 176 /* TypeQuery */ && node.parent.exprName === node)) ||
+ (node.parent.kind === 270 /* ExportSpecifier */); // We allow reexporting const enums
+ if (!ok) {
+ error(node, ts.Diagnostics.const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment_or_type_query);
+ }
+ if (compilerOptions.isolatedModules) {
+ ts.Debug.assert(!!(type.symbol.flags & 128 /* ConstEnum */));
+ var constEnumDeclaration = type.symbol.valueDeclaration;
+ if (constEnumDeclaration.flags & 8388608 /* Ambient */) {
+ error(node, ts.Diagnostics.Cannot_access_ambient_const_enums_when_the_isolatedModules_flag_is_provided);
}
- });
+ }
}
- function needCollisionCheckForIdentifier(node, identifier, name) {
- if (!(identifier && identifier.escapedText === name)) {
- return false;
+ function checkParenthesizedExpression(node, checkMode) {
+ var tag = ts.isInJSFile(node) ? ts.getJSDocTypeTag(node) : undefined;
+ if (tag) {
+ return checkAssertionWorker(tag, tag.typeExpression.type, node.expression, checkMode);
}
- if (node.kind === 159 /* PropertyDeclaration */ ||
- node.kind === 158 /* PropertySignature */ ||
- node.kind === 161 /* MethodDeclaration */ ||
- node.kind === 160 /* MethodSignature */ ||
- node.kind === 163 /* GetAccessor */ ||
- node.kind === 164 /* SetAccessor */) {
- // it is ok to have member named '_super' or '_this' - member access is always qualified
- return false;
+ return checkExpression(node.expression, checkMode);
+ }
+ function checkExpressionWorker(node, checkMode, forceTuple) {
+ var kind = node.kind;
+ if (cancellationToken) {
+ // Only bother checking on a few construct kinds. We don't want to be excessively
+ // hitting the cancellation token on every node we check.
+ switch (kind) {
+ case 221 /* ClassExpression */:
+ case 208 /* FunctionExpression */:
+ case 209 /* ArrowFunction */:
+ cancellationToken.throwIfCancellationRequested();
+ }
+ }
+ switch (kind) {
+ case 78 /* Identifier */:
+ return checkIdentifier(node);
+ case 107 /* ThisKeyword */:
+ return checkThisExpression(node);
+ case 105 /* SuperKeyword */:
+ return checkSuperExpression(node);
+ case 103 /* NullKeyword */:
+ return nullWideningType;
+ case 14 /* NoSubstitutionTemplateLiteral */:
+ case 10 /* StringLiteral */:
+ return getFreshTypeOfLiteralType(getLiteralType(node.text));
+ case 8 /* NumericLiteral */:
+ checkGrammarNumericLiteral(node);
+ return getFreshTypeOfLiteralType(getLiteralType(+node.text));
+ case 9 /* BigIntLiteral */:
+ checkGrammarBigIntLiteral(node);
+ return getFreshTypeOfLiteralType(getBigIntLiteralType(node));
+ case 109 /* TrueKeyword */:
+ return trueType;
+ case 94 /* FalseKeyword */:
+ return falseType;
+ case 218 /* TemplateExpression */:
+ return checkTemplateExpression(node);
+ case 13 /* RegularExpressionLiteral */:
+ return globalRegExpType;
+ case 199 /* ArrayLiteralExpression */:
+ return checkArrayLiteral(node, checkMode, forceTuple);
+ case 200 /* ObjectLiteralExpression */:
+ return checkObjectLiteral(node, checkMode);
+ case 201 /* PropertyAccessExpression */:
+ return checkPropertyAccessExpression(node);
+ case 157 /* QualifiedName */:
+ return checkQualifiedName(node);
+ case 202 /* ElementAccessExpression */:
+ return checkIndexedAccess(node);
+ case 203 /* CallExpression */:
+ if (node.expression.kind === 99 /* ImportKeyword */) {
+ return checkImportCallExpression(node);
+ }
+ // falls through
+ case 204 /* NewExpression */:
+ return checkCallExpression(node, checkMode);
+ case 205 /* TaggedTemplateExpression */:
+ return checkTaggedTemplateExpression(node);
+ case 207 /* ParenthesizedExpression */:
+ return checkParenthesizedExpression(node, checkMode);
+ case 221 /* ClassExpression */:
+ return checkClassExpression(node);
+ case 208 /* FunctionExpression */:
+ case 209 /* ArrowFunction */:
+ return checkFunctionExpressionOrObjectLiteralMethod(node, checkMode);
+ case 211 /* TypeOfExpression */:
+ return checkTypeOfExpression(node);
+ case 206 /* TypeAssertionExpression */:
+ case 224 /* AsExpression */:
+ return checkAssertion(node);
+ case 225 /* NonNullExpression */:
+ return checkNonNullAssertion(node);
+ case 226 /* MetaProperty */:
+ return checkMetaProperty(node);
+ case 210 /* DeleteExpression */:
+ return checkDeleteExpression(node);
+ case 212 /* VoidExpression */:
+ return checkVoidExpression(node);
+ case 213 /* AwaitExpression */:
+ return checkAwaitExpression(node);
+ case 214 /* PrefixUnaryExpression */:
+ return checkPrefixUnaryExpression(node);
+ case 215 /* PostfixUnaryExpression */:
+ return checkPostfixUnaryExpression(node);
+ case 216 /* BinaryExpression */:
+ return checkBinaryExpression(node, checkMode);
+ case 217 /* ConditionalExpression */:
+ return checkConditionalExpression(node, checkMode);
+ case 220 /* SpreadElement */:
+ return checkSpreadExpression(node, checkMode);
+ case 222 /* OmittedExpression */:
+ return undefinedWideningType;
+ case 219 /* YieldExpression */:
+ return checkYieldExpression(node);
+ case 227 /* SyntheticExpression */:
+ return checkSyntheticExpression(node);
+ case 283 /* JsxExpression */:
+ return checkJsxExpression(node, checkMode);
+ case 273 /* JsxElement */:
+ return checkJsxElement(node, checkMode);
+ case 274 /* JsxSelfClosingElement */:
+ return checkJsxSelfClosingElement(node, checkMode);
+ case 277 /* JsxFragment */:
+ return checkJsxFragment(node);
+ case 281 /* JsxAttributes */:
+ return checkJsxAttributes(node, checkMode);
+ case 275 /* JsxOpeningElement */:
+ ts.Debug.fail("Shouldn't ever directly check a JsxOpeningElement");
+ }
+ return errorType;
+ }
+ // DECLARATION AND STATEMENT TYPE CHECKING
+ function checkTypeParameter(node) {
+ // Grammar Checking
+ if (node.expression) {
+ grammarErrorOnFirstToken(node.expression, ts.Diagnostics.Type_expected);
+ }
+ checkSourceElement(node.constraint);
+ checkSourceElement(node.default);
+ var typeParameter = getDeclaredTypeOfTypeParameter(getSymbolOfNode(node));
+ // Resolve base constraint to reveal circularity errors
+ getBaseConstraintOfType(typeParameter);
+ if (!hasNonCircularTypeParameterDefault(typeParameter)) {
+ error(node.default, ts.Diagnostics.Type_parameter_0_has_a_circular_default, typeToString(typeParameter));
}
- if (node.flags & 8388608 /* Ambient */) {
- // ambient context - no codegen impact
- return false;
+ var constraintType = getConstraintOfTypeParameter(typeParameter);
+ var defaultType = getDefaultFromTypeParameter(typeParameter);
+ if (constraintType && defaultType) {
+ checkTypeAssignableTo(defaultType, getTypeWithThisArgument(instantiateType(constraintType, makeUnaryTypeMapper(typeParameter, defaultType)), defaultType), node.default, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1);
}
- var root = ts.getRootDeclaration(node);
- if (root.kind === 156 /* Parameter */ && ts.nodeIsMissing(root.parent.body)) {
- // just an overload - no codegen impact
- return false;
+ if (produceDiagnostics) {
+ checkTypeNameIsReserved(node.name, ts.Diagnostics.Type_parameter_name_cannot_be_0);
}
- return true;
}
- // this function will run after checking the source file so 'CaptureThis' is correct for all nodes
- function checkIfThisIsCapturedInEnclosingScope(node) {
- ts.findAncestor(node, function (current) {
- if (getNodeCheckFlags(current) & 4 /* CaptureThis */) {
- var isDeclaration_1 = node.kind !== 75 /* Identifier */;
- if (isDeclaration_1) {
- error(ts.getNameOfDeclaration(node), ts.Diagnostics.Duplicate_identifier_this_Compiler_uses_variable_declaration_this_to_capture_this_reference);
- }
- else {
- error(node, ts.Diagnostics.Expression_resolves_to_variable_declaration_this_that_compiler_uses_to_capture_this_reference);
- }
- return true;
+ function checkParameter(node) {
+ // Grammar checking
+ // It is a SyntaxError if the Identifier "eval" or the Identifier "arguments" occurs as the
+ // Identifier in a PropertySetParameterList of a PropertyAssignment that is contained in strict code
+ // or if its FunctionBody is strict code(11.1.5).
+ checkGrammarDecoratorsAndModifiers(node);
+ checkVariableLikeDeclaration(node);
+ var func = ts.getContainingFunction(node);
+ if (ts.hasSyntacticModifier(node, 92 /* ParameterPropertyModifier */)) {
+ if (!(func.kind === 166 /* Constructor */ && ts.nodeIsPresent(func.body))) {
+ error(node, ts.Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation);
}
- return false;
- });
- }
- function checkIfNewTargetIsCapturedInEnclosingScope(node) {
- ts.findAncestor(node, function (current) {
- if (getNodeCheckFlags(current) & 8 /* CaptureNewTarget */) {
- var isDeclaration_2 = node.kind !== 75 /* Identifier */;
- if (isDeclaration_2) {
- error(ts.getNameOfDeclaration(node), ts.Diagnostics.Duplicate_identifier_newTarget_Compiler_uses_variable_declaration_newTarget_to_capture_new_target_meta_property_reference);
- }
- else {
- error(node, ts.Diagnostics.Expression_resolves_to_variable_declaration_newTarget_that_compiler_uses_to_capture_new_target_meta_property_reference);
- }
- return true;
+ if (func.kind === 166 /* Constructor */ && ts.isIdentifier(node.name) && node.name.escapedText === "constructor") {
+ error(node.name, ts.Diagnostics.constructor_cannot_be_used_as_a_parameter_property_name);
}
- return false;
- });
- }
- function checkWeakMapCollision(node) {
- var enclosingBlockScope = ts.getEnclosingBlockScopeContainer(node);
- if (getNodeCheckFlags(enclosingBlockScope) & 67108864 /* ContainsClassWithPrivateIdentifiers */) {
- error(node, ts.Diagnostics.Compiler_reserves_name_0_when_emitting_private_identifier_downlevel, "WeakMap");
- }
- }
- function checkCollisionWithRequireExportsInGeneratedCode(node, name) {
- // No need to check for require or exports for ES6 modules and later
- if (moduleKind >= ts.ModuleKind.ES2015 || compilerOptions.noEmit) {
- return;
}
- if (!needCollisionCheckForIdentifier(node, name, "require") && !needCollisionCheckForIdentifier(node, name, "exports")) {
- return;
+ if (node.questionToken && ts.isBindingPattern(node.name) && func.body) {
+ error(node, ts.Diagnostics.A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature);
}
- // Uninstantiated modules shouldnt do this check
- if (ts.isModuleDeclaration(node) && ts.getModuleInstanceState(node) !== 1 /* Instantiated */) {
- return;
+ if (node.name && ts.isIdentifier(node.name) && (node.name.escapedText === "this" || node.name.escapedText === "new")) {
+ if (func.parameters.indexOf(node) !== 0) {
+ error(node, ts.Diagnostics.A_0_parameter_must_be_the_first_parameter, node.name.escapedText);
+ }
+ if (func.kind === 166 /* Constructor */ || func.kind === 170 /* ConstructSignature */ || func.kind === 175 /* ConstructorType */) {
+ error(node, ts.Diagnostics.A_constructor_cannot_have_a_this_parameter);
+ }
+ if (func.kind === 209 /* ArrowFunction */) {
+ error(node, ts.Diagnostics.An_arrow_function_cannot_have_a_this_parameter);
+ }
+ if (func.kind === 167 /* GetAccessor */ || func.kind === 168 /* SetAccessor */) {
+ error(node, ts.Diagnostics.get_and_set_accessors_cannot_declare_this_parameters);
+ }
}
- // In case of variable declaration, node.parent is variable statement so look at the variable statement's parent
- var parent = getDeclarationContainer(node);
- if (parent.kind === 290 /* SourceFile */ && ts.isExternalOrCommonJsModule(parent)) {
- // If the declaration happens to be in external module, report error that require and exports are reserved keywords
- error(name, ts.Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module, ts.declarationNameToString(name), ts.declarationNameToString(name));
+ // Only check rest parameter type if it's not a binding pattern. Since binding patterns are
+ // not allowed in a rest parameter, we already have an error from checkGrammarParameterList.
+ if (node.dotDotDotToken && !ts.isBindingPattern(node.name) && !isTypeAssignableTo(getReducedType(getTypeOfSymbol(node.symbol)), anyReadonlyArrayType)) {
+ error(node, ts.Diagnostics.A_rest_parameter_must_be_of_an_array_type);
}
}
- function checkCollisionWithGlobalPromiseInGeneratedCode(node, name) {
- if (languageVersion >= 4 /* ES2017 */ || compilerOptions.noEmit || !needCollisionCheckForIdentifier(node, name, "Promise")) {
- return;
- }
- // Uninstantiated modules shouldnt do this check
- if (ts.isModuleDeclaration(node) && ts.getModuleInstanceState(node) !== 1 /* Instantiated */) {
+ function checkTypePredicate(node) {
+ var parent = getTypePredicateParent(node);
+ if (!parent) {
+ // The parent must not be valid.
+ error(node, ts.Diagnostics.A_type_predicate_is_only_allowed_in_return_type_position_for_functions_and_methods);
return;
}
- // In case of variable declaration, node.parent is variable statement so look at the variable statement's parent
- var parent = getDeclarationContainer(node);
- if (parent.kind === 290 /* SourceFile */ && ts.isExternalOrCommonJsModule(parent) && parent.flags & 2048 /* HasAsyncFunctions */) {
- // If the declaration happens to be in external module, report error that Promise is a reserved identifier.
- error(name, ts.Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module_containing_async_functions, ts.declarationNameToString(name), ts.declarationNameToString(name));
- }
- }
- function checkVarDeclaredNamesNotShadowed(node) {
- // - ScriptBody : StatementList
- // It is a Syntax Error if any element of the LexicallyDeclaredNames of StatementList
- // also occurs in the VarDeclaredNames of StatementList.
- // - Block : { StatementList }
- // It is a Syntax Error if any element of the LexicallyDeclaredNames of StatementList
- // also occurs in the VarDeclaredNames of StatementList.
- // Variable declarations are hoisted to the top of their function scope. They can shadow
- // block scoped declarations, which bind tighter. this will not be flagged as duplicate definition
- // by the binder as the declaration scope is different.
- // A non-initialized declaration is a no-op as the block declaration will resolve before the var
- // declaration. the problem is if the declaration has an initializer. this will act as a write to the
- // block declared value. this is fine for let, but not const.
- // Only consider declarations with initializers, uninitialized const declarations will not
- // step on a let/const variable.
- // Do not consider const and const declarations, as duplicate block-scoped declarations
- // are handled by the binder.
- // We are only looking for const declarations that step on let\const declarations from a
- // different scope. e.g.:
- // {
- // const x = 0; // localDeclarationSymbol obtained after name resolution will correspond to this declaration
- // const x = 0; // symbol for this declaration will be 'symbol'
- // }
- // skip block-scoped variables and parameters
- if ((ts.getCombinedNodeFlags(node) & 3 /* BlockScoped */) !== 0 || ts.isParameterDeclaration(node)) {
+ var signature = getSignatureFromDeclaration(parent);
+ var typePredicate = getTypePredicateOfSignature(signature);
+ if (!typePredicate) {
return;
}
- // skip variable declarations that don't have initializers
- // NOTE: in ES6 spec initializer is required in variable declarations where name is binding pattern
- // so we'll always treat binding elements as initialized
- if (node.kind === 242 /* VariableDeclaration */ && !node.initializer) {
- return;
+ checkSourceElement(node.type);
+ var parameterName = node.parameterName;
+ if (typePredicate.kind === 0 /* This */ || typePredicate.kind === 2 /* AssertsThis */) {
+ getTypeFromThisTypeNode(parameterName);
}
- var symbol = getSymbolOfNode(node);
- if (symbol.flags & 1 /* FunctionScopedVariable */) {
- if (!ts.isIdentifier(node.name))
- return ts.Debug.fail();
- var localDeclarationSymbol = resolveName(node, node.name.escapedText, 3 /* Variable */, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ false);
- if (localDeclarationSymbol &&
- localDeclarationSymbol !== symbol &&
- localDeclarationSymbol.flags & 2 /* BlockScopedVariable */) {
- if (getDeclarationNodeFlagsFromSymbol(localDeclarationSymbol) & 3 /* BlockScoped */) {
- var varDeclList = ts.getAncestor(localDeclarationSymbol.valueDeclaration, 243 /* VariableDeclarationList */);
- var container = varDeclList.parent.kind === 225 /* VariableStatement */ && varDeclList.parent.parent
- ? varDeclList.parent.parent
- : undefined;
- // names of block-scoped and function scoped variables can collide only
- // if block scoped variable is defined in the function\module\source file scope (because of variable hoisting)
- var namesShareScope = container &&
- (container.kind === 223 /* Block */ && ts.isFunctionLike(container.parent) ||
- container.kind === 250 /* ModuleBlock */ ||
- container.kind === 249 /* ModuleDeclaration */ ||
- container.kind === 290 /* SourceFile */);
- // here we know that function scoped variable is shadowed by block scoped one
- // if they are defined in the same scope - binder has already reported redeclaration error
- // otherwise if variable has an initializer - show error that initialization will fail
- // since LHS will be block scoped name instead of function scoped
- if (!namesShareScope) {
- var name = symbolToString(localDeclarationSymbol);
- error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name, name);
+ else {
+ if (typePredicate.parameterIndex >= 0) {
+ if (signatureHasRestParameter(signature) && typePredicate.parameterIndex === signature.parameters.length - 1) {
+ error(parameterName, ts.Diagnostics.A_type_predicate_cannot_reference_a_rest_parameter);
+ }
+ else {
+ if (typePredicate.type) {
+ var leadingError = function () { return ts.chainDiagnosticMessages(/*details*/ undefined, ts.Diagnostics.A_type_predicate_s_type_must_be_assignable_to_its_parameter_s_type); };
+ checkTypeAssignableTo(typePredicate.type, getTypeOfSymbol(signature.parameters[typePredicate.parameterIndex]), node.type,
+ /*headMessage*/ undefined, leadingError);
+ }
+ }
+ }
+ else if (parameterName) {
+ var hasReportedError = false;
+ for (var _i = 0, _a = parent.parameters; _i < _a.length; _i++) {
+ var name = _a[_i].name;
+ if (ts.isBindingPattern(name) &&
+ checkIfTypePredicateVariableIsDeclaredInBindingPattern(name, parameterName, typePredicate.parameterName)) {
+ hasReportedError = true;
+ break;
}
}
+ if (!hasReportedError) {
+ error(node.parameterName, ts.Diagnostics.Cannot_find_parameter_0, typePredicate.parameterName);
+ }
}
}
}
- function convertAutoToAny(type) {
- return type === autoType ? anyType : type === autoArrayType ? anyArrayType : type;
- }
- // Check variable, parameter, or property declaration
- function checkVariableLikeDeclaration(node) {
- checkDecorators(node);
- if (!ts.isBindingElement(node)) {
- checkSourceElement(node.type);
- }
- // JSDoc `function(string, string): string` syntax results in parameters with no name
- if (!node.name) {
- return;
- }
- // For a computed property, just check the initializer and exit
- // Do not use hasDynamicName here, because that returns false for well known symbols.
- // We want to perform checkComputedPropertyName for all computed properties, including
- // well known symbols.
- if (node.name.kind === 154 /* ComputedPropertyName */) {
- checkComputedPropertyName(node.name);
- if (node.initializer) {
- checkExpressionCached(node.initializer);
- }
+ function getTypePredicateParent(node) {
+ switch (node.parent.kind) {
+ case 209 /* ArrowFunction */:
+ case 169 /* CallSignature */:
+ case 251 /* FunctionDeclaration */:
+ case 208 /* FunctionExpression */:
+ case 174 /* FunctionType */:
+ case 165 /* MethodDeclaration */:
+ case 164 /* MethodSignature */:
+ var parent = node.parent;
+ if (node === parent.type) {
+ return parent;
+ }
}
- if (node.kind === 191 /* BindingElement */) {
- if (node.parent.kind === 189 /* ObjectBindingPattern */ && languageVersion < 99 /* ESNext */) {
- checkExternalEmitHelpers(node, 4 /* Rest */);
+ }
+ function checkIfTypePredicateVariableIsDeclaredInBindingPattern(pattern, predicateVariableNode, predicateVariableName) {
+ for (var _i = 0, _a = pattern.elements; _i < _a.length; _i++) {
+ var element = _a[_i];
+ if (ts.isOmittedExpression(element)) {
+ continue;
}
- // check computed properties inside property names of binding elements
- if (node.propertyName && node.propertyName.kind === 154 /* ComputedPropertyName */) {
- checkComputedPropertyName(node.propertyName);
+ var name = element.name;
+ if (name.kind === 78 /* Identifier */ && name.escapedText === predicateVariableName) {
+ error(predicateVariableNode, ts.Diagnostics.A_type_predicate_cannot_reference_element_0_in_a_binding_pattern, predicateVariableName);
+ return true;
}
- // check private/protected variable access
- var parent = node.parent.parent;
- var parentType = getTypeForBindingElementParent(parent);
- var name = node.propertyName || node.name;
- if (parentType && !ts.isBindingPattern(name)) {
- var exprType = getLiteralTypeFromPropertyName(name);
- if (isTypeUsableAsPropertyName(exprType)) {
- var nameText = getPropertyNameFromType(exprType);
- var property = getPropertyOfType(parentType, nameText);
- if (property) {
- markPropertyAsReferenced(property, /*nodeForCheckWriteOnly*/ undefined, /*isThisAccess*/ false); // A destructuring is never a write-only reference.
- checkPropertyAccessibility(parent, !!parent.initializer && parent.initializer.kind === 102 /* SuperKeyword */, parentType, property);
- }
+ else if (name.kind === 197 /* ArrayBindingPattern */ || name.kind === 196 /* ObjectBindingPattern */) {
+ if (checkIfTypePredicateVariableIsDeclaredInBindingPattern(name, predicateVariableNode, predicateVariableName)) {
+ return true;
}
}
}
- // For a binding pattern, check contained binding elements
- if (ts.isBindingPattern(node.name)) {
- if (node.name.kind === 190 /* ArrayBindingPattern */ && languageVersion < 2 /* ES2015 */ && compilerOptions.downlevelIteration) {
- checkExternalEmitHelpers(node, 512 /* Read */);
+ }
+ function checkSignatureDeclaration(node) {
+ // Grammar checking
+ if (node.kind === 171 /* IndexSignature */) {
+ checkGrammarIndexSignature(node);
+ }
+ // TODO (yuisu): Remove this check in else-if when SyntaxKind.Construct is moved and ambient context is handled
+ else if (node.kind === 174 /* FunctionType */ || node.kind === 251 /* FunctionDeclaration */ || node.kind === 175 /* ConstructorType */ ||
+ node.kind === 169 /* CallSignature */ || node.kind === 166 /* Constructor */ ||
+ node.kind === 170 /* ConstructSignature */) {
+ checkGrammarFunctionLikeDeclaration(node);
+ }
+ var functionFlags = ts.getFunctionFlags(node);
+ if (!(functionFlags & 4 /* Invalid */)) {
+ // Async generators prior to ESNext require the __await and __asyncGenerator helpers
+ if ((functionFlags & 3 /* AsyncGenerator */) === 3 /* AsyncGenerator */ && languageVersion < 99 /* ESNext */) {
+ checkExternalEmitHelpers(node, 12288 /* AsyncGeneratorIncludes */);
+ }
+ // Async functions prior to ES2017 require the __awaiter helper
+ if ((functionFlags & 3 /* AsyncGenerator */) === 2 /* Async */ && languageVersion < 4 /* ES2017 */) {
+ checkExternalEmitHelpers(node, 64 /* Awaiter */);
+ }
+ // Generator functions, Async functions, and Async Generator functions prior to
+ // ES2015 require the __generator helper
+ if ((functionFlags & 3 /* AsyncGenerator */) !== 0 /* Normal */ && languageVersion < 2 /* ES2015 */) {
+ checkExternalEmitHelpers(node, 128 /* Generator */);
}
- ts.forEach(node.name.elements, checkSourceElement);
}
- // For a parameter declaration with an initializer, error and exit if the containing function doesn't have a body
- if (node.initializer && ts.getRootDeclaration(node).kind === 156 /* Parameter */ && ts.nodeIsMissing(ts.getContainingFunction(node).body)) {
- error(node, ts.Diagnostics.A_parameter_initializer_is_only_allowed_in_a_function_or_constructor_implementation);
- return;
+ checkTypeParameters(node.typeParameters);
+ ts.forEach(node.parameters, checkParameter);
+ // TODO(rbuckton): Should we start checking JSDoc types?
+ if (node.type) {
+ checkSourceElement(node.type);
}
- // For a binding pattern, validate the initializer and exit
- if (ts.isBindingPattern(node.name)) {
- var needCheckInitializer = node.initializer && node.parent.parent.kind !== 231 /* ForInStatement */;
- var needCheckWidenedType = node.name.elements.length === 0;
- if (needCheckInitializer || needCheckWidenedType) {
- // Don't validate for-in initializer as it is already an error
- var widenedType = getWidenedTypeForVariableLikeDeclaration(node);
- if (needCheckInitializer) {
- var initializerType = checkExpressionCached(node.initializer);
- if (strictNullChecks && needCheckWidenedType) {
- checkNonNullNonVoidType(initializerType, node);
+ if (produceDiagnostics) {
+ checkCollisionWithArgumentsInGeneratedCode(node);
+ var returnTypeNode = ts.getEffectiveReturnTypeNode(node);
+ if (noImplicitAny && !returnTypeNode) {
+ switch (node.kind) {
+ case 170 /* ConstructSignature */:
+ error(node, ts.Diagnostics.Construct_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type);
+ break;
+ case 169 /* CallSignature */:
+ error(node, ts.Diagnostics.Call_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type);
+ break;
+ }
+ }
+ if (returnTypeNode) {
+ var functionFlags_1 = ts.getFunctionFlags(node);
+ if ((functionFlags_1 & (4 /* Invalid */ | 1 /* Generator */)) === 1 /* Generator */) {
+ var returnType = getTypeFromTypeNode(returnTypeNode);
+ if (returnType === voidType) {
+ error(returnTypeNode, ts.Diagnostics.A_generator_cannot_have_a_void_type_annotation);
}
else {
- checkTypeAssignableToAndOptionallyElaborate(initializerType, getWidenedTypeForVariableLikeDeclaration(node), node, node.initializer);
+ // Naively, one could check that Generator is assignable to the return type annotation.
+ // However, that would not catch the error in the following case.
+ //
+ // interface BadGenerator extends Iterable, Iterator { }
+ // function* g(): BadGenerator { } // Iterable and Iterator have different types!
+ //
+ var generatorYieldType = getIterationTypeOfGeneratorFunctionReturnType(0 /* Yield */, returnType, (functionFlags_1 & 2 /* Async */) !== 0) || anyType;
+ var generatorReturnType = getIterationTypeOfGeneratorFunctionReturnType(1 /* Return */, returnType, (functionFlags_1 & 2 /* Async */) !== 0) || generatorYieldType;
+ var generatorNextType = getIterationTypeOfGeneratorFunctionReturnType(2 /* Next */, returnType, (functionFlags_1 & 2 /* Async */) !== 0) || unknownType;
+ var generatorInstantiation = createGeneratorReturnType(generatorYieldType, generatorReturnType, generatorNextType, !!(functionFlags_1 & 2 /* Async */));
+ checkTypeAssignableTo(generatorInstantiation, returnType, returnTypeNode);
}
}
- // check the binding pattern with empty elements
- if (needCheckWidenedType) {
- if (ts.isArrayBindingPattern(node.name)) {
- checkIteratedTypeOrElementType(65 /* Destructuring */, widenedType, undefinedType, node);
- }
- else if (strictNullChecks) {
- checkNonNullNonVoidType(widenedType, node);
- }
+ else if ((functionFlags_1 & 3 /* AsyncGenerator */) === 2 /* Async */) {
+ checkAsyncFunctionReturnType(node, returnTypeNode);
}
}
- return;
+ if (node.kind !== 171 /* IndexSignature */ && node.kind !== 308 /* JSDocFunctionType */) {
+ registerForUnusedIdentifiersCheck(node);
+ }
}
- var symbol = getSymbolOfNode(node);
- var type = convertAutoToAny(getTypeOfSymbol(symbol));
- if (node === symbol.valueDeclaration) {
- // Node is the primary declaration of the symbol, just validate the initializer
- // Don't validate for-in initializer as it is already an error
- var initializer = ts.getEffectiveInitializer(node);
- if (initializer) {
- var isJSObjectLiteralInitializer = ts.isInJSFile(node) &&
- ts.isObjectLiteralExpression(initializer) &&
- (initializer.properties.length === 0 || ts.isPrototypeAccess(node.name)) &&
- ts.hasEntries(symbol.exports);
- if (!isJSObjectLiteralInitializer && node.parent.parent.kind !== 231 /* ForInStatement */) {
- checkTypeAssignableToAndOptionallyElaborate(checkExpressionCached(initializer), type, node, initializer, /*headMessage*/ undefined);
+ }
+ function checkClassForDuplicateDeclarations(node) {
+ var instanceNames = new ts.Map();
+ var staticNames = new ts.Map();
+ // instance and static private identifiers share the same scope
+ var privateIdentifiers = new ts.Map();
+ for (var _i = 0, _a = node.members; _i < _a.length; _i++) {
+ var member = _a[_i];
+ if (member.kind === 166 /* Constructor */) {
+ for (var _b = 0, _c = member.parameters; _b < _c.length; _b++) {
+ var param = _c[_b];
+ if (ts.isParameterPropertyDeclaration(param, member) && !ts.isBindingPattern(param.name)) {
+ addName(instanceNames, param.name, param.name.escapedText, 3 /* GetOrSetAccessor */);
+ }
}
}
- if (symbol.declarations.length > 1) {
- if (ts.some(symbol.declarations, function (d) { return d !== node && ts.isVariableLike(d) && !areDeclarationFlagsIdentical(d, node); })) {
- error(node.name, ts.Diagnostics.All_declarations_of_0_must_have_identical_modifiers, ts.declarationNameToString(node.name));
+ else {
+ var isStatic = ts.hasSyntacticModifier(member, 32 /* Static */);
+ var name = member.name;
+ if (!name) {
+ return;
+ }
+ var names = ts.isPrivateIdentifier(name) ? privateIdentifiers :
+ isStatic ? staticNames :
+ instanceNames;
+ var memberName = name && ts.getPropertyNameForPropertyNameNode(name);
+ if (memberName) {
+ switch (member.kind) {
+ case 167 /* GetAccessor */:
+ addName(names, name, memberName, 1 /* GetAccessor */);
+ break;
+ case 168 /* SetAccessor */:
+ addName(names, name, memberName, 2 /* SetAccessor */);
+ break;
+ case 163 /* PropertyDeclaration */:
+ addName(names, name, memberName, 3 /* GetOrSetAccessor */);
+ break;
+ case 165 /* MethodDeclaration */:
+ addName(names, name, memberName, 8 /* Method */);
+ break;
+ }
}
}
}
- else {
- // Node is a secondary declaration, check that type is identical to primary declaration and check that
- // initializer is consistent with type associated with the node
- var declarationType = convertAutoToAny(getWidenedTypeForVariableLikeDeclaration(node));
- if (type !== errorType && declarationType !== errorType &&
- !isTypeIdenticalTo(type, declarationType) &&
- !(symbol.flags & 67108864 /* Assignment */)) {
- errorNextVariableOrPropertyDeclarationMustHaveSameType(symbol.valueDeclaration, type, node, declarationType);
- }
- if (node.initializer) {
- checkTypeAssignableToAndOptionallyElaborate(checkExpressionCached(node.initializer), declarationType, node, node.initializer, /*headMessage*/ undefined);
+ function addName(names, location, name, meaning) {
+ var prev = names.get(name);
+ if (prev) {
+ if (prev & 8 /* Method */) {
+ if (meaning !== 8 /* Method */) {
+ error(location, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(location));
+ }
+ }
+ else if (prev & meaning) {
+ error(location, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(location));
+ }
+ else {
+ names.set(name, prev | meaning);
+ }
}
- if (!areDeclarationFlagsIdentical(node, symbol.valueDeclaration)) {
- error(node.name, ts.Diagnostics.All_declarations_of_0_must_have_identical_modifiers, ts.declarationNameToString(node.name));
+ else {
+ names.set(name, meaning);
}
}
- if (node.kind !== 159 /* PropertyDeclaration */ && node.kind !== 158 /* PropertySignature */) {
- // We know we don't have a binding pattern or computed name here
- checkExportsOnMergedDeclarations(node);
- if (node.kind === 242 /* VariableDeclaration */ || node.kind === 191 /* BindingElement */) {
- checkVarDeclaredNamesNotShadowed(node);
- }
- checkCollisionWithRequireExportsInGeneratedCode(node, node.name);
- checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name);
- if (!compilerOptions.noEmit && languageVersion < 99 /* ESNext */ && needCollisionCheckForIdentifier(node, node.name, "WeakMap")) {
- potentialWeakMapCollisions.push(node);
+ }
+ /**
+ * Static members being set on a constructor function may conflict with built-in properties
+ * of Function. Esp. in ECMAScript 5 there are non-configurable and non-writable
+ * built-in properties. This check issues a transpile error when a class has a static
+ * member with the same name as a non-writable built-in property.
+ *
+ * @see http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.3
+ * @see http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5
+ * @see http://www.ecma-international.org/ecma-262/6.0/#sec-properties-of-the-function-constructor
+ * @see http://www.ecma-international.org/ecma-262/6.0/#sec-function-instances
+ */
+ function checkClassForStaticPropertyNameConflicts(node) {
+ for (var _i = 0, _a = node.members; _i < _a.length; _i++) {
+ var member = _a[_i];
+ var memberNameNode = member.name;
+ var isStatic = ts.hasSyntacticModifier(member, 32 /* Static */);
+ if (isStatic && memberNameNode) {
+ var memberName = ts.getPropertyNameForPropertyNameNode(memberNameNode);
+ switch (memberName) {
+ case "name":
+ case "length":
+ case "caller":
+ case "arguments":
+ case "prototype":
+ var message = ts.Diagnostics.Static_property_0_conflicts_with_built_in_property_Function_0_of_constructor_function_1;
+ var className = getNameOfSymbolAsWritten(getSymbolOfNode(node));
+ error(memberNameNode, message, memberName, className);
+ break;
+ }
}
}
}
- function errorNextVariableOrPropertyDeclarationMustHaveSameType(firstDeclaration, firstType, nextDeclaration, nextType) {
- var nextDeclarationName = ts.getNameOfDeclaration(nextDeclaration);
- var message = nextDeclaration.kind === 159 /* PropertyDeclaration */ || nextDeclaration.kind === 158 /* PropertySignature */
- ? ts.Diagnostics.Subsequent_property_declarations_must_have_the_same_type_Property_0_must_be_of_type_1_but_here_has_type_2
- : ts.Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2;
- var declName = ts.declarationNameToString(nextDeclarationName);
- var err = error(nextDeclarationName, message, declName, typeToString(firstType), typeToString(nextType));
- if (firstDeclaration) {
- ts.addRelatedInfo(err, ts.createDiagnosticForNode(firstDeclaration, ts.Diagnostics._0_was_also_declared_here, declName));
+ function checkObjectTypeForDuplicateDeclarations(node) {
+ var names = new ts.Map();
+ for (var _i = 0, _a = node.members; _i < _a.length; _i++) {
+ var member = _a[_i];
+ if (member.kind === 162 /* PropertySignature */) {
+ var memberName = void 0;
+ var name = member.name;
+ switch (name.kind) {
+ case 10 /* StringLiteral */:
+ case 8 /* NumericLiteral */:
+ memberName = name.text;
+ break;
+ case 78 /* Identifier */:
+ memberName = ts.idText(name);
+ break;
+ default:
+ continue;
+ }
+ if (names.get(memberName)) {
+ error(ts.getNameOfDeclaration(member.symbol.valueDeclaration), ts.Diagnostics.Duplicate_identifier_0, memberName);
+ error(member.name, ts.Diagnostics.Duplicate_identifier_0, memberName);
+ }
+ else {
+ names.set(memberName, true);
+ }
+ }
}
}
- function areDeclarationFlagsIdentical(left, right) {
- if ((left.kind === 156 /* Parameter */ && right.kind === 242 /* VariableDeclaration */) ||
- (left.kind === 242 /* VariableDeclaration */ && right.kind === 156 /* Parameter */)) {
- // Differences in optionality between parameters and variables are allowed.
- return true;
- }
- if (ts.hasQuestionToken(left) !== ts.hasQuestionToken(right)) {
- return false;
+ function checkTypeForDuplicateIndexSignatures(node) {
+ if (node.kind === 253 /* InterfaceDeclaration */) {
+ var nodeSymbol = getSymbolOfNode(node);
+ // in case of merging interface declaration it is possible that we'll enter this check procedure several times for every declaration
+ // to prevent this run check only for the first declaration of a given kind
+ if (nodeSymbol.declarations.length > 0 && nodeSymbol.declarations[0] !== node) {
+ return;
+ }
+ }
+ // TypeScript 1.0 spec (April 2014)
+ // 3.7.4: An object type can contain at most one string index signature and one numeric index signature.
+ // 8.5: A class declaration can have at most one string index member declaration and one numeric index member declaration
+ var indexSymbol = getIndexSymbol(getSymbolOfNode(node));
+ if (indexSymbol) {
+ var seenNumericIndexer = false;
+ var seenStringIndexer = false;
+ for (var _i = 0, _a = indexSymbol.declarations; _i < _a.length; _i++) {
+ var decl = _a[_i];
+ var declaration = decl;
+ if (declaration.parameters.length === 1 && declaration.parameters[0].type) {
+ switch (declaration.parameters[0].type.kind) {
+ case 147 /* StringKeyword */:
+ if (!seenStringIndexer) {
+ seenStringIndexer = true;
+ }
+ else {
+ error(declaration, ts.Diagnostics.Duplicate_string_index_signature);
+ }
+ break;
+ case 144 /* NumberKeyword */:
+ if (!seenNumericIndexer) {
+ seenNumericIndexer = true;
+ }
+ else {
+ error(declaration, ts.Diagnostics.Duplicate_number_index_signature);
+ }
+ break;
+ }
+ }
+ }
}
- var interestingFlags = 8 /* Private */ |
- 16 /* Protected */ |
- 256 /* Async */ |
- 128 /* Abstract */ |
- 64 /* Readonly */ |
- 32 /* Static */;
- return ts.getSelectedModifierFlags(left, interestingFlags) === ts.getSelectedModifierFlags(right, interestingFlags);
- }
- function checkVariableDeclaration(node) {
- checkGrammarVariableDeclaration(node);
- return checkVariableLikeDeclaration(node);
- }
- function checkBindingElement(node) {
- checkGrammarBindingElement(node);
- return checkVariableLikeDeclaration(node);
}
- function checkVariableStatement(node) {
+ function checkPropertyDeclaration(node) {
// Grammar checking
- if (!checkGrammarDecoratorsAndModifiers(node) && !checkGrammarVariableDeclarationList(node.declarationList))
- checkGrammarForDisallowedLetOrConstStatement(node);
- ts.forEach(node.declarationList.declarations, checkSourceElement);
+ if (!checkGrammarDecoratorsAndModifiers(node) && !checkGrammarProperty(node))
+ checkGrammarComputedPropertyName(node.name);
+ checkVariableLikeDeclaration(node);
+ // Private class fields transformation relies on WeakMaps.
+ if (ts.isPrivateIdentifier(node.name) && languageVersion < 99 /* ESNext */) {
+ for (var lexicalScope = ts.getEnclosingBlockScopeContainer(node); !!lexicalScope; lexicalScope = ts.getEnclosingBlockScopeContainer(lexicalScope)) {
+ getNodeLinks(lexicalScope).flags |= 67108864 /* ContainsClassWithPrivateIdentifiers */;
+ }
+ }
}
- function checkExpressionStatement(node) {
- // Grammar checking
- checkGrammarStatementInAmbientContext(node);
- checkExpression(node.expression);
+ function checkPropertySignature(node) {
+ if (ts.isPrivateIdentifier(node.name)) {
+ error(node, ts.Diagnostics.Private_identifiers_are_not_allowed_outside_class_bodies);
+ }
+ return checkPropertyDeclaration(node);
}
- function checkIfStatement(node) {
+ function checkMethodDeclaration(node) {
// Grammar checking
- checkGrammarStatementInAmbientContext(node);
- var type = checkTruthinessExpression(node.expression);
- checkTestingKnownTruthyCallableType(node.expression, node.thenStatement, type);
- checkSourceElement(node.thenStatement);
- if (node.thenStatement.kind === 224 /* EmptyStatement */) {
- error(node.thenStatement, ts.Diagnostics.The_body_of_an_if_statement_cannot_be_the_empty_statement);
+ if (!checkGrammarMethod(node))
+ checkGrammarComputedPropertyName(node.name);
+ if (ts.isPrivateIdentifier(node.name)) {
+ error(node, ts.Diagnostics.A_method_cannot_be_named_with_a_private_identifier);
}
- checkSourceElement(node.elseStatement);
- }
- function checkTestingKnownTruthyCallableType(condExpr, body, type) {
- if (!strictNullChecks) {
- return;
+ // Grammar checking for modifiers is done inside the function checkGrammarFunctionLikeDeclaration
+ checkFunctionOrMethodDeclaration(node);
+ // Abstract methods cannot have an implementation.
+ // Extra checks are to avoid reporting multiple errors relating to the "abstractness" of the node.
+ if (ts.hasSyntacticModifier(node, 128 /* Abstract */) && node.kind === 165 /* MethodDeclaration */ && node.body) {
+ error(node, ts.Diagnostics.Method_0_cannot_have_an_implementation_because_it_is_marked_abstract, ts.declarationNameToString(node.name));
}
- var testedNode = ts.isIdentifier(condExpr)
- ? condExpr
- : ts.isPropertyAccessExpression(condExpr)
- ? condExpr.name
- : undefined;
- if (!testedNode) {
- return;
+ }
+ function checkConstructorDeclaration(node) {
+ // Grammar check on signature of constructor and modifier of the constructor is done in checkSignatureDeclaration function.
+ checkSignatureDeclaration(node);
+ // Grammar check for checking only related to constructorDeclaration
+ if (!checkGrammarConstructorTypeParameters(node))
+ checkGrammarConstructorTypeAnnotation(node);
+ checkSourceElement(node.body);
+ var symbol = getSymbolOfNode(node);
+ var firstDeclaration = ts.getDeclarationOfKind(symbol, node.kind);
+ // Only type check the symbol once
+ if (node === firstDeclaration) {
+ checkFunctionOrConstructorSymbol(symbol);
}
- var possiblyFalsy = getFalsyFlags(type);
- if (possiblyFalsy) {
+ // exit early in the case of signature - super checks are not relevant to them
+ if (ts.nodeIsMissing(node.body)) {
return;
}
- // While it technically should be invalid for any known-truthy value
- // to be tested, we de-scope to functions unrefenced in the block as a
- // heuristic to identify the most common bugs. There are too many
- // false positives for values sourced from type definitions without
- // strictNullChecks otherwise.
- var callSignatures = getSignaturesOfType(type, 0 /* Call */);
- if (callSignatures.length === 0) {
+ if (!produceDiagnostics) {
return;
}
- var testedFunctionSymbol = getSymbolAtLocation(testedNode);
- if (!testedFunctionSymbol) {
- return;
+ function isInstancePropertyWithInitializerOrPrivateIdentifierProperty(n) {
+ if (ts.isPrivateIdentifierPropertyDeclaration(n)) {
+ return true;
+ }
+ return n.kind === 163 /* PropertyDeclaration */ &&
+ !ts.hasSyntacticModifier(n, 32 /* Static */) &&
+ !!n.initializer;
}
- var functionIsUsedInBody = ts.forEachChild(body, function check(childNode) {
- if (ts.isIdentifier(childNode)) {
- var childSymbol = getSymbolAtLocation(childNode);
- if (childSymbol && childSymbol === testedFunctionSymbol) {
- // If the test was a simple identifier, the above check is sufficient
- if (ts.isIdentifier(condExpr)) {
- return true;
- }
- // Otherwise we need to ensure the symbol is called on the same target
- var testedExpression = testedNode.parent;
- var childExpression = childNode.parent;
- while (testedExpression && childExpression) {
- if (ts.isIdentifier(testedExpression) && ts.isIdentifier(childExpression) ||
- testedExpression.kind === 104 /* ThisKeyword */ && childExpression.kind === 104 /* ThisKeyword */) {
- return getSymbolAtLocation(testedExpression) === getSymbolAtLocation(childExpression);
- }
- if (ts.isPropertyAccessExpression(testedExpression) && ts.isPropertyAccessExpression(childExpression)) {
- if (getSymbolAtLocation(testedExpression.name) !== getSymbolAtLocation(childExpression.name)) {
- return false;
- }
- childExpression = childExpression.expression;
- testedExpression = testedExpression.expression;
+ // TS 1.0 spec (April 2014): 8.3.2
+ // Constructors of classes with no extends clause may not contain super calls, whereas
+ // constructors of derived classes must contain at least one super call somewhere in their function body.
+ var containingClassDecl = node.parent;
+ if (ts.getClassExtendsHeritageElement(containingClassDecl)) {
+ captureLexicalThis(node.parent, containingClassDecl);
+ var classExtendsNull = classDeclarationExtendsNull(containingClassDecl);
+ var superCall = findFirstSuperCall(node.body);
+ if (superCall) {
+ if (classExtendsNull) {
+ error(superCall, ts.Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null);
+ }
+ // The first statement in the body of a constructor (excluding prologue directives) must be a super call
+ // if both of the following are true:
+ // - The containing class is a derived class.
+ // - The constructor declares parameter properties
+ // or the containing class declares instance member variables with initializers.
+ var superCallShouldBeFirst = (compilerOptions.target !== 99 /* ESNext */ || !compilerOptions.useDefineForClassFields) &&
+ (ts.some(node.parent.members, isInstancePropertyWithInitializerOrPrivateIdentifierProperty) ||
+ ts.some(node.parameters, function (p) { return ts.hasSyntacticModifier(p, 92 /* ParameterPropertyModifier */); }));
+ // Skip past any prologue directives to find the first statement
+ // to ensure that it was a super call.
+ if (superCallShouldBeFirst) {
+ var statements = node.body.statements;
+ var superCallStatement = void 0;
+ for (var _i = 0, statements_4 = statements; _i < statements_4.length; _i++) {
+ var statement = statements_4[_i];
+ if (statement.kind === 233 /* ExpressionStatement */ && ts.isSuperCall(statement.expression)) {
+ superCallStatement = statement;
+ break;
}
- else {
- return false;
+ if (!ts.isPrologueDirective(statement)) {
+ break;
}
}
+ if (!superCallStatement) {
+ error(node, ts.Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_parameter_properties_or_private_identifiers);
+ }
}
}
- return ts.forEachChild(childNode, check);
- });
- if (!functionIsUsedInBody) {
- error(condExpr, ts.Diagnostics.This_condition_will_always_return_true_since_the_function_is_always_defined_Did_you_mean_to_call_it_instead);
- }
- }
- function checkDoStatement(node) {
- // Grammar checking
- checkGrammarStatementInAmbientContext(node);
- checkSourceElement(node.statement);
- checkTruthinessExpression(node.expression);
- }
- function checkWhileStatement(node) {
- // Grammar checking
- checkGrammarStatementInAmbientContext(node);
- checkTruthinessExpression(node.expression);
- checkSourceElement(node.statement);
- }
- function checkTruthinessOfType(type, node) {
- if (type.flags & 16384 /* Void */) {
- error(node, ts.Diagnostics.An_expression_of_type_void_cannot_be_tested_for_truthiness);
+ else if (!classExtendsNull) {
+ error(node, ts.Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call);
+ }
}
- return type;
- }
- function checkTruthinessExpression(node, checkMode) {
- return checkTruthinessOfType(checkExpression(node, checkMode), node);
}
- function checkForStatement(node) {
- // Grammar checking
- if (!checkGrammarStatementInAmbientContext(node)) {
- if (node.initializer && node.initializer.kind === 243 /* VariableDeclarationList */) {
- checkGrammarVariableDeclarationList(node.initializer);
+ function checkAccessorDeclaration(node) {
+ if (produceDiagnostics) {
+ // Grammar checking accessors
+ if (!checkGrammarFunctionLikeDeclaration(node) && !checkGrammarAccessor(node))
+ checkGrammarComputedPropertyName(node.name);
+ checkDecorators(node);
+ checkSignatureDeclaration(node);
+ if (node.kind === 167 /* GetAccessor */) {
+ if (!(node.flags & 8388608 /* Ambient */) && ts.nodeIsPresent(node.body) && (node.flags & 256 /* HasImplicitReturn */)) {
+ if (!(node.flags & 512 /* HasExplicitReturn */)) {
+ error(node.name, ts.Diagnostics.A_get_accessor_must_return_a_value);
+ }
+ }
}
- }
- if (node.initializer) {
- if (node.initializer.kind === 243 /* VariableDeclarationList */) {
- ts.forEach(node.initializer.declarations, checkVariableDeclaration);
+ // Do not use hasDynamicName here, because that returns false for well known symbols.
+ // We want to perform checkComputedPropertyName for all computed properties, including
+ // well known symbols.
+ if (node.name.kind === 158 /* ComputedPropertyName */) {
+ checkComputedPropertyName(node.name);
}
- else {
- checkExpression(node.initializer);
+ if (ts.isPrivateIdentifier(node.name)) {
+ error(node.name, ts.Diagnostics.An_accessor_cannot_be_named_with_a_private_identifier);
+ }
+ if (!hasNonBindableDynamicName(node)) {
+ // TypeScript 1.0 spec (April 2014): 8.4.3
+ // Accessors for the same member name must specify the same accessibility.
+ var otherKind = node.kind === 167 /* GetAccessor */ ? 168 /* SetAccessor */ : 167 /* GetAccessor */;
+ var otherAccessor = ts.getDeclarationOfKind(getSymbolOfNode(node), otherKind);
+ if (otherAccessor) {
+ var nodeFlags = ts.getEffectiveModifierFlags(node);
+ var otherFlags = ts.getEffectiveModifierFlags(otherAccessor);
+ if ((nodeFlags & 28 /* AccessibilityModifier */) !== (otherFlags & 28 /* AccessibilityModifier */)) {
+ error(node.name, ts.Diagnostics.Getter_and_setter_accessors_do_not_agree_in_visibility);
+ }
+ if ((nodeFlags & 128 /* Abstract */) !== (otherFlags & 128 /* Abstract */)) {
+ error(node.name, ts.Diagnostics.Accessors_must_both_be_abstract_or_non_abstract);
+ }
+ // TypeScript 1.0 spec (April 2014): 4.5
+ // If both accessors include type annotations, the specified types must be identical.
+ checkAccessorDeclarationTypesIdentical(node, otherAccessor, getAnnotatedAccessorType, ts.Diagnostics.get_and_set_accessor_must_have_the_same_type);
+ checkAccessorDeclarationTypesIdentical(node, otherAccessor, getThisTypeOfDeclaration, ts.Diagnostics.get_and_set_accessor_must_have_the_same_this_type);
+ }
+ }
+ var returnType = getTypeOfAccessors(getSymbolOfNode(node));
+ if (node.kind === 167 /* GetAccessor */) {
+ checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnType);
}
}
- if (node.condition)
- checkTruthinessExpression(node.condition);
- if (node.incrementor)
- checkExpression(node.incrementor);
- checkSourceElement(node.statement);
- if (node.locals) {
- registerForUnusedIdentifiersCheck(node);
+ checkSourceElement(node.body);
+ }
+ function checkAccessorDeclarationTypesIdentical(first, second, getAnnotatedType, message) {
+ var firstType = getAnnotatedType(first);
+ var secondType = getAnnotatedType(second);
+ if (firstType && secondType && !isTypeIdenticalTo(firstType, secondType)) {
+ error(first, message);
}
}
- function checkForOfStatement(node) {
- checkGrammarForInOrForOfStatement(node);
- if (node.awaitModifier) {
- var functionFlags = ts.getFunctionFlags(ts.getContainingFunction(node));
- if ((functionFlags & (4 /* Invalid */ | 2 /* Async */)) === 2 /* Async */ && languageVersion < 99 /* ESNext */) {
- // for..await..of in an async function or async generator function prior to ESNext requires the __asyncValues helper
- checkExternalEmitHelpers(node, 32768 /* ForAwaitOfIncludes */);
+ function checkMissingDeclaration(node) {
+ checkDecorators(node);
+ }
+ function getEffectiveTypeArguments(node, typeParameters) {
+ return fillMissingTypeArguments(ts.map(node.typeArguments, getTypeFromTypeNode), typeParameters, getMinTypeArgumentCount(typeParameters), ts.isInJSFile(node));
+ }
+ function checkTypeArgumentConstraints(node, typeParameters) {
+ var typeArguments;
+ var mapper;
+ var result = true;
+ for (var i = 0; i < typeParameters.length; i++) {
+ var constraint = getConstraintOfTypeParameter(typeParameters[i]);
+ if (constraint) {
+ if (!typeArguments) {
+ typeArguments = getEffectiveTypeArguments(node, typeParameters);
+ mapper = createTypeMapper(typeParameters, typeArguments);
+ }
+ result = result && checkTypeAssignableTo(typeArguments[i], instantiateType(constraint, mapper), node.typeArguments[i], ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1);
}
}
- else if (compilerOptions.downlevelIteration && languageVersion < 2 /* ES2015 */) {
- // for..of prior to ES2015 requires the __values helper when downlevelIteration is enabled
- checkExternalEmitHelpers(node, 256 /* ForOfIncludes */);
+ return result;
+ }
+ function getTypeParametersForTypeReference(node) {
+ var type = getTypeFromTypeReference(node);
+ if (type !== errorType) {
+ var symbol = getNodeLinks(node).resolvedSymbol;
+ if (symbol) {
+ return symbol.flags & 524288 /* TypeAlias */ && getSymbolLinks(symbol).typeParameters ||
+ (ts.getObjectFlags(type) & 4 /* Reference */ ? type.target.localTypeParameters : undefined);
+ }
}
- // Check the LHS and RHS
- // If the LHS is a declaration, just check it as a variable declaration, which will in turn check the RHS
- // via checkRightHandSideOfForOf.
- // If the LHS is an expression, check the LHS, as a destructuring assignment or as a reference.
- // Then check that the RHS is assignable to it.
- if (node.initializer.kind === 243 /* VariableDeclarationList */) {
- checkForInOrForOfVariableDeclaration(node);
+ return undefined;
+ }
+ function checkTypeReferenceNode(node) {
+ checkGrammarTypeArguments(node, node.typeArguments);
+ if (node.kind === 173 /* TypeReference */ && node.typeName.jsdocDotPos !== undefined && !ts.isInJSFile(node) && !ts.isInJSDoc(node)) {
+ grammarErrorAtPos(node, node.typeName.jsdocDotPos, 1, ts.Diagnostics.JSDoc_types_can_only_be_used_inside_documentation_comments);
}
- else {
- var varExpr = node.initializer;
- var iteratedType = checkRightHandSideOfForOf(node);
- // There may be a destructuring assignment on the left side
- if (varExpr.kind === 192 /* ArrayLiteralExpression */ || varExpr.kind === 193 /* ObjectLiteralExpression */) {
- // iteratedType may be undefined. In this case, we still want to check the structure of
- // varExpr, in particular making sure it's a valid LeftHandSideExpression. But we'd like
- // to short circuit the type relation checking as much as possible, so we pass the unknownType.
- checkDestructuringAssignment(varExpr, iteratedType || errorType);
+ ts.forEach(node.typeArguments, checkSourceElement);
+ var type = getTypeFromTypeReference(node);
+ if (type !== errorType) {
+ if (node.typeArguments && produceDiagnostics) {
+ var typeParameters = getTypeParametersForTypeReference(node);
+ if (typeParameters) {
+ checkTypeArgumentConstraints(node, typeParameters);
+ }
}
- else {
- var leftType = checkExpression(varExpr);
- checkReferenceExpression(varExpr, ts.Diagnostics.The_left_hand_side_of_a_for_of_statement_must_be_a_variable_or_a_property_access, ts.Diagnostics.The_left_hand_side_of_a_for_of_statement_may_not_be_an_optional_property_access);
- // iteratedType will be undefined if the rightType was missing properties/signatures
- // required to get its iteratedType (like [Symbol.iterator] or next). This may be
- // because we accessed properties from anyType, or it may have led to an error inside
- // getElementTypeOfIterable.
- if (iteratedType) {
- checkTypeAssignableToAndOptionallyElaborate(iteratedType, leftType, varExpr, node.expression);
+ var symbol = getNodeLinks(node).resolvedSymbol;
+ if (symbol) {
+ if (ts.some(symbol.declarations, function (d) { return isTypeDeclaration(d) && !!(d.flags & 134217728 /* Deprecated */); })) {
+ errorOrSuggestion(/* isError */ false, getDeprecatedSuggestionNode(node), ts.Diagnostics._0_is_deprecated, symbol.escapedName);
+ }
+ if (type.flags & 32 /* Enum */ && symbol.flags & 8 /* EnumMember */) {
+ error(node, ts.Diagnostics.Enum_type_0_has_members_with_initializers_that_are_not_literals, typeToString(type));
}
}
}
- checkSourceElement(node.statement);
- if (node.locals) {
- registerForUnusedIdentifiersCheck(node);
+ }
+ function getTypeArgumentConstraint(node) {
+ var typeReferenceNode = ts.tryCast(node.parent, ts.isTypeReferenceType);
+ if (!typeReferenceNode)
+ return undefined;
+ var typeParameters = getTypeParametersForTypeReference(typeReferenceNode); // TODO: GH#18217
+ var constraint = getConstraintOfTypeParameter(typeParameters[typeReferenceNode.typeArguments.indexOf(node)]);
+ return constraint && instantiateType(constraint, createTypeMapper(typeParameters, getEffectiveTypeArguments(typeReferenceNode, typeParameters)));
+ }
+ function checkTypeQuery(node) {
+ getTypeFromTypeQueryNode(node);
+ }
+ function checkTypeLiteral(node) {
+ ts.forEach(node.members, checkSourceElement);
+ if (produceDiagnostics) {
+ var type = getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node);
+ checkIndexConstraints(type);
+ checkTypeForDuplicateIndexSignatures(node);
+ checkObjectTypeForDuplicateDeclarations(node);
}
}
- function checkForInStatement(node) {
- // Grammar checking
- checkGrammarForInOrForOfStatement(node);
- var rightType = getNonNullableTypeIfNeeded(checkExpression(node.expression));
- // TypeScript 1.0 spec (April 2014): 5.4
- // In a 'for-in' statement of the form
- // for (let VarDecl in Expr) Statement
- // VarDecl must be a variable declaration without a type annotation that declares a variable of type Any,
- // and Expr must be an expression of type Any, an object type, or a type parameter type.
- if (node.initializer.kind === 243 /* VariableDeclarationList */) {
- var variable = node.initializer.declarations[0];
- if (variable && ts.isBindingPattern(variable.name)) {
- error(variable.name, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_destructuring_pattern);
+ function checkArrayType(node) {
+ checkSourceElement(node.elementType);
+ }
+ function checkTupleType(node) {
+ var elementTypes = node.elements;
+ var seenOptionalElement = false;
+ var seenRestElement = false;
+ var hasNamedElement = ts.some(elementTypes, ts.isNamedTupleMember);
+ for (var i = 0; i < elementTypes.length; i++) {
+ var e = elementTypes[i];
+ if (e.kind !== 192 /* NamedTupleMember */ && hasNamedElement) {
+ grammarErrorOnNode(e, ts.Diagnostics.Tuple_members_must_all_have_names_or_all_not_have_names);
+ break;
}
- checkForInOrForOfVariableDeclaration(node);
- }
- else {
- // In a 'for-in' statement of the form
- // for (Var in Expr) Statement
- // Var must be an expression classified as a reference of type Any or the String primitive type,
- // and Expr must be an expression of type Any, an object type, or a type parameter type.
- var varExpr = node.initializer;
- var leftType = checkExpression(varExpr);
- if (varExpr.kind === 192 /* ArrayLiteralExpression */ || varExpr.kind === 193 /* ObjectLiteralExpression */) {
- error(varExpr, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_destructuring_pattern);
+ var flags = getTupleElementFlags(e);
+ if (flags & 8 /* Variadic */) {
+ var type = getTypeFromTypeNode(e.type);
+ if (!isArrayLikeType(type)) {
+ error(e, ts.Diagnostics.A_rest_element_type_must_be_an_array_type);
+ break;
+ }
+ if (isArrayType(type) || isTupleType(type) && type.target.combinedFlags & 4 /* Rest */) {
+ seenRestElement = true;
+ }
}
- else if (!isTypeAssignableTo(getIndexTypeOrString(rightType), leftType)) {
- error(varExpr, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_must_be_of_type_string_or_any);
+ else if (flags & 4 /* Rest */) {
+ seenRestElement = true;
}
- else {
- // run check only former check succeeded to avoid cascading errors
- checkReferenceExpression(varExpr, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_must_be_a_variable_or_a_property_access, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_may_not_be_an_optional_property_access);
+ else if (flags & 2 /* Optional */) {
+ seenOptionalElement = true;
+ }
+ else if (seenOptionalElement) {
+ grammarErrorOnNode(e, ts.Diagnostics.A_required_element_cannot_follow_an_optional_element);
+ break;
+ }
+ if (seenRestElement && i !== elementTypes.length - 1) {
+ grammarErrorOnNode(e, ts.Diagnostics.A_rest_element_must_be_last_in_a_tuple_type);
+ break;
}
}
- // unknownType is returned i.e. if node.expression is identifier whose name cannot be resolved
- // in this case error about missing name is already reported - do not report extra one
- if (rightType === neverType || !isTypeAssignableToKind(rightType, 67108864 /* NonPrimitive */ | 58982400 /* InstantiableNonPrimitive */)) {
- error(node.expression, ts.Diagnostics.The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter_but_here_has_type_0, typeToString(rightType));
- }
- checkSourceElement(node.statement);
- if (node.locals) {
- registerForUnusedIdentifiersCheck(node);
- }
- }
- function checkForInOrForOfVariableDeclaration(iterationStatement) {
- var variableDeclarationList = iterationStatement.initializer;
- // checkGrammarForInOrForOfStatement will check that there is exactly one declaration.
- if (variableDeclarationList.declarations.length >= 1) {
- var decl = variableDeclarationList.declarations[0];
- checkVariableDeclaration(decl);
- }
- }
- function checkRightHandSideOfForOf(statement) {
- var use = statement.awaitModifier ? 15 /* ForAwaitOf */ : 13 /* ForOf */;
- return checkIteratedTypeOrElementType(use, checkNonNullExpression(statement.expression), undefinedType, statement.expression);
+ ts.forEach(node.elements, checkSourceElement);
+ getTypeFromTypeNode(node);
}
- function checkIteratedTypeOrElementType(use, inputType, sentType, errorNode) {
- if (isTypeAny(inputType)) {
- return inputType;
- }
- return getIteratedTypeOrElementType(use, inputType, sentType, errorNode, /*checkAssignability*/ true) || anyType;
+ function checkUnionOrIntersectionType(node) {
+ ts.forEach(node.types, checkSourceElement);
+ getTypeFromTypeNode(node);
}
- /**
- * When consuming an iterable type in a for..of, spread, or iterator destructuring assignment
- * we want to get the iterated type of an iterable for ES2015 or later, or the iterated type
- * of a iterable (if defined globally) or element type of an array like for ES2015 or earlier.
- */
- function getIteratedTypeOrElementType(use, inputType, sentType, errorNode, checkAssignability) {
- var allowAsyncIterables = (use & 2 /* AllowsAsyncIterablesFlag */) !== 0;
- if (inputType === neverType) {
- reportTypeNotIterableError(errorNode, inputType, allowAsyncIterables); // TODO: GH#18217
- return undefined;
+ function checkIndexedAccessIndexType(type, accessNode) {
+ if (!(type.flags & 8388608 /* IndexedAccess */)) {
+ return type;
}
- var uplevelIteration = languageVersion >= 2 /* ES2015 */;
- var downlevelIteration = !uplevelIteration && compilerOptions.downlevelIteration;
- // Get the iterated type of an `Iterable` or `IterableIterator` only in ES2015
- // or higher, when inside of an async generator or for-await-if, or when
- // downlevelIteration is requested.
- if (uplevelIteration || downlevelIteration || allowAsyncIterables) {
- // We only report errors for an invalid iterable type in ES2015 or higher.
- var iterationTypes = getIterationTypesOfIterable(inputType, use, uplevelIteration ? errorNode : undefined);
- if (checkAssignability) {
- if (iterationTypes) {
- var diagnostic = use & 8 /* ForOfFlag */ ? ts.Diagnostics.Cannot_iterate_value_because_the_next_method_of_its_iterator_expects_type_1_but_for_of_will_always_send_0 :
- use & 32 /* SpreadFlag */ ? ts.Diagnostics.Cannot_iterate_value_because_the_next_method_of_its_iterator_expects_type_1_but_array_spread_will_always_send_0 :
- use & 64 /* DestructuringFlag */ ? ts.Diagnostics.Cannot_iterate_value_because_the_next_method_of_its_iterator_expects_type_1_but_array_destructuring_will_always_send_0 :
- use & 16 /* YieldStarFlag */ ? ts.Diagnostics.Cannot_delegate_iteration_to_value_because_the_next_method_of_its_iterator_expects_type_1_but_the_containing_generator_will_always_send_0 :
- undefined;
- if (diagnostic) {
- checkTypeAssignableTo(sentType, iterationTypes.nextType, errorNode, diagnostic);
- }
- }
- }
- if (iterationTypes || uplevelIteration) {
- return iterationTypes && iterationTypes.yieldType;
+ // Check if the index type is assignable to 'keyof T' for the object type.
+ var objectType = type.objectType;
+ var indexType = type.indexType;
+ if (isTypeAssignableTo(indexType, getIndexType(objectType, /*stringsOnly*/ false))) {
+ if (accessNode.kind === 202 /* ElementAccessExpression */ && ts.isAssignmentTarget(accessNode) &&
+ ts.getObjectFlags(objectType) & 32 /* Mapped */ && getMappedTypeModifiers(objectType) & 1 /* IncludeReadonly */) {
+ error(accessNode, ts.Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(objectType));
}
+ return type;
}
- var arrayType = inputType;
- var reportedError = false;
- var hasStringConstituent = false;
- // If strings are permitted, remove any string-like constituents from the array type.
- // This allows us to find other non-string element types from an array unioned with
- // a string.
- if (use & 4 /* AllowsStringInputFlag */) {
- if (arrayType.flags & 1048576 /* Union */) {
- // After we remove all types that are StringLike, we will know if there was a string constituent
- // based on whether the result of filter is a new array.
- var arrayTypes = inputType.types;
- var filteredTypes = ts.filter(arrayTypes, function (t) { return !(t.flags & 132 /* StringLike */); });
- if (filteredTypes !== arrayTypes) {
- arrayType = getUnionType(filteredTypes, 2 /* Subtype */);
- }
- }
- else if (arrayType.flags & 132 /* StringLike */) {
- arrayType = neverType;
- }
- hasStringConstituent = arrayType !== inputType;
- if (hasStringConstituent) {
- if (languageVersion < 1 /* ES5 */) {
- if (errorNode) {
- error(errorNode, ts.Diagnostics.Using_a_string_in_a_for_of_statement_is_only_supported_in_ECMAScript_5_and_higher);
- reportedError = true;
- }
- }
- // Now that we've removed all the StringLike types, if no constituents remain, then the entire
- // arrayOrStringType was a string.
- if (arrayType.flags & 131072 /* Never */) {
- return stringType;
+ // Check if we're indexing with a numeric type and if either object or index types
+ // is a generic type with a constraint that has a numeric index signature.
+ var apparentObjectType = getApparentType(objectType);
+ if (getIndexInfoOfType(apparentObjectType, 1 /* Number */) && isTypeAssignableToKind(indexType, 296 /* NumberLike */)) {
+ return type;
+ }
+ if (isGenericObjectType(objectType)) {
+ var propertyName_1 = getPropertyNameFromIndex(indexType, accessNode);
+ if (propertyName_1) {
+ var propertySymbol = forEachType(apparentObjectType, function (t) { return getPropertyOfType(t, propertyName_1); });
+ if (propertySymbol && ts.getDeclarationModifierFlagsFromSymbol(propertySymbol) & 24 /* NonPublicAccessibilityModifier */) {
+ error(accessNode, ts.Diagnostics.Private_or_protected_member_0_cannot_be_accessed_on_a_type_parameter, ts.unescapeLeadingUnderscores(propertyName_1));
+ return errorType;
}
}
}
- if (!isArrayLikeType(arrayType)) {
- if (errorNode && !reportedError) {
- // Which error we report depends on whether we allow strings or if there was a
- // string constituent. For example, if the input type is number | string, we
- // want to say that number is not an array type. But if the input was just
- // number and string input is allowed, we want to say that number is not an
- // array type or a string type.
- var yieldType = getIterationTypeOfIterable(use, 0 /* Yield */, inputType, /*errorNode*/ undefined);
- var _a = !(use & 4 /* AllowsStringInputFlag */) || hasStringConstituent
- ? downlevelIteration
- ? [ts.Diagnostics.Type_0_is_not_an_array_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator, true]
- : yieldType
- ? [ts.Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators, false]
- : [ts.Diagnostics.Type_0_is_not_an_array_type, true]
- : downlevelIteration
- ? [ts.Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator, true]
- : yieldType
- ? [ts.Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators, false]
- : [ts.Diagnostics.Type_0_is_not_an_array_type_or_a_string_type, true], defaultDiagnostic = _a[0], maybeMissingAwait = _a[1];
- errorAndMaybeSuggestAwait(errorNode, maybeMissingAwait && !!getAwaitedTypeOfPromise(arrayType), defaultDiagnostic, typeToString(arrayType));
- }
- return hasStringConstituent ? stringType : undefined;
+ error(accessNode, ts.Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(indexType), typeToString(objectType));
+ return errorType;
+ }
+ function checkIndexedAccessType(node) {
+ checkSourceElement(node.objectType);
+ checkSourceElement(node.indexType);
+ checkIndexedAccessIndexType(getTypeFromIndexedAccessTypeNode(node), node);
+ }
+ function checkMappedType(node) {
+ checkSourceElement(node.typeParameter);
+ checkSourceElement(node.nameType);
+ checkSourceElement(node.type);
+ if (!node.type) {
+ reportImplicitAny(node, anyType);
}
- var arrayElementType = getIndexTypeOfType(arrayType, 1 /* Number */);
- if (hasStringConstituent && arrayElementType) {
- // This is just an optimization for the case where arrayOrStringType is string | string[]
- if (arrayElementType.flags & 132 /* StringLike */) {
- return stringType;
- }
- return getUnionType([arrayElementType, stringType], 2 /* Subtype */);
+ var type = getTypeFromMappedTypeNode(node);
+ var nameType = getNameTypeFromMappedType(type);
+ if (nameType) {
+ checkTypeAssignableTo(nameType, keyofConstraintType, node.nameType);
+ }
+ else {
+ var constraintType = getConstraintTypeFromMappedType(type);
+ checkTypeAssignableTo(constraintType, keyofConstraintType, ts.getEffectiveConstraintOfTypeParameter(node.typeParameter));
}
- return arrayElementType;
}
- /**
- * Gets the requested "iteration type" from an `Iterable`-like or `AsyncIterable`-like type.
- */
- function getIterationTypeOfIterable(use, typeKind, inputType, errorNode) {
- if (isTypeAny(inputType)) {
- return undefined;
+ function checkThisType(node) {
+ getTypeFromThisTypeNode(node);
+ }
+ function checkTypeOperator(node) {
+ checkGrammarTypeOperatorNode(node);
+ checkSourceElement(node.type);
+ }
+ function checkConditionalType(node) {
+ ts.forEachChild(node, checkSourceElement);
+ }
+ function checkInferType(node) {
+ if (!ts.findAncestor(node, function (n) { return n.parent && n.parent.kind === 184 /* ConditionalType */ && n.parent.extendsType === n; })) {
+ grammarErrorOnNode(node, ts.Diagnostics.infer_declarations_are_only_permitted_in_the_extends_clause_of_a_conditional_type);
}
- var iterationTypes = getIterationTypesOfIterable(inputType, use, errorNode);
- return iterationTypes && iterationTypes[getIterationTypesKeyFromIterationTypeKind(typeKind)];
+ checkSourceElement(node.typeParameter);
+ registerForUnusedIdentifiersCheck(node);
}
- function createIterationTypes(yieldType, returnType, nextType) {
- // `yieldType` and `returnType` are defaulted to `neverType` they each will be combined
- // via `getUnionType` when merging iteration types. `nextType` is defined as `unknownType`
- // as it is combined via `getIntersectionType` when merging iteration types.
- if (yieldType === void 0) { yieldType = neverType; }
- if (returnType === void 0) { returnType = neverType; }
- if (nextType === void 0) { nextType = unknownType; }
- // Use the cache only for intrinsic types to keep it small as they are likely to be
- // more frequently created (i.e. `Iterator`). Iteration types
- // are also cached on the type they are requested for, so we shouldn't need to maintain
- // the cache for less-frequently used types.
- if (yieldType.flags & 67359327 /* Intrinsic */ &&
- returnType.flags & (1 /* Any */ | 131072 /* Never */ | 2 /* Unknown */ | 16384 /* Void */ | 32768 /* Undefined */) &&
- nextType.flags & (1 /* Any */ | 131072 /* Never */ | 2 /* Unknown */ | 16384 /* Void */ | 32768 /* Undefined */)) {
- var id = getTypeListId([yieldType, returnType, nextType]);
- var iterationTypes = iterationTypesCache.get(id);
- if (!iterationTypes) {
- iterationTypes = { yieldType: yieldType, returnType: returnType, nextType: nextType };
- iterationTypesCache.set(id, iterationTypes);
- }
- return iterationTypes;
+ function checkTemplateLiteralType(node) {
+ for (var _i = 0, _a = node.templateSpans; _i < _a.length; _i++) {
+ var span = _a[_i];
+ checkSourceElement(span.type);
+ var type = getTypeFromTypeNode(span.type);
+ checkTypeAssignableTo(type, templateConstraintType, span.type);
}
- return { yieldType: yieldType, returnType: returnType, nextType: nextType };
+ getTypeFromTypeNode(node);
}
- /**
- * Combines multiple `IterationTypes` records.
- *
- * If `array` is empty or all elements are missing or are references to `noIterationTypes`,
- * then `noIterationTypes` is returned. Otherwise, an `IterationTypes` record is returned
- * for the combined iteration types.
- */
- function combineIterationTypes(array) {
- var yieldTypes;
- var returnTypes;
- var nextTypes;
- for (var _i = 0, array_10 = array; _i < array_10.length; _i++) {
- var iterationTypes = array_10[_i];
- if (iterationTypes === undefined || iterationTypes === noIterationTypes) {
- continue;
- }
- if (iterationTypes === anyIterationTypes) {
- return anyIterationTypes;
- }
- yieldTypes = ts.append(yieldTypes, iterationTypes.yieldType);
- returnTypes = ts.append(returnTypes, iterationTypes.returnType);
- nextTypes = ts.append(nextTypes, iterationTypes.nextType);
+ function checkImportType(node) {
+ checkSourceElement(node.argument);
+ getTypeFromTypeNode(node);
+ }
+ function checkNamedTupleMember(node) {
+ if (node.dotDotDotToken && node.questionToken) {
+ grammarErrorOnNode(node, ts.Diagnostics.A_tuple_member_cannot_be_both_optional_and_rest);
}
- if (yieldTypes || returnTypes || nextTypes) {
- return createIterationTypes(yieldTypes && getUnionType(yieldTypes), returnTypes && getUnionType(returnTypes), nextTypes && getIntersectionType(nextTypes));
+ if (node.type.kind === 180 /* OptionalType */) {
+ grammarErrorOnNode(node.type, ts.Diagnostics.A_labeled_tuple_element_is_declared_as_optional_with_a_question_mark_after_the_name_and_before_the_colon_rather_than_after_the_type);
}
- return noIterationTypes;
+ if (node.type.kind === 181 /* RestType */) {
+ grammarErrorOnNode(node.type, ts.Diagnostics.A_labeled_tuple_element_is_declared_as_rest_with_a_before_the_name_rather_than_before_the_type);
+ }
+ checkSourceElement(node.type);
+ getTypeFromTypeNode(node);
}
- function getCachedIterationTypes(type, cacheKey) {
- return type[cacheKey];
+ function isPrivateWithinAmbient(node) {
+ return (ts.hasEffectiveModifier(node, 8 /* Private */) || ts.isPrivateIdentifierPropertyDeclaration(node)) && !!(node.flags & 8388608 /* Ambient */);
}
- function setCachedIterationTypes(type, cacheKey, cachedTypes) {
- return type[cacheKey] = cachedTypes;
+ function getEffectiveDeclarationFlags(n, flagsToCheck) {
+ var flags = ts.getCombinedModifierFlags(n);
+ // children of classes (even ambient classes) should not be marked as ambient or export
+ // because those flags have no useful semantics there.
+ if (n.parent.kind !== 253 /* InterfaceDeclaration */ &&
+ n.parent.kind !== 252 /* ClassDeclaration */ &&
+ n.parent.kind !== 221 /* ClassExpression */ &&
+ n.flags & 8388608 /* Ambient */) {
+ if (!(flags & 2 /* Ambient */) && !(ts.isModuleBlock(n.parent) && ts.isModuleDeclaration(n.parent.parent) && ts.isGlobalScopeAugmentation(n.parent.parent))) {
+ // It is nested in an ambient context, which means it is automatically exported
+ flags |= 1 /* Export */;
+ }
+ flags |= 2 /* Ambient */;
+ }
+ return flags & flagsToCheck;
}
- /**
- * Gets the *yield*, *return*, and *next* types from an `Iterable`-like or `AsyncIterable`-like type.
- *
- * At every level that involves analyzing return types of signatures, we union the return types of all the signatures.
- *
- * Another thing to note is that at any step of this process, we could run into a dead end,
- * meaning either the property is missing, or we run into the anyType. If either of these things
- * happens, we return `undefined` to signal that we could not find the iteration type. If a property
- * is missing, and the previous step did not result in `any`, then we also give an error if the
- * caller requested it. Then the caller can decide what to do in the case where there is no iterated
- * type.
- *
- * For a **for-of** statement, `yield*` (in a normal generator), spread, array
- * destructuring, or normal generator we will only ever look for a `[Symbol.iterator]()`
- * method.
- *
- * For an async generator we will only ever look at the `[Symbol.asyncIterator]()` method.
- *
- * For a **for-await-of** statement or a `yield*` in an async generator we will look for
- * the `[Symbol.asyncIterator]()` method first, and then the `[Symbol.iterator]()` method.
- */
- function getIterationTypesOfIterable(type, use, errorNode) {
- if (isTypeAny(type)) {
- return anyIterationTypes;
+ function checkFunctionOrConstructorSymbol(symbol) {
+ if (!produceDiagnostics) {
+ return;
}
- if (!(type.flags & 1048576 /* Union */)) {
- var iterationTypes_1 = getIterationTypesOfIterableWorker(type, use, errorNode);
- if (iterationTypes_1 === noIterationTypes) {
- if (errorNode) {
- reportTypeNotIterableError(errorNode, type, !!(use & 2 /* AllowsAsyncIterablesFlag */));
- }
- return undefined;
+ function getCanonicalOverload(overloads, implementation) {
+ // Consider the canonical set of flags to be the flags of the bodyDeclaration or the first declaration
+ // Error on all deviations from this canonical set of flags
+ // The caveat is that if some overloads are defined in lib.d.ts, we don't want to
+ // report the errors on those. To achieve this, we will say that the implementation is
+ // the canonical signature only if it is in the same container as the first overload
+ var implementationSharesContainerWithFirstOverload = implementation !== undefined && implementation.parent === overloads[0].parent;
+ return implementationSharesContainerWithFirstOverload ? implementation : overloads[0];
+ }
+ function checkFlagAgreementBetweenOverloads(overloads, implementation, flagsToCheck, someOverloadFlags, allOverloadFlags) {
+ // Error if some overloads have a flag that is not shared by all overloads. To find the
+ // deviations, we XOR someOverloadFlags with allOverloadFlags
+ var someButNotAllOverloadFlags = someOverloadFlags ^ allOverloadFlags;
+ if (someButNotAllOverloadFlags !== 0) {
+ var canonicalFlags_1 = getEffectiveDeclarationFlags(getCanonicalOverload(overloads, implementation), flagsToCheck);
+ ts.forEach(overloads, function (o) {
+ var deviation = getEffectiveDeclarationFlags(o, flagsToCheck) ^ canonicalFlags_1;
+ if (deviation & 1 /* Export */) {
+ error(ts.getNameOfDeclaration(o), ts.Diagnostics.Overload_signatures_must_all_be_exported_or_non_exported);
+ }
+ else if (deviation & 2 /* Ambient */) {
+ error(ts.getNameOfDeclaration(o), ts.Diagnostics.Overload_signatures_must_all_be_ambient_or_non_ambient);
+ }
+ else if (deviation & (8 /* Private */ | 16 /* Protected */)) {
+ error(ts.getNameOfDeclaration(o) || o, ts.Diagnostics.Overload_signatures_must_all_be_public_private_or_protected);
+ }
+ else if (deviation & 128 /* Abstract */) {
+ error(ts.getNameOfDeclaration(o), ts.Diagnostics.Overload_signatures_must_all_be_abstract_or_non_abstract);
+ }
+ });
}
- return iterationTypes_1;
}
- var cacheKey = use & 2 /* AllowsAsyncIterablesFlag */ ? "iterationTypesOfAsyncIterable" : "iterationTypesOfIterable";
- var cachedTypes = getCachedIterationTypes(type, cacheKey);
- if (cachedTypes)
- return cachedTypes === noIterationTypes ? undefined : cachedTypes;
- var allIterationTypes;
- for (var _i = 0, _a = type.types; _i < _a.length; _i++) {
- var constituent = _a[_i];
- var iterationTypes_2 = getIterationTypesOfIterableWorker(constituent, use, errorNode);
- if (iterationTypes_2 === noIterationTypes) {
- if (errorNode) {
- reportTypeNotIterableError(errorNode, type, !!(use & 2 /* AllowsAsyncIterablesFlag */));
- errorNode = undefined;
+ function checkQuestionTokenAgreementBetweenOverloads(overloads, implementation, someHaveQuestionToken, allHaveQuestionToken) {
+ if (someHaveQuestionToken !== allHaveQuestionToken) {
+ var canonicalHasQuestionToken_1 = ts.hasQuestionToken(getCanonicalOverload(overloads, implementation));
+ ts.forEach(overloads, function (o) {
+ var deviation = ts.hasQuestionToken(o) !== canonicalHasQuestionToken_1;
+ if (deviation) {
+ error(ts.getNameOfDeclaration(o), ts.Diagnostics.Overload_signatures_must_all_be_optional_or_required);
+ }
+ });
+ }
+ }
+ var flagsToCheck = 1 /* Export */ | 2 /* Ambient */ | 8 /* Private */ | 16 /* Protected */ | 128 /* Abstract */;
+ var someNodeFlags = 0 /* None */;
+ var allNodeFlags = flagsToCheck;
+ var someHaveQuestionToken = false;
+ var allHaveQuestionToken = true;
+ var hasOverloads = false;
+ var bodyDeclaration;
+ var lastSeenNonAmbientDeclaration;
+ var previousDeclaration;
+ var declarations = symbol.declarations;
+ var isConstructor = (symbol.flags & 16384 /* Constructor */) !== 0;
+ function reportImplementationExpectedError(node) {
+ if (node.name && ts.nodeIsMissing(node.name)) {
+ return;
+ }
+ var seen = false;
+ var subsequentNode = ts.forEachChild(node.parent, function (c) {
+ if (seen) {
+ return c;
+ }
+ else {
+ seen = c === node;
+ }
+ });
+ // We may be here because of some extra nodes between overloads that could not be parsed into a valid node.
+ // In this case the subsequent node is not really consecutive (.pos !== node.end), and we must ignore it here.
+ if (subsequentNode && subsequentNode.pos === node.end) {
+ if (subsequentNode.kind === node.kind) {
+ var errorNode_1 = subsequentNode.name || subsequentNode;
+ var subsequentName = subsequentNode.name;
+ if (node.name && subsequentName && (
+ // both are private identifiers
+ ts.isPrivateIdentifier(node.name) && ts.isPrivateIdentifier(subsequentName) && node.name.escapedText === subsequentName.escapedText ||
+ // Both are computed property names
+ // TODO: GH#17345: These are methods, so handle computed name case. (`Always allowing computed property names is *not* the correct behavior!)
+ ts.isComputedPropertyName(node.name) && ts.isComputedPropertyName(subsequentName) ||
+ // Both are literal property names that are the same.
+ ts.isPropertyNameLiteral(node.name) && ts.isPropertyNameLiteral(subsequentName) &&
+ ts.getEscapedTextOfIdentifierOrLiteral(node.name) === ts.getEscapedTextOfIdentifierOrLiteral(subsequentName))) {
+ var reportError = (node.kind === 165 /* MethodDeclaration */ || node.kind === 164 /* MethodSignature */) &&
+ ts.hasSyntacticModifier(node, 32 /* Static */) !== ts.hasSyntacticModifier(subsequentNode, 32 /* Static */);
+ // we can get here in two cases
+ // 1. mixed static and instance class members
+ // 2. something with the same name was defined before the set of overloads that prevents them from merging
+ // here we'll report error only for the first case since for second we should already report error in binder
+ if (reportError) {
+ var diagnostic = ts.hasSyntacticModifier(node, 32 /* Static */) ? ts.Diagnostics.Function_overload_must_be_static : ts.Diagnostics.Function_overload_must_not_be_static;
+ error(errorNode_1, diagnostic);
+ }
+ return;
+ }
+ if (ts.nodeIsPresent(subsequentNode.body)) {
+ error(errorNode_1, ts.Diagnostics.Function_implementation_name_must_be_0, ts.declarationNameToString(node.name));
+ return;
+ }
}
}
+ var errorNode = node.name || node;
+ if (isConstructor) {
+ error(errorNode, ts.Diagnostics.Constructor_implementation_is_missing);
+ }
else {
- allIterationTypes = ts.append(allIterationTypes, iterationTypes_2);
+ // Report different errors regarding non-consecutive blocks of declarations depending on whether
+ // the node in question is abstract.
+ if (ts.hasSyntacticModifier(node, 128 /* Abstract */)) {
+ error(errorNode, ts.Diagnostics.All_declarations_of_an_abstract_method_must_be_consecutive);
+ }
+ else {
+ error(errorNode, ts.Diagnostics.Function_implementation_is_missing_or_not_immediately_following_the_declaration);
+ }
}
}
- var iterationTypes = allIterationTypes ? combineIterationTypes(allIterationTypes) : noIterationTypes;
- setCachedIterationTypes(type, cacheKey, iterationTypes);
- return iterationTypes === noIterationTypes ? undefined : iterationTypes;
- }
- function getAsyncFromSyncIterationTypes(iterationTypes, errorNode) {
- if (iterationTypes === noIterationTypes)
- return noIterationTypes;
- if (iterationTypes === anyIterationTypes)
- return anyIterationTypes;
- var yieldType = iterationTypes.yieldType, returnType = iterationTypes.returnType, nextType = iterationTypes.nextType;
- return createIterationTypes(getAwaitedType(yieldType, errorNode) || anyType, getAwaitedType(returnType, errorNode) || anyType, nextType);
- }
- /**
- * Gets the *yield*, *return*, and *next* types from a non-union type.
- *
- * If we are unable to find the *yield*, *return*, and *next* types, `noIterationTypes` is
- * returned to indicate to the caller that it should report an error. Otherwise, an
- * `IterationTypes` record is returned.
- *
- * NOTE: You probably don't want to call this directly and should be calling
- * `getIterationTypesOfIterable` instead.
- */
- function getIterationTypesOfIterableWorker(type, use, errorNode) {
- if (isTypeAny(type)) {
- return anyIterationTypes;
- }
- if (use & 2 /* AllowsAsyncIterablesFlag */) {
- var iterationTypes = getIterationTypesOfIterableCached(type, asyncIterationTypesResolver) ||
- getIterationTypesOfIterableFast(type, asyncIterationTypesResolver);
- if (iterationTypes) {
- return iterationTypes;
+ var duplicateFunctionDeclaration = false;
+ var multipleConstructorImplementation = false;
+ var hasNonAmbientClass = false;
+ var functionDeclarations = [];
+ for (var _i = 0, declarations_4 = declarations; _i < declarations_4.length; _i++) {
+ var current = declarations_4[_i];
+ var node = current;
+ var inAmbientContext = node.flags & 8388608 /* Ambient */;
+ var inAmbientContextOrInterface = node.parent && (node.parent.kind === 253 /* InterfaceDeclaration */ || node.parent.kind === 177 /* TypeLiteral */) || inAmbientContext;
+ if (inAmbientContextOrInterface) {
+ // check if declarations are consecutive only if they are non-ambient
+ // 1. ambient declarations can be interleaved
+ // i.e. this is legal
+ // declare function foo();
+ // declare function bar();
+ // declare function foo();
+ // 2. mixing ambient and non-ambient declarations is a separate error that will be reported - do not want to report an extra one
+ previousDeclaration = undefined;
}
- }
- if (use & 1 /* AllowsSyncIterablesFlag */) {
- var iterationTypes = getIterationTypesOfIterableCached(type, syncIterationTypesResolver) ||
- getIterationTypesOfIterableFast(type, syncIterationTypesResolver);
- if (iterationTypes) {
- if (use & 2 /* AllowsAsyncIterablesFlag */) {
- // for a sync iterable in an async context, only use the cached types if they are valid.
- if (iterationTypes !== noIterationTypes) {
- return setCachedIterationTypes(type, "iterationTypesOfAsyncIterable", getAsyncFromSyncIterationTypes(iterationTypes, errorNode));
+ if ((node.kind === 252 /* ClassDeclaration */ || node.kind === 221 /* ClassExpression */) && !inAmbientContext) {
+ hasNonAmbientClass = true;
+ }
+ if (node.kind === 251 /* FunctionDeclaration */ || node.kind === 165 /* MethodDeclaration */ || node.kind === 164 /* MethodSignature */ || node.kind === 166 /* Constructor */) {
+ functionDeclarations.push(node);
+ var currentNodeFlags = getEffectiveDeclarationFlags(node, flagsToCheck);
+ someNodeFlags |= currentNodeFlags;
+ allNodeFlags &= currentNodeFlags;
+ someHaveQuestionToken = someHaveQuestionToken || ts.hasQuestionToken(node);
+ allHaveQuestionToken = allHaveQuestionToken && ts.hasQuestionToken(node);
+ var bodyIsPresent = ts.nodeIsPresent(node.body);
+ if (bodyIsPresent && bodyDeclaration) {
+ if (isConstructor) {
+ multipleConstructorImplementation = true;
+ }
+ else {
+ duplicateFunctionDeclaration = true;
+ }
+ }
+ else if ((previousDeclaration === null || previousDeclaration === void 0 ? void 0 : previousDeclaration.parent) === node.parent && previousDeclaration.end !== node.pos) {
+ reportImplementationExpectedError(previousDeclaration);
+ }
+ if (bodyIsPresent) {
+ if (!bodyDeclaration) {
+ bodyDeclaration = node;
}
}
else {
- return iterationTypes;
+ hasOverloads = true;
+ }
+ previousDeclaration = node;
+ if (!inAmbientContextOrInterface) {
+ lastSeenNonAmbientDeclaration = node;
}
}
}
- if (use & 2 /* AllowsAsyncIterablesFlag */) {
- var iterationTypes = getIterationTypesOfIterableSlow(type, asyncIterationTypesResolver, errorNode);
- if (iterationTypes !== noIterationTypes) {
- return iterationTypes;
- }
+ if (multipleConstructorImplementation) {
+ ts.forEach(functionDeclarations, function (declaration) {
+ error(declaration, ts.Diagnostics.Multiple_constructor_implementations_are_not_allowed);
+ });
}
- if (use & 1 /* AllowsSyncIterablesFlag */) {
- var iterationTypes = getIterationTypesOfIterableSlow(type, syncIterationTypesResolver, errorNode);
- if (iterationTypes !== noIterationTypes) {
- if (use & 2 /* AllowsAsyncIterablesFlag */) {
- return setCachedIterationTypes(type, "iterationTypesOfAsyncIterable", iterationTypes
- ? getAsyncFromSyncIterationTypes(iterationTypes, errorNode)
- : noIterationTypes);
- }
- else {
- return iterationTypes;
+ if (duplicateFunctionDeclaration) {
+ ts.forEach(functionDeclarations, function (declaration) {
+ error(ts.getNameOfDeclaration(declaration) || declaration, ts.Diagnostics.Duplicate_function_implementation);
+ });
+ }
+ if (hasNonAmbientClass && !isConstructor && symbol.flags & 16 /* Function */) {
+ // A non-ambient class cannot be an implementation for a non-constructor function/class merge
+ // TODO: The below just replicates our older error from when classes and functions were
+ // entirely unable to merge - a more helpful message like "Class declaration cannot implement overload list"
+ // might be warranted. :shrug:
+ ts.forEach(declarations, function (declaration) {
+ addDuplicateDeclarationError(declaration, ts.Diagnostics.Duplicate_identifier_0, ts.symbolName(symbol), declarations);
+ });
+ }
+ // Abstract methods can't have an implementation -- in particular, they don't need one.
+ if (lastSeenNonAmbientDeclaration && !lastSeenNonAmbientDeclaration.body &&
+ !ts.hasSyntacticModifier(lastSeenNonAmbientDeclaration, 128 /* Abstract */) && !lastSeenNonAmbientDeclaration.questionToken) {
+ reportImplementationExpectedError(lastSeenNonAmbientDeclaration);
+ }
+ if (hasOverloads) {
+ checkFlagAgreementBetweenOverloads(declarations, bodyDeclaration, flagsToCheck, someNodeFlags, allNodeFlags);
+ checkQuestionTokenAgreementBetweenOverloads(declarations, bodyDeclaration, someHaveQuestionToken, allHaveQuestionToken);
+ if (bodyDeclaration) {
+ var signatures = getSignaturesOfSymbol(symbol);
+ var bodySignature = getSignatureFromDeclaration(bodyDeclaration);
+ for (var _a = 0, signatures_10 = signatures; _a < signatures_10.length; _a++) {
+ var signature = signatures_10[_a];
+ if (!isImplementationCompatibleWithOverload(bodySignature, signature)) {
+ ts.addRelatedInfo(error(signature.declaration, ts.Diagnostics.This_overload_signature_is_not_compatible_with_its_implementation_signature), ts.createDiagnosticForNode(bodyDeclaration, ts.Diagnostics.The_implementation_signature_is_declared_here));
+ break;
+ }
}
}
}
- return noIterationTypes;
- }
- /**
- * Gets the *yield*, *return*, and *next* types of an `Iterable`-like or
- * `AsyncIterable`-like type from the cache.
- *
- * NOTE: You probably don't want to call this directly and should be calling
- * `getIterationTypesOfIterable` instead.
- */
- function getIterationTypesOfIterableCached(type, resolver) {
- return getCachedIterationTypes(type, resolver.iterableCacheKey);
- }
- function getIterationTypesOfGlobalIterableType(globalType, resolver) {
- var globalIterationTypes = getIterationTypesOfIterableCached(globalType, resolver) ||
- getIterationTypesOfIterableSlow(globalType, resolver, /*errorNode*/ undefined);
- return globalIterationTypes === noIterationTypes ? defaultIterationTypes : globalIterationTypes;
}
- /**
- * Gets the *yield*, *return*, and *next* types of an `Iterable`-like or `AsyncIterable`-like
- * type from from common heuristics.
- *
- * If we previously analyzed this type and found no iteration types, `noIterationTypes` is
- * returned. If we found iteration types, an `IterationTypes` record is returned.
- * Otherwise, we return `undefined` to indicate to the caller it should perform a more
- * exhaustive analysis.
- *
- * NOTE: You probably don't want to call this directly and should be calling
- * `getIterationTypesOfIterable` instead.
- */
- function getIterationTypesOfIterableFast(type, resolver) {
- // As an optimization, if the type is an instantiation of one of the following global types, then
- // just grab its related type argument:
- // - `Iterable` or `AsyncIterable