Kay
Kay2y 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
NeTT2y ago
Why not use two separate interfaces
Kay
KayOP2y 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
SyrupThinker2y 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
KayOP2y ago
yhea thx :)

Did you find this page helpful?