I am new to TypeScript. I came across this scenario:
function testArgs(arg: string) {
console.log(typeof arg);
}
const arr = ['apple', 'banana', 'grapes'];
arr.forEach((rec, i) => {
testArgs(i);
});
The output is:
number
number
number
I know it is because ts code gets converted into js, thus console.log
prints number
as there are no types in js. But, shouldn't typescript convert the argument passed to the testArgs
method into string implicitly as the method argument accepts string arguments?
I am new to TypeScript. I came across this scenario:
function testArgs(arg: string) {
console.log(typeof arg);
}
const arr = ['apple', 'banana', 'grapes'];
arr.forEach((rec, i) => {
testArgs(i);
});
The output is:
number
number
number
I know it is because ts code gets converted into js, thus console.log
prints number
as there are no types in js. But, shouldn't typescript convert the argument passed to the testArgs
method into string implicitly as the method argument accepts string arguments?
-
You don't seem to call
testArgs
at all. – georg Commented Jun 24, 2019 at 17:45 - @georg I jsut updated the question – Pritam Bohra Commented Jun 24, 2019 at 17:50
- 1 there are types in js and also you probably aren't checking types while piling typescript as this should plain about trying to assign number to string also i dont know if you noticed that you are pushing index to testArgs – Xesenix Commented Jun 24, 2019 at 17:52
-
Did you mean
testArgs(rec)
? Your example fails pilation. – Aaron Beall Commented Jun 24, 2019 at 18:15
4 Answers
Reset to default 4It's important to remember that Typescript will never actually change a data type depending on the type information. This understanding es from the fundamental nature of types: They are there to help you to avoid making mistakes, they don't take an active role in the code.
That being said, Typescript can be as loose or as strict as you like. The stricter it is, the more "helpful" it is to you as a coder (and conversely, the more annoying it can be if you have lazy habits :P)
I would remend setting a few options in your tsconfig.json, which would have caught your error and you'd have been able to easily see the issue:
"strict": true,
"noImplicitReturns": true,
"noImplicitAny": true,
"noImplicitThis": true,
The result would have been that TypeScript would throw a pile error alerting you to the problem (the problem being that you're passing in the index, not the item):
error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
Incidentally, try to avoid using that old foreach syntax - it's very expensive (calling a function for every item) and can lead to messy code flow.
Try this instead:
for (const item of arr) {
testArgs(item);
}
you are getting type of index (i) that is a number you need to get typeof rec
const arr:string[] = ['apple', 'banana', 'grapes'];
arr.forEach((rec, i) => {
this.testArgs(rec); // you need to call this
console.log(typeof rec); //this is the one that will get the type of array element
console.log(typeof i); // This is getting type of index not the array
});
If you call testArgs
method with a number it won't convert it into a string
Typescript is meant for casting objects in javascript into a type, or in other words giving objects a type. It is not meant for type converting, basically converting your object's type into other
Data types in TypeScript are just for making sure that we are passing the correct type of data. After pilation it's just JavaScript, so when you use typeof
on an argument, JavaScript will evaluate the types based on the actual value passed, no matter what you have used as the datatype in the TypeScript function.
testArgs(1) - "number"
testArgs("1") - "string"