#i have a working set of composables, but am having trouble with the last bit of typing

37 messages · Page 1 of 1 (latest)

storm quailBOT
#
plainblackguy#8238

Preview:ts ... const users = useUserKind(useUserRecord) const warden = users.mint({id: "x", admin: true}) warden.talk()

fiery goblet
#

The warden can talk, but typescript thinks the warden is VingRecord<'User'> rather than UserRecord

#

My thought was to pass the UserRecord type into useVingKind() so that it could then be attached to mint() and other functions like it, but that then gives me an error saying that VingRecord<'User'> cannot be assigned to UserRecord.

#

I'm lost, and need some help.

#

Here's what I mean by that last statement about passing the UserRecord:

storm quailBOT
#
plainblackguy#8238

Preview:```ts
...
function useVingKind<
T extends ModelName,
VR extends VingRecord<T>

({
recordComposable,
}: {
recordComposable: typeof useVingRecord<T>
}) {
const VingKind: VingKind<T, VR> = {
mint(props) {
return recordComposable({props})
},
}
return VingKind
}
...```

fiery goblet
#

the code still runs this way, but now the typescript error is on mint() instead of warden.talk()

fiery goblet
#

I was able to cheat and make it work by adding as VR to the mint() return.

storm quailBOT
#
plainblackguy#8238

Preview:ts ... { mint(props) { return recordComposable({props}) as VR }, ...

fiery goblet
#

However, I think it's likely cheating. So if there's someone who has the "right" way to do it, please let me know.

storm quailBOT
#
n_n#2622

Preview:```ts
...
function useVingKind<
T extends ModelName,
VR extends VingRecord<T>

({
recordComposable,
}: {
recordComposable: (opts: {props: Props<T>}) => VR
}) {
const VingKind: VingKind<T, VR> = {
mint(props) {
return recordComposable({props})
},
}
return VingKind
}
...```

plucky breach
#

you'll want to make sure recordComposable is a function that returns the correct type

#

nitpicks:

storm quailBOT
#
n_n#2622

Preview:ts interface Model { // optional: making it an interface lets users augment it later User: {id: string; admin: boolean} APIKey: {id: string; key: string} } type ModelName = keyof Model // optional: this type doesn't do very much, it can be replaced by its definition everywhere it's used type Props<T extends ModelName> = Model[T] ...

fiery goblet
#

reading...

plucky breach
fiery goblet
#

i don't see anything different in what you did vs what i did

#

i wish there was a way to diff

plucky breach
#

the parameter to useVingKind is now this:

{
  recordComposable: (opts: {props: Props<T>}) => VR
}
#

the return type is VR instead of VingRecord<T>

fiery goblet
#

oh i see, instead of doing typeof you declared the function type

plucky breach
#

yeah, it's the only way you'll get VR as the return type

fiery goblet
#

well the other way was by cheating, which is what i did 🙂

plucky breach
#

heh

fiery goblet
#

but now i'm going to back and fix it to be the right way

#

thank you

#

other than the 2 comments was there anything i was supposed to see in the nitpicks?

plucky breach
#

the type ModelName = keyof Model

#

(also optional of course, especially if this is just a simplified representation of your actual code)

plucky breach
fiery goblet
#

yeah, this is a dramatically simplified version

#

partially cuz i'm slow and partially because of how much it took to untangle it, that little sample took me 3 hours to put together so i could get your help

#

so thank you so much!

plucky breach
#

np

#

!close