JavaScript Limit a class to include only functions with a specific signature

In my app I have several classes which are used to create XML strings. Each class has a few methods that take some arguments and return a string. I want to specify this limitation so that methods with a different signature or other class members can't be added. To this end, here's where I got so far:

interface IRequestParser {
  [funcName: string]: (...args: any[]) => string;
}

and an example of a class:

class myParser implements IRequestParser {
  [funcName: string]: (...args: any[]) => string;

  func1(a) {
    return '';
  }
}

This prevents me from adding functions that don't return strings, or any other non-method member, so these aren't allowed inside myParser:

  a: string; // not allowed
  b() { // not allowed
    return 5;
  }

However, this has the effect of letting me call any function name from an instance of myParser without an alert:

const a = new myParser();
console.log(a.func1('a'));
console.log(a.func2(4, 5, ['s', 'b']));

The call to func1 makes sense and I would also get an error if I didn't supply a value for its argument a. However, I can also call non-existing function func2 and any other name.

Is there a way to both limit the type of class members while also avoiding this situation of calling any function with any name?

Answer:1

You could also do this:

class MyParser implements Record<keyof MyParser, (...args: any[]) => string> {

  // okay
  func1(a: any) {
    return '';
  }

  // error: string is not assignable to (...args: any[]) => string
  a: string; 

  // error: number is not assignable to string
  b() { 
    return 5;
  }
}

const myParser = new MyParser();
myParser.func1("okay"); // works
myParser.funcNope("huh"); // error, no property funcNope

It's permissible to implement a concrete mapped type (like Record<>), and it is sometimes possible to have circular references (referring to keys via keyof is usually acceptable).

Hope that helps; good luck!

Answer:2

In my SPA I am using axios to make requests to the API. I currently use axios request interceptors to add auth headers to requests but I also wanted to use them to catch 401 errors and remove the ...

In my SPA I am using axios to make requests to the API. I currently use axios request interceptors to add auth headers to requests but I also wanted to use them to catch 401 errors and remove the ...

  1. axios catch request

I need regular expression to not allow only whitespaces in a field(the user should not enter just whitespaces in the field) but it can allow completely empty field and it can also allow string with ...

I need regular expression to not allow only whitespaces in a field(the user should not enter just whitespaces in the field) but it can allow completely empty field and it can also allow string with ...

  1. regular expression javascript tester
  2. regular expression javascript example
  3. regular expression javascript tutorial
  4. regular expression javascript test
  5. regular expression javascript phone number
  6. regular expression javascript mdn
  7. regular expression javascript generator
  8. regular expression javascript match
  9. regular expression javascript email
  10. regular expression javascript w
  11. regular expression javascript or
  12. regular expression javascript phone number validation
  13. regular expression javascript replace
  14. regular expression javascript space
  15. regular expression javascript validation
  16. regular expression javascript special characters
  17. regular expression javascript escape characters
  18. regular expression javascript for name
  19. regular expression javascript numbers only
  20. regular expression javascript for password

When it comes to custom extended element methods such as PlainJS closest() DOM Element method: // closest polyfill this.Element && function(ElementPrototype) { ElementPrototype.closest = ...

When it comes to custom extended element methods such as PlainJS closest() DOM Element method: // closest polyfill this.Element && function(ElementPrototype) { ElementPrototype.closest = ...

I want to be able to create a new array of objects by filtering one by multiple search terms Example: const arr = [ { 'city': 'Atlanta', 'state': 'Georgia' }, { 'city': '...

I want to be able to create a new array of objects by filtering one by multiple search terms Example: const arr = [ { 'city': 'Atlanta', 'state': 'Georgia' }, { 'city': '...

  1. filter array objects javascript
  2. filter array objects angular 6
  3. filter array objects
  4. filter array objects js
  5. filter array objects swift
  6. filter array objects jquery
  7. filter array objects angularjs
  8. filter array objects lodash
  9. filter array objects angular2
  10. array filter objects php
  11. filter array of objects by property
  12. filter array of objects typescript
  13. filter array of objects javascript es6
  14. filter array of objects by property javascript
  15. filter array of objects javascript by key
  16. filter array of objects by key
  17. filter array of objects by key value
  18. filter array of objects javascript by value
  19. filter array of objects based on property
  20. filter array of objects javascript based on property