I am working with some extensions:
type ExtensionOptions = { name: string }
type BoldOptions = ExtensionOptions & { boldShortcut?: string }
type ItalicOptions = ExtensionOptions & { italicShortcut?: string }
type LinkOptions = ExtensionOptions & { autoLink?: boolean }
declare class Extension {
constructor(options: ExtensionOptions)
}
declare class BoldExtension extends Extension {
isSelectionBold: boolean
constructor(options: BoldOptions)
}
declare class ItalicExtension extends Extension {
isSelectionItalic: boolean
constructor(options: ItalicOptions)
}
declare class LinkExtension extends Extension {
isSelectionLink: boolean
constructor(options: LinkOptions)
}
My goal is to define an Array type that allows passing Tuple containing an extension and its options with intellisense support. For example:
// Generic type. It currently lacks intellisense
type Extensions = Array<[Extension, ExtensionOptions]>
const extensions = [
[BoldExtension, { name: 'bold' }],
[ItalicExtension, { name: 'italic' }],
[LinkExtension, { name: 'link', autoLink: true }] // Intellisense needed for LinkExtension here
]
I attempted some experiments:
type Extensions<T> = Array<[
T,
T extends new (...args: infer Options) => any
? Options[0] // first argument should be the `options`
: never
]>
Here's the TypeScript playground link for reference.