Closed
Description
TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)
Code
I'm new to typescript and I'm wondering why not typescript add the class shape interface likes example below?
interface InstanceInterface {
foo: string;
bar(): string;
}
interface ClassInterface {
// the constructor shape
new (foo: string): InstanceInterface;
// default static method
baz(): void {
console.log('I come from interface.')
}
}
// We get hints guiding us to implement the constructor and static properties and static methods.
class Cls: ClassInterface implements InstanceInterface {
constructor(foo: string) {
this.foo = foo
}
static baz(): void {
console.log('I override the interface baz.')
}
foo: string
bar(): string {
return this.foo
}
}
As I know, Java8 added the static method defining and default implements.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
aluanhaddad commentedon Aug 1, 2017
@zheeeng from a type checking point of view, you absolutely can do exactly what you want and with almost identical syntax:
Also, comparing TypeScript to Java makes no more sense than comparing JavaScript to Java.
zheeeng commentedon Aug 1, 2017
@aluanhaddad Thx, it works and is elegant!
A more question, is it being suggested to use in our programs? I don't know whether should we focus on shaping class in programming. If we should, I didn't find any practice writing like that and there are no the official guidelines or a convenient syntax likes I proposed. If I'm losing in confusions... I wish an explanation on why we don't need to check class shape interface.
[-]Why TypeScript doesn't provide a way to define the class shape interface?[/-][+]Why doesn't TypeScript provide a way to implement an interface on the static side?[/+]zheeeng commentedon Aug 2, 2017
@Andy-MS @RyanCavanaugh What if libs like React requires that developer must manually set the static property displayname on each class component? The typescript friendly hints will help a lot.