franfrLor
franfrLor

Reputation: 99

Typescript solve lint function

I have this error returned by the linter with typescript : Don't use Function as a type. The Function type accepts any function-like value. It provides no type safety when calling the function, which can be a common source of bugs. It also accepts things like class declarations, which will throw at runtime as they will not be called with new. If you are expecting the function to accept certain arguments, you should explicitly define the function shape.eslint@typescript-eslint/ban-types)

I use the notify plugin from quasar: https://next.quasar.dev/quasar-plugins/notify

How can I resolve this error?

Here's my code

let masquerNotification:Function
    function afficherNotification () {
      if (notification.value && notification.value.message !== '') {
        masquerNotification = $q.notify({
          type: notification.value.type,
          message: notification.value.message,
          position: 'bottom-right',
          timeout: notification.value.timeout,
          actions: [{ icon: 'close', color: 'white' }]
        })
        void nettoyerNotification()
      }
      if (notification.value.masquer) {
        masquerNotification()
      }
    }

error!

Upvotes: -1

Views: 1629

Answers (2)

doggyxomax
doggyxomax

Reputation: 39

After a little research, where an array of classes was passed to the method isAllowed(Classes) and a check for every item was performed: instance of Class.

I found that it is possible to use an analog of Function: NewableFunction.

This is still not the right solution to the problem, but it helps with this case.

You can read this doc about callable and newable function types.

Update #1 (27.11.2024):

Finally found a solution!

You can use new(...args: any): OutputObjectClass notation inside curly brackets (object):

type ClassConstructor<T extends object = object> = { new(...args: any): T };

Then use as generic type:

const method = (Instance: ClassContructor) => { ... };

// or

const method = <T extends object>(Instance: ClassContructor<T>) => { ... }

Upvotes: 0

WebEXP0528
WebEXP0528

Reputation: 601

Please use ()=>void instead of Function Type.

let masquerNotification: () => void = () => {
  if (notification.value && notification.value.message !== '') {
    masquerNotification = $q.notify({
      type: notification.value.type,
      message: notification.value.message,
      position: 'bottom-right',
      timeout: notification.value.timeout,
      actions: [{ icon: 'close', color: 'white' }],
    });
    void nettoyerNotification();
  }
  if (notification.value.masquer) {
    masquerNotification();
  }
};

or

let masquerNotification: () => void = () => {
  function afficherNotification(){
    if (notification.value && notification.value.message !== '') {
      masquerNotification = $q.notify({
        type: notification.value.type,
        message: notification.value.message,
        position: 'bottom-right',
        timeout: notification.value.timeout,
        actions: [{ icon: 'close', color: 'white' }],
      });
      void nettoyerNotification();
    }
    if (notification.value.masquer) {
      masquerNotification();
    }
  }
};

Upvotes: 2

Related Questions