最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - How to make class extend interface? - Stack Overflow

programmeradmin1浏览0评论

Given

interface Foo {
  x: number
  fn?(): void
}

class Bar implements Foo {
  x = 1
}

const bar = new Bar()
bar.fn

The last line errors: bar does not appear to be a Foo.

Given

interface Foo {
  x: number
  fn?(): void
}

class Bar implements Foo {
  x = 1
}

const bar = new Bar()
bar.fn

The last line errors: bar does not appear to be a Foo.

Share Improve this question asked Mar 18 at 19:52 user29889977user29889977 1532 silver badges8 bronze badges 2
  • The error message I get when I try this is "Property 'fn' does not exist on type 'Bar'". – Bergi Commented Mar 18 at 20:51
  • You might be looking for const bar: Foo = new Bar() – Bergi Commented Mar 18 at 20:51
Add a comment  | 

2 Answers 2

Reset to default 0

The code throws error because of this:

The contract of the interface says that, the implementation should have the property x mandatorily and may have the property fn optionally.

Now, as we know, an implement clause is just a check. It checks the implementation against the interface. And it does this check in the below case as well. Since fn is optional, the implement clause does not make this mandatory. Therefore the implementation got succeeded without fn. However, the reference fails consequently, throwing the respective error. Please read the Cautions.

interface Foo {
  x: number
  fn?(): void
}

class Bar implements Foo {
  x = 1
}

const bar = new Bar()
bar.fn // throws error as Property 'fn' does not exist on type 'Bar'.

Solution proposed:

Access the optional property safely as below, please note TS does not throw error on the expression bar.fn here. This if statement over here guards the type, and removes the type undefined.

if( bar.fn !== undefined) {
   
}

I solved it as I was asking it:

You have to add interface Bar extends Foo {} like so:

interface Foo {
  x: number
  fn?(): void
}

class Bar implements Foo {
  x = 1
}

interface Bar extends Foo {}

const bar = new Bar()
bar.fn // <-- works now
发布评论

评论列表(0)

  1. 暂无评论