Preview:ts ... const users = useUserKind(useUserRecord) const warden = users.mint({id: "x", admin: true}) warden.talk()
#i have a working set of composables, but am having trouble with the last bit of typing
37 messages · Page 1 of 1 (latest)
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:
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
}
...```
the code still runs this way, but now the typescript error is on mint() instead of warden.talk()
I was able to cheat and make it work by adding as VR to the mint() return.
Preview:ts ... { mint(props) { return recordComposable({props}) as VR }, ...
However, I think it's likely cheating. So if there's someone who has the "right" way to do it, please let me know.
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
}
...```
you'll want to make sure recordComposable is a function that returns the correct type
nitpicks:
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...
(this shouldn't be an issue if you're planning on making functions like useUserRecord that return the proper type - which is the type-safe way to do it anyway)
i don't see anything different in what you did vs what i did
i wish there was a way to diff
look at the embed here
the parameter to useVingKind is now this:
{
recordComposable: (opts: {props: Props<T>}) => VR
}
the return type is VR instead of VingRecord<T>
oh i see, instead of doing typeof you declared the function type
yeah, it's the only way you'll get VR as the return type
well the other way was by cheating, which is what i did 🙂
heh
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?
the type ModelName = keyof Model
(also optional of course, especially if this is just a simplified representation of your actual code)
(instead of doing it manually)