Consider the following scenario:
const enum BasicColor {
Red,
Green,
Blue
}
There is a method that can accept values from the enum above or any arbitrary number:
function foo(input: number | BasicColor) {
// implementation here
}
Is it necessary to explicitly specify the type as a union of number and BasicColor in order to indicate that both kinds of values are valid inputs?
Given that using either number or BasicColor as the parameter type allows for passing in all the same values (since enum-typed arguments can accept any number), and both calls will compile successfully regardless:
function bar(input: number) {
}
bar(BasicColor.Red);
function baz(input: BasicColor) {
}
baz(42);
Should we refrain from relying on the type system to serve as documentation in this case?