Skip to content

Nested type assertions in JS code are broken #7775

@ExE-Boss

Description

@ExE-Boss

Prettier 1.19.1
Playground link

When using type assertions, TypeScript sometimes requires an intermediate cast to unknown due to a lack of significant overlap between types.

Prettier’s formatting currently breaks this intermediate JSDoc‑based type assertion.

Input:

const BarImpl = /** @type {BarConstructor} */ (
	/** @type {unknown} */
	(function Bar() {
		throw new Error("Internal error: Illegal constructor");
	})
);

Note: BarImpl is wrapped by a Proxy later in the code, which takes care of the actual apply and construct calls, but that’s irrelevant to this issue.

Output:

const BarImpl /** @type {BarConstructor} */ =
	/** @type {unknown} */
	(function Bar() {
		throw new Error("Internal error: Illegal constructor");
	});

Expected behavior:

const BarImpl = /** @type {BarConstructor} */ (
	/** @type {unknown} */
	(function Bar() {
		throw new Error("Internal error: Illegal constructor");
	})
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:commentsIssues with how Prettier prints commentslang:javascriptIssues affecting JSlocked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions