Skip to content

Allow using the satisfies operator in JSDoc commentsΒ #54355

Closed
@jespertheend

Description

@jespertheend

Suggestion

πŸ” Search Terms

satisfies operator jsdoc

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

TypeScript has the satisfies operator, but there's no way to use this operator in JavaScript files with JSDoc.

πŸ“ƒ Motivating Example

Take the following TypeScript code:

type Foo = {
	bar: string;
	baz: number;
}

const x = {
	bar: "sldkfj",
	baz: 3,
} as const satisfies Foo;

This is currently not possible in JavaScript files

πŸ’» Use Cases

The example above gives the benefit of both type checking x for errors, as well as autocompleting property names when adding new properties.

Proposal

I think the same syntax for casting can be used here. Especially with the example above since that is already being cast:

/**
 * @typedef Foo
 * @property {string} bar
 * @property {number} baz
 */

const x = /** @type {const satisfies Foo} */ ({
	bar: "bar",
	baz: 3,
});

For cases where you don't want to cast as const similar syntax can be used:

const x = /** @type {satisfies Foo} */ ({
	bar: "bar",
	baz: 3,
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions