#How to extract certain keys whose value is a certain type?

1 messages · Page 1 of 1 (latest)

glacial mortarBOT
#
mochoman#0777

Preview:```ts
type Entity = {
[key: string]: unknown
}

type ParticipantTag = {
uuid: string
name: string
colour: string | null
priority: string
}

type Relation<T> = T

type Participant = {
id: string
firstName: string
middleName: string
lastName: stri
...```

strange gull
#

So the only relations that can be included are tags - but this still allows all values

#

I could be going about it all wrong

thin ibex
#

Relation is just an identity type there, so your T[K] extends Relation<T[K]> condition will be true for any possible T[K]

#

there needs to be some structural difference about the relation properties that you can check

#

i'm not sure what's up for grabs here, but possibilities include:

  1. adjusting Relation to add some structure to it so you can tell just by looking at a value whether it is a Relation or not
  2. change the outer structure to make relations easier to identify (e.g. put them in a sub-object under a relations key)
  3. keep track of which keys are relations separately
#

all else being equal i'd probably vote for option 2, which could look something like this:

glacial mortarBOT
#
mkantor#7432

Preview:```ts
type Entity = {
[key: string]: unknown
}

type ParticipantTag = {
uuid: string
name: string
colour: string | null
priority: string
}

type Relation<T> = T

type Participant = {
id: string
firstName: string
middleName: string
lastName: stri
...```

strange gull
#

Hmm gotchya - 2 and 3 look promising

#

Not sure what other benefits I may getwith #2 as I am rebuilding the API as well so am open to this change