Z
Z
zlodiak2020-04-21 22:55:32
typescript
zlodiak, 2020-04-21 22:55:32

Why is it possible to add a number and an array?

typescript is a strongly typed language. That is, it does not allow type conversion during the operation. However, the following code does not throw any error messages:

const multy = ( a, b ) => a + b;

const number = 5;
const array = [ 0, 1, 2 ];

const result1 = multy( number, array[ 2 ] );
const result2 = multy( number, array );

console.log(result1, typeof result1)
console.log(result2, typeof result2)


Conclusion:
7 "number"
50,1,2 string


I thought that only javascript allows you to add a number and an array and get a string as an output, but typescript does not. But the code says otherwise. Please tell me where I'm wrong.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Yarkov, 2020-04-21
@zlodiak

Well, add types to multy arguments and you will be happy. And then you drown for the typescript, but I don’t see a single type in the code.

A
abberati, 2020-04-22
@abberati

Typescript is a language with weak static typing.
The tsconfig config allows you to configure the compiler in different ways - it can be configured so that it will only inform about errors and compile the code anyway. Google for keywords emit on error. And no implicit any, google it too.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question