#Limit config properties, based on (string) endpoint

9 messages · Page 1 of 1 (latest)

long moon
#

Hi everyone, struggling a bit with scoping a set of properties, based on the input defined, and at the same time controlling the output response of the fetch, with certain config.

There are two version of the same interface Clerk I need to combine how they work, so I can both limit the properties to correct allowed ones, and also return the correct type based on the inputs.

I've created a TS Playground here, that shows the issue: https://tinyurl.com/2x3ppc8m

edgy tundra
#

excess properties are generally allowed in TS, in case you weren't aware. the warning you get for contextually-typed object literals is a special case because it's provable that the property is never accessible anywhere else

agile acornBOT
#
const f = (x: { a: number }) => {}
const a = { a: 1, b: 2 }
f(a) // no error
edgy tundra
#

is there a problem caused in your program by allowing them?

long moon
#

Yes, can't be allowed. Must strictly follow the Config interfaces, else the API throws errors.

edgy tundra
#

you'll need to loop over the object at runtime then and strip out the properties you don't want. there's no way to model exact types in the type system

#

you might consider a validator library. i know that io-ts can automatically strip excess properties when validating, and i assumes others like zod can as well

long moon
#

Yeah thought about it, but kind of expected Typescript to actually be able to to this kind of stuff.

long moon
#

/resolved