I’m running into one of these different subtype of constraint errors and I know why it’s happening (see playground comments) but I’m not sure how to fix it.
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
11 messages · Page 1 of 1 (latest)
I’m running into one of these different subtype of constraint errors and I know why it’s happening (see playground comments) but I’m not sure how to fix it.
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
@viscid vector Here's a shortened URL of your playground link! You can remove the full link from your message.
Preview:```ts
interface ExternalProps {
myId: string
}
interface Base {
baseA: string
baseB: string
}
interface Derived extends Base {
baseA: string
baseB: string
c: string
}
const doSomething = (props: Derived): void => {}
// To fix error, need to be able to state "P extends Base but cannot contain any keys in ExternalProps"
...```
do you specifically care that component doesn't receive myId? (and if so, why?)
Hi @digital oxide sorry I got really sick the day after I posted this. If you're still able to help I'd really appreciate it.
The myId is basically a function that can only be called once (and I use it before calling component) so from a business logic standpoint there is no case in which it makes sense to forward it, that's why I was trying to extract it. I guess I could create a new dummy version to pass along? It seems like an odd thing to do.
@viscid vector Usually this error means there's some case that makes this unsafe.
e.g. I think in this case P could have a { myId } property and that wouldn't work here.
Right I was wondering if there's a way to prevent P from having myId or require it to be an optional property
Technically you could do (props: ExternalProps & Omit<P, "myId">), but I don't think that actually makes the type error go away.
I don't think that works, I did try changing component's signature to props: Omit<P, 'myId'> which actually makes the error go away but results in a different one. I guess since P is being inferred from component, adding an Omit in the type messes with the inference.
I guess broken inference is a workable solution though, just have to explicitly pass P:
higherOrderFunc<Derived>(doSomething). I wonder why the inference breaks though
Preview:```ts
interface ExternalProps {
myId: string
}
interface Base {
baseA: string
baseB: string
}
interface Derived extends Base {
baseA: string
baseB: string
c: string
}
const doSomething = (props: Derived): void => {}
// To fix error, need to be able to state "P extends Base but cannot contain any keys in ExternalProps"
...```