Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140131

TypeScript error: "Type 'number' is not assignable to type '0 | 1 | 2'". Why am I getting this error?

$
0
0

I am getting a weird TypeScript error.

I have the following example:

interface Foo {
  prop: 0 | 1 | 2;
}

class Bar implements Foo {
  prop = 1;
}

And I am getting the error:

src/example.ts:6:3 - error TS2416: Property 'prop' in type 'Bar' is not assignable to the same property in base type 'Foo'.
  Type 'number' is not assignable to type '0 | 1 | 2'.

6   prop = 1;
    ~~~~

Why does this code give the error?


Viewing all articles
Browse latest Browse all 140131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>