Open k-tten opened 3 years ago
See also the somewhat off-topic discussion at #41594
Need this to generate state repositories. The basic version could look like
type StateRepository<T extends string, E> =
& { [K in `set${Capitalize<T>}`]: (...args: [[T]: E]) => void }
// Dynamically named tuples are used here ^^^
& { [K in `get${Capitalize<T>}`]: () => E }
const capitalize = <S extends string>(string: S) => string.charAt(0).toUpperCase() + string.slice(1) as Capitalize<S>
const getStateRepository = <T extends string, E>(name: T, start: E): StateRepository<T, E> => {
let state = start
const cName = capitalize(name)
return {
[`set${cName}`]: data => { state = data },
[`get${cName}`]: () => state,
}
}
const counterRepository = getStateRepository('counter', 0)
counterRepository.setCounter(1)
const value = counterRepository.getCounter()
I think my example is too specific and not well presented. The more common problem people face is the one @awerlogus suggested: having a function whose return type is based on the parameters.
This would be great for ORMs for example. Instead of arbitrary names like id
, we could have dynamically named tuples and the argument name would be userId
, etc.
TL;DR: Dynamically named tuples would allow users to be more flexible and create more friendly complex types π
Commented on the design meeting issue related to this one with a motivating use-case. Linking it up here: https://github.com/microsoft/TypeScript/issues/55511#issuecomment-1747675356
Is there any update for this?
I ran into this issue today attempting to dynamically generate id parameter names from a known field tuple for primary keys:
function get(...ids: ExtractPrimaryKeys<Entity>) {}
Unfortunately, I am limited to:
function get(ids_0: number, ids_1: string) {}
I want to be able to name the tuple generated in ExtractPrimaryKeys
for better function argument documentation:
function get(organizationId: number, userId: string) {}
This would be epic!
My actual code looks like:
export type ExtractPrimaryKeys<T extends Entity<any, any>> = T["primaryKeyFields"] extends readonly [...infer U]
? [...{ [K in keyof U]: FieldTypeToTypeScriptType<T["primaryKeyFields"][K]> }]
: never;
But there is no way to dynamically set the generated tuple element name in this case.
One suggestion is that we could name generated tuple elements like [K in keyof U as "foo"]
:
export type ExtractPrimaryKeys<T extends Entity<any, any>> = T["primaryKeyFields"] extends readonly [...infer U]
? [...{ [K in keyof U as T["primaryKeyFields"][K]["name"]]: FieldTypeToTypeScriptType<T["primaryKeyFields"][K]> }]
: never;
Suggestion
TypeScript's type system is extremely expansive and powerful, but one thing that I cannot seem to do is give tuples dynamic names. Eg:
Or even:
Just like objects, we should be able to use bracket syntax to indicate that the expression inside should be used as the name.
We can then proceed to use other types to "calculate" the names.
π Search Terms
β Viability Checklist
My suggestion meets these guidelines:
I believe this one meets the last guideline because of goals 2 and 4.
β Suggestion
Dynamically/calculated/inserted tuple names instead of static ones.
π Motivating Example
Consider the following example (ripped from a project of mine):
Instead of boring, bland names, I could use this new feature to give them more descriptive/detailed names. Names are important because they describe the object in as few words as possible.
components_0
simply does not convey the same idea asx
.Suppose I create a type
InsertTupleNames
to create a tuple whose names are calculated, then I could use:Which means, the parameters are now named the correct names, and they make more sense to the end user.
Currently, I have to settle for a massive tuple containing all possible lists of parameter names, which is not ideal or maintainable.
π» Use Cases
It would make more complex types/functions more readable/usable.
I think this example I have shown demonstrates that TypeScript's type system is extremely powerful, and so powerful that we can already create standalone types based on another one (aka
CreateVector
createsVectorStruct<...>
andVectorStruct
is a standalone type, functional, and solely created fromCreateVector
).If we had this then we could make spread parameters and the outputted JavaScript easier to read with higher legibility.