Skip to content

Allowed AsExpression-like type assertions on object literal shorthands #21855

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

Closed
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
7 changes: 6 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14975,7 +14975,12 @@ namespace ts {
}
else {
Debug.assert(memberDecl.kind === SyntaxKind.ShorthandPropertyAssignment);
type = checkExpressionForMutableLocation(memberDecl.name, checkMode);
if (memberDecl.type) {
type = getTypeFromTypeNode(memberDecl.type);
}
else {
type = checkExpressionForMutableLocation(memberDecl.name, checkMode);
}
}

if (jsdocType) {
Expand Down
14 changes: 13 additions & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ namespace ts {
return visitNodes(cbNode, cbNodes, node.decorators) ||
visitNodes(cbNode, cbNodes, node.modifiers) ||
visitNode(cbNode, (<ShorthandPropertyAssignment>node).name) ||
visitNode(cbNode, (<ShorthandPropertyAssignment>node).type) ||
visitNode(cbNode, (<ShorthandPropertyAssignment>node).questionToken) ||
visitNode(cbNode, (<ShorthandPropertyAssignment>node).equalsToken) ||
visitNode(cbNode, (<ShorthandPropertyAssignment>node).objectAssignmentInitializer);
Expand Down Expand Up @@ -4591,14 +4592,25 @@ namespace ts {
// IdentifierReference[?Yield] Initializer[In, ?Yield]
// this is necessary because ObjectLiteral productions are also used to cover grammar for ObjectAssignmentPattern
const isShorthandPropertyAssignment =
tokenIsIdentifier && (token() === SyntaxKind.CommaToken || token() === SyntaxKind.CloseBraceToken || token() === SyntaxKind.EqualsToken);
tokenIsIdentifier && (
token() === SyntaxKind.CommaToken
|| token() === SyntaxKind.CloseBraceToken
|| token() === SyntaxKind.EqualsToken
|| token() === SyntaxKind.AsKeyword);
if (isShorthandPropertyAssignment) {
node.kind = SyntaxKind.ShorthandPropertyAssignment;
const equalsToken = parseOptionalToken(SyntaxKind.EqualsToken);
if (equalsToken) {
(<ShorthandPropertyAssignment>node).equalsToken = equalsToken;
(<ShorthandPropertyAssignment>node).objectAssignmentInitializer = allowInAnd(parseAssignmentExpressionOrHigher);
}
else {
const asKeyword = parseOptionalToken(118);
if (asKeyword) {
const asType = parseType();
(<ShorthandPropertyAssignment>node).type = asType;
}
}
}
else {
node.kind = SyntaxKind.PropertyAssignment;
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,8 @@ namespace ts {
// it is grammar error to appear in actual object initializer
equalsToken?: Token<SyntaxKind.EqualsToken>;
objectAssignmentInitializer?: Expression;
// Type assertion, as `<type>name` if before the name or `name as type` if after
type?: TypeNode;
}

export interface SpreadAssignment extends ObjectLiteralElement, JSDocContainer {
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ declare namespace ts {
questionToken?: QuestionToken;
equalsToken?: Token<SyntaxKind.EqualsToken>;
objectAssignmentInitializer?: Expression;
type?: TypeNode;
}
interface SpreadAssignment extends ObjectLiteralElement, JSDocContainer {
parent: ObjectLiteralExpression;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ declare namespace ts {
questionToken?: QuestionToken;
equalsToken?: Token<SyntaxKind.EqualsToken>;
objectAssignmentInitializer?: Expression;
type?: TypeNode;
}
interface SpreadAssignment extends ObjectLiteralElement, JSDocContainer {
parent: ObjectLiteralExpression;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
tests/cases/compiler/shorthandPropertyAssignmentsAsExpression.ts(40,12): error TS2352: Type 'number' cannot be converted to type 'Valid'.
tests/cases/compiler/shorthandPropertyAssignmentsAsExpression.ts(44,2): error TS2322: Type '{ extra: Valid; }' is not assignable to type 'ContainsValid'.
Object literal may only specify known properties, and 'extra' does not exist in type 'ContainsValid'.
tests/cases/compiler/shorthandPropertyAssignmentsAsExpression.ts(44,9): error TS2693: 'number' only refers to a type, but is being used as a value here.


==== tests/cases/compiler/shorthandPropertyAssignmentsAsExpression.ts (3 errors) ====
interface Valid {
general: number;
specific: 0;
optional?: 1;
}

const general = 2;
let specific = 0;

const valid = { general, specific };

specific = 2;

const expressionValid = {
general,
specific as 0,
};

const invalid = { general, specific };

const optional = 3;

const veryInvalid = {
general,
specific,
optional as number | undefined,
};

interface ContainsValid {
required: Valid;
optional?: Valid;
}

const fullContains: ContainsValid = {
required: {} as Valid,
optional: {} as Valid,
};

const invalidContains: ContainsValid = {
required: 7 as Valid,
~~~~~~~~~~
!!! error TS2352: Type 'number' cannot be converted to type 'Valid'.
};

const extraContains: ContainsValid = {
extra: number as Valid,
~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2322: Type '{ extra: Valid; }' is not assignable to type 'ContainsValid'.
!!! error TS2322: Object literal may only specify known properties, and 'extra' does not exist in type 'ContainsValid'.
~~~~~~
!!! error TS2693: 'number' only refers to a type, but is being used as a value here.
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//// [shorthandPropertyAssignmentsAsExpression.ts]
interface Valid {
general: number;
specific: 0;
optional?: 1;
}

const general = 2;
let specific = 0;

const valid = { general, specific };

specific = 2;

const expressionValid = {
general,
specific as 0,
};

const invalid = { general, specific };

const optional = 3;

const veryInvalid = {
general,
specific,
optional as number | undefined,
};

interface ContainsValid {
required: Valid;
optional?: Valid;
}

const fullContains: ContainsValid = {
required: {} as Valid,
optional: {} as Valid,
};

const invalidContains: ContainsValid = {
required: 7 as Valid,
};

const extraContains: ContainsValid = {
extra: number as Valid,
};


//// [shorthandPropertyAssignmentsAsExpression.js]
var general = 2;
var specific = 0;
var valid = { general: general, specific: specific };
specific = 2;
var expressionValid = {
general: general,
specific: specific,
};
var invalid = { general: general, specific: specific };
var optional = 3;
var veryInvalid = {
general: general,
specific: specific,
optional: optional,
};
var fullContains = {
required: {},
optional: {},
};
var invalidContains = {
required: 7,
};
var extraContains = {
extra: number,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
=== tests/cases/compiler/shorthandPropertyAssignmentsAsExpression.ts ===
interface Valid {
>Valid : Symbol(Valid, Decl(shorthandPropertyAssignmentsAsExpression.ts, 0, 0))

general: number;
>general : Symbol(Valid.general, Decl(shorthandPropertyAssignmentsAsExpression.ts, 0, 17))

specific: 0;
>specific : Symbol(Valid.specific, Decl(shorthandPropertyAssignmentsAsExpression.ts, 1, 17))

optional?: 1;
>optional : Symbol(Valid.optional, Decl(shorthandPropertyAssignmentsAsExpression.ts, 2, 13))
}

const general = 2;
>general : Symbol(general, Decl(shorthandPropertyAssignmentsAsExpression.ts, 6, 5))

let specific = 0;
>specific : Symbol(specific, Decl(shorthandPropertyAssignmentsAsExpression.ts, 7, 3))

const valid = { general, specific };
>valid : Symbol(valid, Decl(shorthandPropertyAssignmentsAsExpression.ts, 9, 5))
>general : Symbol(general, Decl(shorthandPropertyAssignmentsAsExpression.ts, 9, 15))
>specific : Symbol(specific, Decl(shorthandPropertyAssignmentsAsExpression.ts, 9, 24))

specific = 2;
>specific : Symbol(specific, Decl(shorthandPropertyAssignmentsAsExpression.ts, 7, 3))

const expressionValid = {
>expressionValid : Symbol(expressionValid, Decl(shorthandPropertyAssignmentsAsExpression.ts, 13, 5))

general,
>general : Symbol(general, Decl(shorthandPropertyAssignmentsAsExpression.ts, 13, 25))

specific as 0,
>specific : Symbol(specific, Decl(shorthandPropertyAssignmentsAsExpression.ts, 14, 9))

};

const invalid = { general, specific };
>invalid : Symbol(invalid, Decl(shorthandPropertyAssignmentsAsExpression.ts, 18, 5))
>general : Symbol(general, Decl(shorthandPropertyAssignmentsAsExpression.ts, 18, 17))
>specific : Symbol(specific, Decl(shorthandPropertyAssignmentsAsExpression.ts, 18, 26))

const optional = 3;
>optional : Symbol(optional, Decl(shorthandPropertyAssignmentsAsExpression.ts, 20, 5))

const veryInvalid = {
>veryInvalid : Symbol(veryInvalid, Decl(shorthandPropertyAssignmentsAsExpression.ts, 22, 5))

general,
>general : Symbol(general, Decl(shorthandPropertyAssignmentsAsExpression.ts, 22, 21))

specific,
>specific : Symbol(specific, Decl(shorthandPropertyAssignmentsAsExpression.ts, 23, 9))

optional as number | undefined,
>optional : Symbol(optional, Decl(shorthandPropertyAssignmentsAsExpression.ts, 24, 10))

};

interface ContainsValid {
>ContainsValid : Symbol(ContainsValid, Decl(shorthandPropertyAssignmentsAsExpression.ts, 26, 2))

required: Valid;
>required : Symbol(ContainsValid.required, Decl(shorthandPropertyAssignmentsAsExpression.ts, 28, 25))
>Valid : Symbol(Valid, Decl(shorthandPropertyAssignmentsAsExpression.ts, 0, 0))

optional?: Valid;
>optional : Symbol(ContainsValid.optional, Decl(shorthandPropertyAssignmentsAsExpression.ts, 29, 17))
>Valid : Symbol(Valid, Decl(shorthandPropertyAssignmentsAsExpression.ts, 0, 0))
}

const fullContains: ContainsValid = {
>fullContains : Symbol(fullContains, Decl(shorthandPropertyAssignmentsAsExpression.ts, 33, 5))
>ContainsValid : Symbol(ContainsValid, Decl(shorthandPropertyAssignmentsAsExpression.ts, 26, 2))

required: {} as Valid,
>required : Symbol(required, Decl(shorthandPropertyAssignmentsAsExpression.ts, 33, 37))
>Valid : Symbol(Valid, Decl(shorthandPropertyAssignmentsAsExpression.ts, 0, 0))

optional: {} as Valid,
>optional : Symbol(optional, Decl(shorthandPropertyAssignmentsAsExpression.ts, 34, 23))
>Valid : Symbol(Valid, Decl(shorthandPropertyAssignmentsAsExpression.ts, 0, 0))

};

const invalidContains: ContainsValid = {
>invalidContains : Symbol(invalidContains, Decl(shorthandPropertyAssignmentsAsExpression.ts, 38, 5))
>ContainsValid : Symbol(ContainsValid, Decl(shorthandPropertyAssignmentsAsExpression.ts, 26, 2))

required: 7 as Valid,
>required : Symbol(required, Decl(shorthandPropertyAssignmentsAsExpression.ts, 38, 40))
>Valid : Symbol(Valid, Decl(shorthandPropertyAssignmentsAsExpression.ts, 0, 0))

};

const extraContains: ContainsValid = {
>extraContains : Symbol(extraContains, Decl(shorthandPropertyAssignmentsAsExpression.ts, 42, 5))
>ContainsValid : Symbol(ContainsValid, Decl(shorthandPropertyAssignmentsAsExpression.ts, 26, 2))

extra: number as Valid,
>extra : Symbol(extra, Decl(shorthandPropertyAssignmentsAsExpression.ts, 42, 38))
>Valid : Symbol(Valid, Decl(shorthandPropertyAssignmentsAsExpression.ts, 0, 0))

};

Loading