1
votes

How would I declare a type that is typeof B and any class that extends B? I've made a type that accepts an array of classes that extends the Component class, but this isn't working on TSC 3.(1.4|2.1).

export class Component<S> {}

export type ComponentClass = new () => Component<{ message: string }>;

export interface ComponentFlags<S> {
	imports?: (typeof Component | new () => Component<any>)[]
}

export class Message extends Component<State> {}

export class Dialog extends Component<State> {
	constructor(el: HTMLDivElement) {
		super({
			imports: [Message]
		});
	}
}

This then raises an error when compiled.

type 'typeof Message' is not assignable to type 'typeof Component'.
1
That sounds better.axiac
Code seems to be fine with TypeScript 3.2.2. No TypeScript error.lurker
There. Just exposed a bit of the code I'm working with. Should make more sense now.user9016207
@lurker That's the code I'm working with, but I've removed stuff that doesn't relate to the question - that I don't want anyone to see yet.user9016207

1 Answers

1
votes

Use this:

const a = <T extends new(...args: any) => B> (p: T|B) => p;

For your case:

export type NewComponnent = new (...args: any[]) => Component<any>;

export interface ComponentFlags<S> {
    imports?: (components: NewComponent[]) => void;
    // OR
    // imports?: (...components: NewComponent[]) => void;
}