Reputation: 857
enum Type{
Video,
BlogPost = "BLOG_POST",
Quiz = "QUIZ"
}
I have the following code in type script, if I have a function createContent
const createContent = (contentType:Type) => {
console.log(contentType)
}
and call this function using
createContent(10)
this works. However, if all enum types are set to strings in the following code it does not allow a number to be passed in. Why is this?
enum Type{
Video = "VIDEO",
BlogPost = "BLOG_POST",
Quiz = "QUIZ"
}
Upvotes: 1
Views: 121
Reputation: 6747
Because an enum instance without a declared value is just an incrementing number. If you didn't write strings to it, it would mean something like this:
enum Type{
Video = 0,
BlogPost = 1,
Quiz = 2
}
Therefore, (if you leave one empty), your enum Type
is not of type string
, but rather string | number
and therefore you are allowed to pass a number OR a string.
Upvotes: 1