C
C#3y ago
enteleform

❔ Is there an equivalent feature to TypeScript's Indexed Access Types?

e.g.
type Example<T> = {
items: T[]
}

type StringItems = Example<string>["items"]
// string[]

function getStringItem(stringItems:StringItems){
// ...
}
type Example<T> = {
items: T[]
}

type StringItems = Example<string>["items"]
// string[]

function getStringItem(stringItems:StringItems){
// ...
}
4 Replies
ero
ero3y ago
I struggle to understand what this does. Doesn't this defeat the whole point of static typing?
Thinker
Thinker3y ago
Unfortunately C#'s type system is nowhere near as sophisticated as TypeScript's, so sadly this is not possible It's not possible to describe a type through other types like this.
enteleform
enteleformOP3y ago
It facilitates reuse of types rather than duplication. e.g. StringItems can be reused as many times as necessary, VS having to write Example<string>["items"] (or a much more complex type 💀) every time. More info @ https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html Gotcha, thanks for the info!
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?