Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operator '+' cannot be applied to types 'number[]' and '-1000' #18402

Closed
vhb56 opened this issue Sep 12, 2017 · 1 comment
Closed

Operator '+' cannot be applied to types 'number[]' and '-1000' #18402

vhb56 opened this issue Sep 12, 2017 · 1 comment
Labels
Question An issue which isn't directly actionable in code

Comments

@vhb56
Copy link

vhb56 commented Sep 12, 2017

Got a function from here:
https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
And TS says there is an error, not sure if it is true.
And in case this is not an error, but ts does count this as error - is there any way to @Suppress it?
Thanks.

TypeScript Version: 2.3.2 / but same in playground: https://www.typescriptlang.org/play/index.html

Code

  function uuidv4()
  {
    return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
      (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
    )
  }

Expected behavior:
No error?
Actual behavior:
Operator '+' cannot be applied to types 'number[]' and '-1000'.

@kitsonk
Copy link
Contributor

kitsonk commented Sep 12, 2017

This is not a support forum.

Questions should be asked at StackOverflow or on Gitter.im.

But... TypeScript is protecting you from implicit coercion, so you need to cast the array literal:

  function uuidv4()
  {
-    return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
+    return ([1e7] as any + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
      (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
    )
  }

@ahejlsberg ahejlsberg added the Question An issue which isn't directly actionable in code label Sep 12, 2017
@vhb56 vhb56 closed this as completed Sep 14, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants