Skip to content
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

Error on mapped type w/properties #46346

Merged
merged 5 commits into from
Oct 18, 2021
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
20 changes: 18 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4973,7 +4973,7 @@ namespace ts {
const typeParameterNode = typeParameterToDeclarationWithConstraint(getTypeParameterFromMappedType(type), context, appropriateConstraintTypeNode);
const nameTypeNode = type.declaration.nameType ? typeToTypeNodeHelper(getNameTypeFromMappedType(type)!, context) : undefined;
const templateTypeNode = typeToTypeNodeHelper(removeMissingType(getTemplateTypeFromMappedType(type), !!(getMappedTypeModifiers(type) & MappedTypeModifiers.IncludeOptional)), context);
const mappedTypeNode = factory.createMappedTypeNode(readonlyToken, typeParameterNode, nameTypeNode, questionToken, templateTypeNode);
const mappedTypeNode = factory.createMappedTypeNode(readonlyToken, typeParameterNode, nameTypeNode, questionToken, templateTypeNode, /*members*/ undefined);
context.approximateLength += 10;
return setEmitFlags(mappedTypeNode, EmitFlags.SingleLine);
}
Expand Down Expand Up @@ -26811,6 +26811,10 @@ namespace ts {
function checkComputedPropertyName(node: ComputedPropertyName): Type {
const links = getNodeLinks(node.expression);
if (!links.resolvedType) {
if ((isTypeLiteralNode(node.parent.parent) || isClassLike(node.parent.parent) || isInterfaceDeclaration(node.parent.parent))
&& isBinaryExpression(node.expression) && node.expression.operatorToken.kind === SyntaxKind.InKeyword) {
return links.resolvedType = errorType;
}
links.resolvedType = checkExpression(node.expression);
// The computed property name of a non-static class field within a loop must be stored in a block-scoped binding.
// (It needs to be bound at class evaluation time.)
Expand Down Expand Up @@ -34736,6 +34740,7 @@ namespace ts {
}

function checkMappedType(node: MappedTypeNode) {
checkGrammarMappedType(node);
checkSourceElement(node.typeParameter);
checkSourceElement(node.nameType);
checkSourceElement(node.type);
Expand All @@ -34755,6 +34760,12 @@ namespace ts {
}
}

function checkGrammarMappedType(node: MappedTypeNode) {
if (node.members?.length) {
return grammarErrorOnNode(node.members[0], Diagnostics.A_mapped_type_may_not_declare_properties_or_methods);
}
}

function checkThisType(node: ThisTypeNode) {
getTypeFromThisTypeNode(node);
}
Expand Down Expand Up @@ -43417,6 +43428,11 @@ namespace ts {
}

function checkGrammarProperty(node: PropertyDeclaration | PropertySignature) {
if (isComputedPropertyName(node.name) && isBinaryExpression(node.name.expression) && node.name.expression.operatorToken.kind === SyntaxKind.InKeyword) {
return grammarErrorOnNode(
(node.parent as ClassLikeDeclaration | InterfaceDeclaration | TypeLiteralNode).members[0],
Diagnostics.A_mapped_type_may_not_declare_properties_or_methods);
}
if (isClassLike(node.parent)) {
if (isStringLiteral(node.name) && node.name.text === "constructor") {
return grammarErrorOnNode(node.name, Diagnostics.Classes_may_not_have_a_field_named_constructor);
Expand All @@ -43436,7 +43452,7 @@ namespace ts {
return grammarErrorOnNode(node.initializer, Diagnostics.An_interface_property_cannot_have_an_initializer);
}
}
else if (node.parent.kind === SyntaxKind.TypeLiteral) {
else if (isTypeLiteralNode(node.parent)) {
if (checkGrammarForInvalidDynamicName(node.name, Diagnostics.A_computed_property_name_in_a_type_literal_must_refer_to_an_expression_whose_type_is_a_literal_type_or_a_unique_symbol_type)) {
return true;
}
Expand Down
5 changes: 4 additions & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5993,7 +5993,10 @@
"category": "Error",
"code": 7060
},

"A mapped type may not declare properties or methods.": {
"category": "Error",
"code": 7061
},

"You cannot rename this element.": {
"category": "Error",
Expand Down
8 changes: 5 additions & 3 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2112,25 +2112,27 @@ namespace ts {
}

// @api
function createMappedTypeNode(readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined): MappedTypeNode {
function createMappedTypeNode(readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined, members: readonly TypeElement[] | undefined): MappedTypeNode {
const node = createBaseNode<MappedTypeNode>(SyntaxKind.MappedType);
node.readonlyToken = readonlyToken;
node.typeParameter = typeParameter;
node.nameType = nameType;
node.questionToken = questionToken;
node.type = type;
node.members = members && createNodeArray(members);
node.transformFlags = TransformFlags.ContainsTypeScript;
return node;
}

// @api
function updateMappedTypeNode(node: MappedTypeNode, readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined): MappedTypeNode {
function updateMappedTypeNode(node: MappedTypeNode, readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined, members: NodeArray<TypeElement> | undefined): MappedTypeNode {
return node.readonlyToken !== readonlyToken
|| node.typeParameter !== typeParameter
|| node.nameType !== nameType
|| node.questionToken !== questionToken
|| node.type !== type
? update(createMappedTypeNode(readonlyToken, typeParameter, nameType, questionToken, type), node)
|| node.members !== members
? update(createMappedTypeNode(readonlyToken, typeParameter, nameType, questionToken, type, members), node)
sandersn marked this conversation as resolved.
Show resolved Hide resolved
: node;
}

Expand Down
6 changes: 4 additions & 2 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ namespace ts {
visitNode(cbNode, (node as MappedTypeNode).typeParameter) ||
visitNode(cbNode, (node as MappedTypeNode).nameType) ||
visitNode(cbNode, (node as MappedTypeNode).questionToken) ||
visitNode(cbNode, (node as MappedTypeNode).type);
visitNode(cbNode, (node as MappedTypeNode).type) ||
visitNodes(cbNode, cbNodes, (node as MappedTypeNode).members);
case SyntaxKind.LiteralType:
return visitNode(cbNode, (node as LiteralTypeNode).literal);
case SyntaxKind.NamedTupleMember:
Expand Down Expand Up @@ -3534,8 +3535,9 @@ namespace ts {
}
const type = parseTypeAnnotation();
parseSemicolon();
const members = parseList(ParsingContext.TypeMembers, parseTypeMember);
parseExpected(SyntaxKind.CloseBraceToken);
return finishNode(factory.createMappedTypeNode(readonlyToken, typeParameter, nameType, questionToken, type), pos);
return finishNode(factory.createMappedTypeNode(readonlyToken, typeParameter, nameType, questionToken, type, members), pos);
}

function parseTupleElementType() {
Expand Down
6 changes: 4 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,8 @@ namespace ts {
readonly nameType?: TypeNode;
readonly questionToken?: QuestionToken | PlusToken | MinusToken;
readonly type?: TypeNode;
/** Used only to produce grammar errors */
readonly members?: NodeArray<TypeElement>;
Copy link
Member

Choose a reason for hiding this comment

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

Do we have any /* @internal */ node members? I assume not. Would make forEachChild act strange.

Copy link
Member

Choose a reason for hiding this comment

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

From what I've seen so far in our codebase, other node properties that are there for error reporting only just have a comment saying so 😕

}

export interface LiteralTypeNode extends TypeNode {
Expand Down Expand Up @@ -7212,8 +7214,8 @@ namespace ts {
updateTypeOperatorNode(node: TypeOperatorNode, type: TypeNode): TypeOperatorNode;
createIndexedAccessTypeNode(objectType: TypeNode, indexType: TypeNode): IndexedAccessTypeNode;
updateIndexedAccessTypeNode(node: IndexedAccessTypeNode, objectType: TypeNode, indexType: TypeNode): IndexedAccessTypeNode;
createMappedTypeNode(readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined): MappedTypeNode;
updateMappedTypeNode(node: MappedTypeNode, readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined): MappedTypeNode;
createMappedTypeNode(readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined, members: NodeArray<TypeElement> | undefined): MappedTypeNode;
updateMappedTypeNode(node: MappedTypeNode, readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined, members: NodeArray<TypeElement> | undefined): MappedTypeNode;
createLiteralTypeNode(literal: LiteralTypeNode["literal"]): LiteralTypeNode;
updateLiteralTypeNode(node: LiteralTypeNode, literal: LiteralTypeNode["literal"]): LiteralTypeNode;
createTemplateLiteralType(head: TemplateHead, templateSpans: readonly TemplateLiteralTypeSpan[]): TemplateLiteralTypeNode;
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/visitorPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,8 @@ namespace ts {
nodeVisitor(node.typeParameter, visitor, isTypeParameterDeclaration),
nodeVisitor(node.nameType, visitor, isTypeNode),
nodeVisitor(node.questionToken, tokenVisitor, isQuestionOrPlusOrMinusToken),
nodeVisitor(node.type, visitor, isTypeNode));
nodeVisitor(node.type, visitor, isTypeNode),
nodesVisitor(node.members, visitor, isTypeElement));

case SyntaxKind.LiteralType:
Debug.type<LiteralTypeNode>(node);
Expand Down
9 changes: 7 additions & 2 deletions src/services/codefixes/convertLiteralTypeToMappedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ namespace ts.codefix {
}

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, { container, typeNode, constraint, name }: Info): void {
changes.replaceNode(sourceFile, container, factory.createMappedTypeNode(/*readonlyToken*/ undefined,
factory.createTypeParameterDeclaration(name, factory.createTypeReferenceNode(constraint)), /*nameType*/ undefined, /*questionToken*/ undefined, typeNode));
changes.replaceNode(sourceFile, container, factory.createMappedTypeNode(
/*readonlyToken*/ undefined,
factory.createTypeParameterDeclaration(name, factory.createTypeReferenceNode(constraint)),
/*nameType*/ undefined,
/*questionToken*/ undefined,
typeNode,
/*members*/ undefined));
}
}
3 changes: 2 additions & 1 deletion src/services/codefixes/convertToMappedObjectType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ namespace ts.codefix {
mappedTypeParameter,
/*nameType*/ undefined,
indexSignature.questionToken,
indexSignature.type);
indexSignature.type,
/*members*/ undefined);
const intersectionType = factory.createIntersectionTypeNode([
...getAllSuperTypeNodes(container),
mappedIntersectionType,
Expand Down
10 changes: 6 additions & 4 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,8 @@ declare namespace ts {
readonly nameType?: TypeNode;
readonly questionToken?: QuestionToken | PlusToken | MinusToken;
readonly type?: TypeNode;
/** Used only to produce grammar errors */
readonly members?: NodeArray<TypeElement>;
}
export interface LiteralTypeNode extends TypeNode {
readonly kind: SyntaxKind.LiteralType;
Expand Down Expand Up @@ -3420,8 +3422,8 @@ declare namespace ts {
updateTypeOperatorNode(node: TypeOperatorNode, type: TypeNode): TypeOperatorNode;
createIndexedAccessTypeNode(objectType: TypeNode, indexType: TypeNode): IndexedAccessTypeNode;
updateIndexedAccessTypeNode(node: IndexedAccessTypeNode, objectType: TypeNode, indexType: TypeNode): IndexedAccessTypeNode;
createMappedTypeNode(readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined): MappedTypeNode;
updateMappedTypeNode(node: MappedTypeNode, readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined): MappedTypeNode;
createMappedTypeNode(readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined, members: NodeArray<TypeElement> | undefined): MappedTypeNode;
updateMappedTypeNode(node: MappedTypeNode, readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined, members: NodeArray<TypeElement> | undefined): MappedTypeNode;
createLiteralTypeNode(literal: LiteralTypeNode["literal"]): LiteralTypeNode;
updateLiteralTypeNode(node: LiteralTypeNode, literal: LiteralTypeNode["literal"]): LiteralTypeNode;
createTemplateLiteralType(head: TemplateHead, templateSpans: readonly TemplateLiteralTypeSpan[]): TemplateLiteralTypeNode;
Expand Down Expand Up @@ -10779,9 +10781,9 @@ declare namespace ts {
/** @deprecated Use `factory.updateIndexedAccessTypeNode` or the factory supplied by your transformation context instead. */
const updateIndexedAccessTypeNode: (node: IndexedAccessTypeNode, objectType: TypeNode, indexType: TypeNode) => IndexedAccessTypeNode;
/** @deprecated Use `factory.createMappedTypeNode` or the factory supplied by your transformation context instead. */
const createMappedTypeNode: (readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined) => MappedTypeNode;
const createMappedTypeNode: (readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined, members: NodeArray<TypeElement> | undefined) => MappedTypeNode;
/** @deprecated Use `factory.updateMappedTypeNode` or the factory supplied by your transformation context instead. */
const updateMappedTypeNode: (node: MappedTypeNode, readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined) => MappedTypeNode;
const updateMappedTypeNode: (node: MappedTypeNode, readonlyToken: ReadonlyKeyword | PlusToken | MinusToken | undefined, typeParameter: TypeParameterDeclaration, nameType: TypeNode | undefined, questionToken: QuestionToken | PlusToken | MinusToken | undefined, type: TypeNode | undefined, members: NodeArray<TypeElement> | undefined) => MappedTypeNode;
/** @deprecated Use `factory.createLiteralTypeNode` or the factory supplied by your transformation context instead. */
const createLiteralTypeNode: (literal: LiteralExpression | BooleanLiteral | PrefixUnaryExpression | NullLiteral) => LiteralTypeNode;
/** @deprecated Use `factory.updateLiteralTypeNode` or the factory supplied by your transformation context instead. */
Expand Down
Loading