1.0.16 • Published 4 years ago

tslint-primitive-interpolation v1.0.16

Weekly downloads
5
License
ISC
Repository
github
Last release
4 years ago

primitive-interpolation

String Interpolation, like ${myVariable} is usually type-unsafe.

for example when you have some logic that looks like:

class User {
  async email(): Promise<string> {
    return fetchFromSomeAPI();
  }
}

async function main(): Promise<void> {
  const user = new User();
  const message = `hello ${user.email()}, welcome to github`;
  // >>> `hello [object Promise], welcome to github`;
  // oof, you forget to await the user.email()
}

Using this linter rule, such logic error, would be captured during linting, with this message:

interpolated variable "user.email()" must be a primitive value, got Promise instead.

Reasoning:

The idea behind this rules, is that any variable to be interpolated, should be a primitive object e.g: number | boolean | string | enum

any, other type should first be whitelisted.

Whitelisting:

This rules accept an options a list of string. the name of type you want to allow. for example, if you're okay with doing string interpolation of a Date variable, you can whitelist it by putting in the options:

{
  "rules": {
    "primitive-interpolation": {
      "severity": "error",
      "options": ["Date"]
    }
  }
  // ... other configs.
}

Shortcoming:

  • unfortunately, this rules can't capture "any" and "unknown" type.
  • for instance-object of a whitelisted object, it'll follow the prototypal-chain. see here
  • for constructor of a whitelisted object, you'll need to whitelist them one by one. see here and here

Installation:

npm install tslint-primitive-interpolation --dev

Or if you're using yarn

yarn add tslint-primitive-interpolation --dev

And then add the rule-directory to your tslint.json

{
  "rules": { /** your linting rules */ },
  "rulesDirectory": [
    "node_modules/tslint-primitive-interpolation/dist"
  ]
}
1.0.16

4 years ago

1.0.15

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.10

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago