Skip to content

No self or forward references in type parameter defaults #29179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7928,22 +7928,17 @@ namespace ts {
const numTypeArguments = length(typeArguments);
if (isJavaScriptImplicitAny || (numTypeArguments >= minTypeArgumentCount && numTypeArguments <= numTypeParameters)) {
const result = typeArguments ? typeArguments.slice() : [];

// Map an unsatisfied type parameter with a default type.
// If a type parameter does not have a default type, or if the default type
// is a forward reference, the empty object type is used.
const baseDefaultType = getDefaultTypeArgumentType(isJavaScriptImplicitAny);
const circularityMapper = createTypeMapper(typeParameters!, map(typeParameters!, () => baseDefaultType));
// Map invalid forward references in default types to the error type
for (let i = numTypeArguments; i < numTypeParameters; i++) {
result[i] = instantiateType(getConstraintFromTypeParameter(typeParameters![i]) || baseDefaultType, circularityMapper);
result[i] = errorType;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is effectively going back to the behavior in the original PR. It looks like the circularity was introduced in #28222 and #28423. Though, prior to this we were just defaulting to {} in TypeScript (any in JavaScript).

}
const baseDefaultType = getDefaultTypeArgumentType(isJavaScriptImplicitAny);
for (let i = numTypeArguments; i < numTypeParameters; i++) {
const mapper = createTypeMapper(typeParameters!, result);
let defaultType = getDefaultFromTypeParameter(typeParameters![i]);
if (isJavaScriptImplicitAny && defaultType && isTypeIdenticalTo(defaultType, emptyObjectType)) {
defaultType = anyType;
}
result[i] = defaultType ? instantiateType(defaultType, mapper) : baseDefaultType;
result[i] = defaultType ? instantiateType(defaultType, createTypeMapper(typeParameters!, result)) : baseDefaultType;
}
result.length = typeParameters!.length;
return result;
Expand Down Expand Up @@ -26465,6 +26460,7 @@ namespace ts {
if (produceDiagnostics) {
if (node.default) {
seenDefault = true;
checkTypeParametersNotReferenced(node.default, typeParameterDeclarations, i);
}
else if (seenDefault) {
error(node, Diagnostics.Required_type_parameters_may_not_follow_optional_type_parameters);
Expand All @@ -26479,6 +26475,24 @@ namespace ts {
}
}

/** Check that type parameter defaults only reference previously declared type parameters */
function checkTypeParametersNotReferenced(root: TypeNode, typeParameters: ReadonlyArray<TypeParameterDeclaration>, index: number) {
visit(root);
function visit(node: Node) {
if (node.kind === SyntaxKind.TypeReference) {
const type = getTypeFromTypeReference(<TypeReferenceNode>node);
if (type.flags & TypeFlags.TypeParameter) {
for (let i = index; i < typeParameters.length; i++) {
if (type.symbol === getSymbolOfNode(typeParameters[i])) {
error(node, Diagnostics.Type_parameter_defaults_can_only_reference_previously_declared_type_parameters);
}
}
}
}
forEachChild(node, visit);
}
}

/** Check that type parameter lists are identical across multiple declarations */
function checkTypeParameterListsIdentical(symbol: Symbol) {
if (symbol.declarations.length === 1) {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2537,6 +2537,10 @@
"category": "Error",
"code": 2743
},
"Type parameter defaults can only reference previously declared type parameters.": {
"category": "Error",
"code": 2744
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
Loading