Skip to content

Instantly share code, notes, and snippets.

@wtho
Last active December 27, 2022 11:05
Show Gist options
  • Save wtho/f37f3f65f7ed7e3b86007cdd7da3572b to your computer and use it in GitHub Desktop.
Save wtho/f37f3f65f7ed7e3b86007cdd7da3572b to your computer and use it in GitHub Desktop.
Optional Attribute Type Definition as inline object
/**
* Definition of arg fields for typesafe event handling.
*
* @example
* {
* parameters: 'param1' | 'param2'
* arguments: 'arg1' | 'arg2'
* }
*/
interface ParameterArgDefinition {
/**
* Definition of query params keys. Type of value is `boolean`.
* @example 'param1' | 'param2'
*/
parameters?: string
/**
* Definition of path params. Type of value is `number`.
* @example 'arg1' | 'arg2'
*/
arguments?: string
}
/**
* TheInterface with nicely defineable optional parameters/arguments.
*
* Define `parameters` and `arguments` if required.
*
* @example
* TheInterface<{
* parameters: 'param1' | 'param2'
* arguments: 'arg1' | 'arg2'
* }>
*/
export interface TheInterface<Def extends ParameterArgDefinition = never> {
otherField: string;
definedParameters: [Def['parameters']] extends [string] ? { [P in Def['parameters']]: boolean } : unknown;
definedArguments: [Def['arguments']] extends [string] ? { [P in Def['arguments']]: number } : unknown;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment