I think i'm pretty close to getting a discriminated union working, but I've hit a snafu.
I'm getting an error of
Type '{ type: "string"; length: number; default: string | (() => string); db: (prop: ...) => void; }' is not assignable to type 'never'.
on trying to call a function inside my discriminated union
type vingProp =
| {
type: 'string',
length: number,
default: string | (() => string),
db: (prop: Extract<vingProp, { type: 'string' }>) => void,
}
| {
type: 'number',
min: number,
max: number,
default: number | (() => number),
db: (prop: Extract<vingProp, { type: 'number' }>) => void,
}
type vingSchema = {
props : vingProp[],
}
const userSchema:vingSchema = {
props : [
{
type : "string",
length: 60,
default: 'foo',
db : (prop) => stringFunc(prop)
},
{
type : "number",
default: 5,
min : 1,
max: 5,
db : (prop) => numberFunc(prop)
}
]
}
function stringFunc(prop:Extract<vingProp, { type : "string"}>) { console.log(prop) }
function numberFunc(prop:Extract<vingProp, { type : "number"}>) { console.log(prop) }
const columns: Record<string, any> = {};
for (const prop of userSchema.props) {
columns[prop.type] = prop.db(prop) // error is here
}