The operation '*=' is not valid for the numeric values of 'number | bigint' and 'number'

I have come across this permutation function in the js-combinatorics library that I would like to customize.

const _BI = typeof BigInt == 'function' ? BigInt : Number;

const _crop = (n) => n <= Number.MAX_SAFE_INTEGER ? Number(n) : _BI(n);

function permutation(n, k) {
  if (n < 0)
      throw new RangeError(`negative n is not acceptable`);
  if (k < 0)
      throw new RangeError(`negative k is not acceptable`);
  if (0 == k)
      return 1;
  if (n < k)
      return 0;
  [n, k] = [_BI(n), _BI(k)];
  let p = _BI(1);
  while (k--) 
      p *= n--;
  return _crop(p);

}

TypeScript gives me an error in the line p *= n--;

Operator '*=' cannot be applied to types 'number | bigint' and 'number'. 

Is there a way to resolve this issue?

Answer №1

This issue arises with the | operator and the *= operator.

  1. The *= operator cannot handle mixed types. It requires both sides to be of the same type, either number or bigint
  2. Even if you use a generic template like N extends number|bigint for both variables n and p, problems can still occur because n might end up being a bigint, a number, or a combination of both (simultaneous typing). Check out this link for more information: https://github.com/microsoft/TypeScript/issues/39569

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Using TypeScript: How to effectively wait for the completion of the array.sort

Need to implement a loading screen in Angular until an array is sorted. Solution provided below: this.isSorting = true; this.array = this.array.sort((a,b) => a.totlaTime - b.totalTime); this.isSorting = false; The issue faced is when isSorting is set t ...

Redeclaring block-scoped variable 'reducer' is not allowed in TypeScript

I encountered an error message Cannot redeclare block-scoped variable 'reducer' when running sample code from a book using tsc. I'm unsure of the reason behind this issue. tsc -v // Version 2.1.0-dev.20160726 Next, I executed ts-node in t ...

Personalized data type for the Request interface in express.js

I'm attempting to modify the type of query in Express.js Request namespace. I have already tried using a custom attribute, but it seems that this approach does not work if the attribute is already declared in @types (it only works for new attributes a ...

The [image link] in the NextJS image was loaded in advance using link preload, but it was not utilized within a short time after the window finished loading

While working on my blog website with NextJS, I encountered a warning in the console related to using next/image: The resource http://localhost:3000/_next/image... was preloaded using link preload but not used within a few seconds from the window's lo ...

Creating and incorporating a generator function within an interface and class: A step-by-step guide

In vanilla JavaScript, the code would look something like this: class Powers { *[Symbol.iterator]() { for(let i = 0; i < 10; i++) yield { i, pow: Math.pow(i, i) } return null; } } This can then be utilized in the following manner: co ...

What is the process for adding new methods to a predefined data type?

Currently, I am utilizing Webpack's require.context in order to eliminate redundancy while importing multiple pages. However, TypeScript is throwing an error stating that Property 'context' does not exist on type 'NodeRequire'.. I ...

Having trouble getting webpack to transpile typescript to ES5?

Despite following official guides and various tutorials, I am still facing an issue with compiling my code to ES5 using TypeScript and webpack. The problem is that the final bundle.js file always contains arrow functions. Here is a snippet from my webpack ...

Angular 13: How to Handle an Empty FormData Object When Uploading Multiple Images

I attempted to upload multiple images using "angular 13", but I'm unable to retrieve the uploaded file in the payload. The formData appears empty in the console. Any suggestions on how to resolve this issue? Here is the HTML code: <form [formGro ...

Streamline copyright verification with Angular

We are currently working on an angular application that we plan to release as open-source. We make sure to include copyright information in every file, specifically in the .ts and .scss files. However, being human, there are times when we may forget to ad ...

Example of TypeScript Ambient Namespace Usage

The Namespaces chapter provides an example involving D3.d.ts that I find puzzling. Here is the complete example: declare namespace D3 { export interface Selectors { select: { (selector: string): Selection; (element: ...

Struggling to create intricate validation with Yup for a Formik form

I am facing a challenge with my Formik form which contains complex validations. Below is the current schema I am working with: const applyPaymentFormValidation = yup.object().shape({ payments: yup.array().of( yup.object().shape({ applied: yup ...

adjust the child component by directly accessing its reference

Struggling to update a child component from the parent component using its reference, but running into some issues. Here's what I've attempted so far: class MainApp extends React.Component<any, any> { construct ...

What is the best way to remove query string parameters prior to running a function when a button is clicked?

I'm facing an issue trying to implement a button that filters events based on their tags. The problem arises when the tag value in the query string parameter does not clear when other buttons are clicked. Instead, the new filter tag value adds up with ...

You haven't included an index signature in the TypeScript type

I am creating MyInterface.dic to function as a dictionary with the format name: value, and here is how it is defined: interface MyInterface { dic: { [name: string]: number } } Next, I am writing a function that expects my specific type: function foo(a ...

What method can be used to seamlessly integrate Vue.js into a TypeScript file?

The focus here is on this particular file: import Vue from 'vue'; It's currently appearing in red within the IDE because the necessary steps to define 'vue' have not been completed yet. What is the best way to integrate without r ...

ways to convert to a specific subtype within a union

type example = { name : string | number | null } type processNumber = { name : number } const a : example = { name : 123 } as const function PROCESS (input : processNumber) { console.log(input.name) } PROCESS(a); I am facing an issue whe ...

Guide to transforming SQL text into a JSONB string

It's not clear how to: select 'a123'::text::jsonb results in ERROR: invalid input syntax for type json select '"a123"'::text::jsonb = INCORRECT string because it's quoted try select '"a123"'::t ...

Using pipes to filter JSON data based on key value pairs

I have a collection of items in the specified structure: { img: './app/images/codeeval.png', title: 'CodeEval', repository: 'https://github.com/Shooshte/CodeEval', description: ...

Struggling to reach the same level of bundle optimization with webpack + angular when compared to angular-cli

I am currently facing an issue with two Angular projects that I have. One is developed using angular-cli, while the other one is built with Webpack and utilizes @ngtools/webpack. Both projects are based on Angular 7.1.4 and @angular-devkit 0.13.5. The code ...

Refresh the information in the <ion-datetime> component by utilizing the formGroup

I am currently working with a form that has been created using 'FormBuilder'. The form includes a date control and I am trying to figure out how to update the data in that control using the patchValue() method. In the template, the control has d ...