Potentially a bug, or just unexpected behavior. So I have this abstract class:
export abstract class Transformer {
abstract identifyViaRawStr: () => boolean;
abstract identifyViaJSObject: (v: any) => boolean;
abstract getValue: (v:any) => string;
abstract transformToBunionFormat: (v: any) => BunionJSON
}
and then I implement it with another class:
const c = new (class implements Transformer{
getValue(v: any) {
return '';
}
identifyViaJSObject: (v: any) => boolean;
identifyViaRawStr: () => boolean;
transformToBunionFormat: (v: any) => BunionJSON;
});
the above compiles fine, but looks funny since 3/4 methods are not implemented. It transpiles without an error to:
class Transformer {
}
exports.Transformer = Transformer;
const c = new (class {
getValue(v) {
return '';
}
});
I mean maybe this is correct, but it's not expected at least for me.
Is this a bug report? If so, there's a template. Is this a feature request? If so, there's a template. Is this a question? If so, this is not the place for it. I'm surprised you could create a new issue without being presented these options and associated templates. Oh well.
The code above looks like intended behavior when you have the --strictPropertyInitialization compiler option disabled. You should consider enabling it.
You've declared your methods as properties. Since those properties are declared in the subclass, TS treats them as implemented. You just didn't initialize them, which is a separate problem. If you're still confused:
abstract class C {
abstract foo: number;
}
class D extends C {
foo: number; // <-- foo is now implemented
}
Also, you should use extends for class inheritance, not implements. I don't feel like you fully understand how class works...
You've declared your methods as properties
I don't understand, how else would you declare them?
@ORESoftware I'm not a member here so I can't modify your issue, sorry. When I start creating an issue with my phone I still get the template, so... 🤷.
I'll reiterate: the compiler is working as intended here and there is no bug. Questions belong elsewhere, such as Stack Overflow. Good luck.
@jcalz pls chill dogg, this was a potential bug - when you file a bug ticket, you don't know for sure if it's bug or not, or just unexpected behavior.
❄️🐶👍
class C {
abstract method();
}
class D extends C {
method() { ...implementation... }
}
If you’re not intending to implement something in the derived class, don’t declare it. If you do and it’s not declared abstract, TS considers it to be implemented.
@ORESoftware abstract classes have been in the language for years; it's a bad guess that we just completely forgot to check how they work with interfaces and that no one else noticed either. Please stop using the issue tracker as a place to resolve your own surprises. If behavior isn't clearly broken, you should assume it's not a bug.
Most helpful comment
❄️🐶👍