question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement interface should not add optional properties

See original GitHub issue

From https://github.com/Microsoft/vscode/issues/46286

TypeScript Version: 2.8.0-dev.20180320

Search Terms:

  • implement interface
  • quick fix
  • code action
  • optional property

Code

interface IFoo {
    x?: number
    y: number
}

class Foo implements IFoo {

}

Trigger implement interface on Foo

Expected behavior: Only required properties required:

interface IFoo {
    x?: number
    y: number
}

class Foo implements IFoo {
    y: number;
}

Actual behavior: Both required and optional properties added:

interface IFoo {
    x?: number
    y: number
}

class Foo implements IFoo {
    x?: number;
    y: number;
}

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

8reactions
RyanCavanaughcommented, Mar 21, 2018

Why is this expected? It’s much easier to delete the optional properties/methods you don’t want than to track down the entire hierarchy of the target interface looking for them. We could add a comment or something or split it out into two commands if needed, but just opting them out feels like a regression in functionality.

2reactions
DanielRosenwassercommented, Feb 2, 2021

I like that idea - I think displaying “implement required members” first, and then “implement all members”, is probably the right direction.

Any opinions @mjbvz?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Force class to implement interface properties including optional
I'm looking for a way to force a class to implement all interface properties but keep them optional if they are in the...
Read more >
TypeScript - Interfaces with Optional Properties - LogicBig
In TypeScript, the interfaces which describe objects can have optional properties. Interfaces with optional properties are written similar ...
Read more >
c# - Implementing an interface when you don't need one of the ...
After all it's just a property. It could return 0 (the default property of int), or whatever value makes sense in your implementation...
Read more >
Handbook - Interfaces - TypeScript
Not all properties of an interface may be required. Some exist under certain conditions or may not be there at all. These optional...
Read more >
How to make Interface properties optional without changing ...
This approach will surely solve our error but we have a more elegant way to handle this rather than going to all interfaces...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found