Back to questions
JuniorTheoryCommon
On which line does tsc report an error?
function show(id: string | number) {
console.log(id.toString());
console.log(id.length);
}
show(42);3
- Expecting no error because
idis sometimes a string: TypeScript allows only what bothstringandnumberhave, and a number has nolength. - Expecting an error on line 2 as well:
toStringexists on both strings and numbers, so calling it onstring | numberis allowed.
- How can you call
toUpperCaseonidwithout a compile error? - What does tsc say about
id + 1whenidhas the typestring | number?