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

use instanceof with a interface got compilation error #19120

Closed
rdkmaster opened this issue Oct 12, 2017 · 4 comments
Closed

use instanceof with a interface got compilation error #19120

rdkmaster opened this issue Oct 12, 2017 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@rdkmaster
Copy link

rdkmaster commented Oct 12, 2017

TypeScript Version: 2.5.3

Code

export interface ISlicedTableData {
    origin: RawTableData;
}

if (tableData instanceof ISlicedTableData) { // <- compilation error here.

}

Expected behavior:
no compilation error, and ts checks tableData is a implementation of ISlicedTableData or not

Actual behavior:

compilation error:  'ISlicedTableData' only refers to a type, but is being used as a value here.
@Ghabriel
Copy link

Interfaces only exist at compile-time and are removed after compilation, so that code makes no sense at run-time.

@rdkmaster
Copy link
Author

Thanks @Ghabriel

Is there any suggestion to do the type checking at runtime with an interface?

@Ghabriel
Copy link

You can try using type guards for that.

@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 12, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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

4 participants