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

make Join work with const tuple values #533

Merged
merged 1 commit into from Dec 25, 2022

Conversation

dwelle
Copy link
Contributor

@dwelle dwelle commented Dec 25, 2022

Make Join type work by passing a type of a const value.

Use case:

const columns = ["id", "name"] as const;  
const toSelectQuery = <T extends readonly string[]>(columns: T): Join<T, ","> => {
  return columns.join(",") as Join<T, ",">;
}
const query = toSelectQuery(columns); // "id,name"

Sadly, when passing arguments as literals you still have to cast as const explicitly:

const query = toSelectQuery(["id", "name"] as const); // "id,name"

That will not be necessary in TS@5.0 by using a const modifier, see playground.

@sindresorhus sindresorhus merged commit db81191 into sindresorhus:main Dec 25, 2022
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

Successfully merging this pull request may close these issues.

None yet

2 participants