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

Allow using the satisfies operator in JSDoc comments #54355

Closed
5 tasks done
jespertheend opened this issue May 23, 2023 · 4 comments
Closed
5 tasks done

Allow using the satisfies operator in JSDoc comments #54355

jespertheend opened this issue May 23, 2023 · 4 comments

Comments

@jespertheend
Copy link
Contributor

jespertheend commented May 23, 2023

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,
});
@jespertheend
Copy link
Contributor Author

As a workaround, for now the same effect can still be achieved using a generic function:

/**
 * @template {Foo} T
 * @param {T} x
 */
const satisfiesFoo = x => x;

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

@MartinJohns
Copy link
Contributor

Duplicate of #51086. Used search terms: satisfies jsdoc in:title

@Andarist
Copy link
Contributor

This already works since 5.0: #51753

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

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

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

TS playground

@jespertheend
Copy link
Contributor Author

Ah sorry about that! Not sure how I missed this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants