0

is there a way to add typing information to this function? More specifically how do you add type information to `arguments`?
function gtag() {
  dataLayer.push(arguments);
}

Of course the error you get is Expected 0 arguments, but got 2. when trying to call e.g. gtag(1, 2). This code snippet comes from google analytics.
Thanks

4
  • Look at the arguments object and IArguments. The arguments object is basically an indexed object, so it doesn't hold any type information. In your sample code, it would be impossible for typescript to make sure arguments is properly typed. Commented Feb 24, 2021 at 13:17
  • So it seems arguments cannot be used in typescript, this is odd since TS is supposedly a superset of JS. Also see: stackoverflow.com/questions/29918324/… Commented Feb 25, 2021 at 9:24
  • Of course you can use it Commented Feb 25, 2021 at 10:14
  • Except you get an error :D Commented Feb 25, 2021 at 11:02

1 Answer 1

0

This SO questions might have the answer for your case, however for our tsconfig / eslint settings this is something that allowed compilation:

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function gtag(..._args: any[]) {
  // eslint-disable-next-line prefer-rest-params
  dataLayer.push(arguments)
}

Note the underscore in _args. As to answer the question itself it seems that what @mike-s said is true and you cannot add type information to arguments.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.