C
C#2y 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
ero2y ago
I struggle to understand what this does. Doesn't this defeat the whole point of static typing?
Thinker
Thinker2y 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
enteleformOP2y 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
Accord2y 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.
Want results from more Discord servers?
Add your server