Kay
Kay17mo ago

Dynamic interfaces

Is it possible to have like an interface that has a type entry and when it is a string for example the interface has a maxlength and minlength or when its a number it has a max and min?
4 Replies
NeTT
NeTT17mo ago
Why not use two separate interfaces
Kay
KayOP17mo ago
cous i have a function that accepts an entry wich can be multiple. but are all based on one. i saw this once where if you set the type to string you can set a default ut if you set the type to int you cans et a min and max so for example
interface {
type: "string" | "number" | "somethingElse"
name: string
description: string
min: number // This entry would only exist if type is number
max: number // This entry would only exist if type is number
default: string // This entry would only exist if type is string
}
interface {
type: "string" | "number" | "somethingElse"
name: string
description: string
min: number // This entry would only exist if type is number
max: number // This entry would only exist if type is number
default: string // This entry would only exist if type is string
}
SyrupThinker
SyrupThinker17mo ago
Something like this?
type XyzBase = { name: string; description: string };
type Xyz =
& XyzBase
& (
| { type: "string"; maxlength: number; minlength: number; default: string }
| { type: "number"; min: number; max: number }
| { type: "somethingElse" }
);
type XyzBase = { name: string; description: string };
type Xyz =
& XyzBase
& (
| { type: "string"; maxlength: number; minlength: number; default: string }
| { type: "number"; min: number; max: number }
| { type: "somethingElse" }
);
Kay
KayOP17mo ago
yhea thx :)