Created
October 22, 2023 14:59
-
-
Save Luna-Klatzer/5611ec93c8aa2358c2d4d7043ac0bdc0 to your computer and use it in GitHub Desktop.
An attempt at developing a type that can infer the correct keys/value types for an interface based on a given schema.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The validation scheme for an option that is an object. | |
* @since 0.11.0 | |
*/ | |
export type KipperConfigObjectValidatorScheme = { | |
[key: string]: "string" | "boolean" | "array<string>" | KipperConfigObjectValidatorScheme; | |
} | |
/** | |
* The validation scheme for the Kipper config. | |
* | |
* Note that while no undefined types are specified, not all options have to be specified. This depends on the | |
* implementation of the {@link ConfigInterpreter}, but not defined arguments will usually be assigned a default value ( | |
* like with the implementation of the default {@link ConfigInterpreter}, with the exception ) | |
* @since 0.11.0 | |
*/ | |
export type KipperBaseConfigScheme = KipperConfigObjectValidatorScheme & { | |
/** | |
* The only option that is always allowed in the config is the `extends` option, which is used to extend another | |
* config. May be a relative or absolute path. | |
* @since 0.11.0 | |
*/ | |
"extends": "string"; | |
}; | |
/** | |
* The default scheme for a Kipper config file, which is used to validate the config. | |
* | |
* This scheme is the default scheme used by the {@link ConfigInterpreter} class, which will per default also only | |
* understand this scheme. | |
* @since 0.11.0 | |
*/ | |
export const defaultKipperConfigScheme = { | |
"extends": "string", | |
"version": "string", | |
"compileConfig": { | |
"target": "string", | |
"warnings": "boolean", | |
"recover": "boolean", | |
"optimisationOptions": { | |
"optimiseInternals": "boolean", | |
"optimiseBuiltIns": "boolean", | |
}, | |
}, | |
"resources": "array<string>", | |
} satisfies KipperConfigObjectValidatorScheme & KipperBaseConfigScheme; | |
/** | |
* The type of the default scheme for a Kipper config file. | |
* | |
* This scheme is the default scheme used by the {@link ConfigInterpreter} class, which will per default also only | |
* understand this scheme. | |
* @since 0.11.0 | |
*/ | |
export type DefaultKipperConfigScheme = typeof defaultKipperConfigScheme; | |
/** | |
* The default Kipper config output format. | |
* | |
* This will be generated when the {@link defaultKipperConfigScheme} is passed to the default {@link ConfigInterpreter} | |
* class. | |
* @since 0.11.0 | |
*/ | |
export type DefaultKipperConfigOutput = { | |
"compileConfig": CompileConfig, | |
} & Partial<{ | |
"version": string, | |
"resources": Array<path.ParsedPath>, | |
}> | |
/** | |
* The config type of the given scheme type. | |
* | |
* This type is used to generate the config type from the given scheme type, which represents the object type which the | |
* config has to follow. | |
* @since 0.11.0 | |
*/ | |
export type KipperConfig<T extends KipperConfigObjectValidatorScheme> = NonNullable<{ | |
[K in keyof T as T[K] extends "string" | "boolean" | "array<string>" | KipperConfigObjectValidatorScheme ? K : never]?: | |
T[K] extends "string" ? string : | |
T[K] extends "boolean" ? boolean : | |
T[K] extends "array<string>" ? Array<string> : | |
T[K] extends KipperConfigObjectValidatorScheme ? KipperConfig<T[K]> : | |
never; | |
}>; | |
var x: KipperConfig<typeof defaultKipperConfigScheme> = { | |
"extends": "string", | |
}; | |
x.compileConfig!!.warnings = true; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment