Skip to content

Preserve quotes around property names in TypeScript classes #4516

@mariusschulz

Description

@mariusschulz

Prettier 1.12.1
Playground link

--parser typescript

Input:

class User {
  "username": string;
}

Output:

class User {
  username: string;
}

Expected behavior:

The output should be identical to the input:

class User {
  "username": string;
}

Otherwise, when the TypeScript code is compiled with --strictNullChecks and --strictPropertyInitialization (which are collectively set by default via the --strict flag), the type checker will raise a type error:

Property 'username' has no initializer and is not definitely assigned in the constructor.

This is because TypeScript's type checker treats properties with quoted names more leniently than unquoted ones. I suggest Prettier preserve quotes around property names in classes when using the TypeScript parser.

Steps to verify this behavior:

  1. Open this TypeScript playground.
  2. Click "Options" and enable both strictNullChecks and strictPropertyInitialization.
  3. Note the type error.

Metadata

Metadata

Assignees

Labels

lang:typescriptIssues affecting TypeScript-specific constructs (not general JS issues)locked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.priority:highCode is printed in a way that alters the AST, breaks syntax, or is a significant regression. Urgent!status:has prIssues with an accompanying pull request. These issues will probably be fixed soon!

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions