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 to remove keys from index type #51952

Closed
5 tasks done
Fuzzyma opened this issue Dec 19, 2022 · 3 comments
Closed
5 tasks done

Allow to remove keys from index type #51952

Fuzzyma opened this issue Dec 19, 2022 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@Fuzzyma
Copy link

Fuzzyma commented Dec 19, 2022

Suggestion

πŸ” Search Terms

index type, omit keys

βœ… 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

It is not possible to create a type, that has an index type + properties of a different type:

type A {
  [key: string]: number;
  foo: string
} // error

However, this case is not unsual and should be considered.
To achive that, it is needed to remove certain keys from the index.
In the definition above, this could simply be done by removing all defined keys (in this caseonly "foo") from the index automatically.

Removing keys from the index is also useful if you wanna add attributes with different type:

type A = Record<string, number>
type B = Omit<A, "foo"> & { foo: string }>

We could even go as far as making this work:

type A {
  [key: Extract<string, "foo">]: number;
  foo: string
}

A differen way of doing this without as much possibilities is to allow for negated matches in string literals:

type A {
  [key: `!foo${string}`]: number;
  foo: string;
}

(a similar issue for that example is here: #49867)

πŸ“ƒ Motivating Example

I guess I cramped all infos into the summary, I am sorry

πŸ’» Use Cases

Atm, mixing index types with other types is almost impossible. If you know, that you get an object that has certain typed values but also know that ONE keys is always set and different, you cannot type this properly atm because you cant have an index type + a different other key.

@MartinJohns
Copy link
Contributor

Duplicate of #4196 / #29317. The type system can't represent types like "A, except of B".

@jcalz
Copy link
Contributor

jcalz commented Dec 19, 2022

Also duplicate of #17867

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Dec 20, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants