I want declare some properties in class
class a extends legacyJsClass {
declare id: number;
declare name: string;
}
but also want that this properties can be reused in other places, but it isn't work
type I = {
id: number;
name: string;
}
class a implements I {} // not work as declare
class a {
declare [K in keyof I]: I[K] // syntax don't exist
}
Thanks for any suggestion
CodePudding user response:
You can use declaration merging.
Consider this example:
interface Shape {
id: number;
name: string;
}
class Shape { }
const foo = new Shape()
foo.id // number
foo.name // string
However, it is not super safe, because, as you may have noticed, there are no id
and name
properties in the class.
Hence, I think this approach is a bit safer:
interface Shape {
id: number;
name: string;
}
class Foo implements Shape {
id = 42;
name = 'John'
}
const foo = new Foo()
foo.id // number
foo.name // string
CodePudding user response:
After inspecting the example of captain-yossarian I write next solution
export type RawType = {
name: string;
}
export interface Custom extends RawType {}
export class Custom extends Legacy<genericParam> {
getName() {
return this.name;
}
}