Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Property does not exist" error on union type even though all union members implement interface #26204

Closed
distinctdan opened this issue Aug 4, 2018 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@distinctdan
Copy link

TypeScript Version: 3.0.1

Search Terms: Property does not exist union type implements interface

Code

interface IGameObject {
    onEvent?: (event) => void;
}

class Player implements IGameObject {}
class Enemy implements IGameObject {}

type MovingObjects = Player | Enemy;

const movingObjs: MovingObjects[] = [];

for (const obj of movingObjs) {
    // Error: "Property 'onEvent' does not exist on type 'MovingObjects[]'.""
    if (movingObjs.onEvent) {
        movingObjs.onEvent('someEvent');
    }
}

Expected behavior:
I would expect to be able to check if the method onEvent is present on the objects because they implement the interface, even though none of them implement the method yet. Some of them may implement it in the near future, so I want this check there to be safe. In my actual code, the interface also has non-optional properties that the classes do implement.

Actual behavior:
The compiler throws an error for "Property 'onEvent' does not exist on type 'MovingObjects[]'".

Playground Link:
https://www.typescriptlang.org/play/#src=interface%20IGameObject%20%7B%0D%0A%20%20%20%20onEvent%3F%3A%20(event)%20%3D%3E%20void%3B%0D%0A%7D%0D%0A%0D%0Aclass%20Player%20implements%20IGameObject%20%7B%7D%0D%0Aclass%20Enemy%20implements%20IGameObject%20%7B%7D%0D%0A%0D%0Atype%20MovingObjects%20%3D%20Player%20%7C%20Enemy%3B%0D%0A%0D%0Aconst%20movingObjs%3A%20MovingObjects%5B%5D%20%3D%20%5B%5D%3B%0D%0A%0D%0Afor%20(const%20obj%20of%20movingObjs)%20%7B%0D%0A%20%20%20%20%2F%2F%20Error%3A%20%22Property%20'onEvent'%20does%20not%20exist%20on%20type%20'MovingObjects%5B%5D'.%22%22%0D%0A%20%20%20%20if%20(movingObjs.onEvent)%20%7B%0D%0A%20%20%20%20%20%20%20%20movingObjs.onEvent('someEvent')%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A

Related Issues:

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Aug 4, 2018

Unfortunately, implementing an interface doesn't actually declare those members in the class. You'll need to explicitly declare onEvent (even as an optional member) in Player and Enemy.

@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Aug 4, 2018
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants