#Different subtype of constraint

11 messages · Page 1 of 1 (latest)

fleet hingeBOT
#

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

Destrolas#7684

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"
...```

digital oxide
#

do you specifically care that component doesn't receive myId? (and if so, why?)

viscid vector
#

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.

proven kernel
#

@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.

viscid vector
#

Right I was wondering if there's a way to prevent P from having myId or require it to be an optional property

proven kernel
#

Technically you could do (props: ExternalProps & Omit<P, "myId">), but I don't think that actually makes the type error go away.

viscid vector
#

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

fleet hingeBOT
#
Destrolas#7684

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"
...```