#Response types matches ALL keys in interface

31 messages ยท Page 1 of 1 (latest)

subtle plover
#
dense cryptBOT
#

@subtle plover Here's a shortened URL of your playground link! You can remove the full link from your message.

madsenmm#0

Preview:```ts
type EndpointBase = "/api/data"
type Endpoints =
| EndpointBase
| ${EndpointBase}/athletes
| ${EndpointBase}/brands

interface ResponseTypes {
"/api/data": {
custom: string
}
"/api/data/brands": {}
"/api/data/athletes": {
data: stri
...```

#
sandiford#0

Preview:```ts
type EndpointBase = "/api/data"
type Endpoints =
| EndpointBase
| ${EndpointBase}/athletes
| ${EndpointBase}/brands

interface ResponseTypes {
"/api/data": {
custom: string
}
"/api/data/brands": {}
"/api/data/athletes": {
data: stri
...```

sonic root
#

Is that it?

subtle plover
#

Jeeesus a brainfart, yes that was it ๐Ÿ˜„
Many thanks!

sonic root
#

T defaults to the constraint if there is nothing to infer it from, even with no default. It's a bit of a footgun.

subtle plover
#

!resolved

shut agate
#

You are not authorized to use this command.

subtle plover
#

Wait, not sure I understand what you mean by that Bert?
Should it be written otherwise?

sonic root
#

I mean that because you didn't have endpoint: T, it couldn't infer T

#

So defaulted T to Endpoints

#

Would be nice if it errored instead

subtle plover
#

Ah yes, thank you for the clarification ๐Ÿ™
A follow up question, if i have a dynamic api route on /api/data/athletes/:this-part-is-dynamic how does one define a route to match that part?

#

@sonic root ๐Ÿ™ Sorry for interrupting you again ๐Ÿ˜„

sonic root
#

${EndpointBase}/athletes/:${string} ?

subtle plover
#

It won't accept non-static values in the interface keys

#

Getting this error:

#

Got it working. Could write it like the following:

[key: `${EndpointBase}/athletes/${string}`]: {};
sonic root
#

Well yeah, I mean that's not really correct to think

#

When I've implemented things like this there is a concrete route

#

'/athletes/:id' or whatever

#

This will be converted into a regex, and you test the regex against the real route to see if it hits that route

#

Your current approach might work, unless you turn off noUncheckedIndexAccess, which reveals that your tsconfig is hidden an error, and actually the type is T | undefined

sonic root
subtle plover
#

So you wouldn't let typescript handle this, but more setup typeguards to ensure correct types?

sonic root
#

There's no way to represent that there is an object property for every string or every x/${string}

#

So there's not really a good way to do it. You couldn't actually satisfy this type, you'd need infinite properties. Unless using a Proxy which TS doesn't have a way to represent.

#

So my description is just how it works on my framework - that wasn't a solution from starting in your position, I just came at it from a different angle where my routes object had /foo/:bar and my type was just typeof routes

#

I mention it because it might be an option, or might give some ideas

#

I guess with what you're doing it technically works fine. You could always Exclude<T, undefined> if you turned on NUIA. So eh, maybe it's fine.

#

Like it's a bit of a hack, but it's convenient haha