Reputation: 923
I want to create the class inheretid from Vinyl.
The constructor of superclass accepts the single parameter of ConstructorOptions
type.
export default class MarkupVinylFile extends Vinyl {
public constructor(options: ConstructorOptions) {
super(options);
}
// ...
}
Currently I can't import the ConstructorOptions
to use it as type annotation. In vinyl/index.d.ts
is has been declared as:
interface ConstructorOptions {
cwd?: string | undefined;
// ...
}
It could not be imported as import Vinyl, { ConstructorOptions } from "vinyl";
.
Is it possible to import the ConstructorOptions
?
Upvotes: 1
Views: 272
Reputation: 53225
As implied by @TobiasS. in the question comments, you can indirectly refer to this type.
Here we can use the ConstructorParameters
built-in utility type for this purpose:
Constructs a tuple or array type from the types of a constructor function type.
// The vinyl types merge a variable, an interface and a namespace,
// so to get the "class", we have to tell TS to target the variable with `typeof`
type ConstructorOptions = ConstructorParameters<typeof Vinyl>[0];
// ^? type ConstructorOptions = ConstructorOptions | undefined
// Alternatively, in case there are more than 1 argument,
// we could have used a rest operator with the full tuple:
export default class MarkupVinylFile extends Vinyl {
public constructor(...args: ConstructorParameters<typeof Vinyl>) {
super(...args);
}
// ...
}
That being said, if your constructor does nothing else than calling super()
, you could just omit it.
Note: you should get an error when trying to extends Vinyl
:
Base constructors must all have the same return type.(2510)
That is because the vinyl types declare several constructors with different return types.
Upvotes: 2