Typescript: use `instanceof` with a interface got compilation error

Created on 12 Oct 2017  路  4Comments  路  Source: microsoft/TypeScript




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.
Working as Intended

Most helpful comment

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

All 4 comments

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

Thanks @Ghabriel

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

You can try using type guards for that.

Was this page helpful?
0 / 5 - 0 ratings