# tslint-primitive-interpolation

> tslint rules to disallow putting non-primitive values inside string interpolation.

Latest version **1.0.16** (published 2020-01-06) · ISC license · 0 weekly downloads

## Install

```sh
npm install tslint-primitive-interpolation
pnpm add tslint-primitive-interpolation
yarn add tslint-primitive-interpolation
bun add tslint-primitive-interpolation
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.16 |
| Published | 2020-01-06 |
| First published | 2020-01-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 39 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | cryptoket |

## Links

- npm: https://www.npmjs.com/package/tslint-primitive-interpolation
- Repository: https://github.com/SiestaMadokaist/primitive-interpolation
- Homepage: https://github.com/SiestaMadokaist/primitive-interpolation/#readme
- Issues: https://github.com/SiestaMadokaist/primitive-interpolation/issues
- npm.io page: https://npm.io/package/tslint-primitive-interpolation

## Recent versions

- 1.0.16 (latest) — 2020-01-06
- 1.0.15 — 2020-01-04
- 1.0.13 — 2020-01-04
- 1.0.12 — 2020-01-04
- 1.0.10 — 2020-01-04
- 1.0.8 — 2020-01-04
- 1.0.7 — 2020-01-04
- 1.0.6 — 2020-01-04
- 1.0.5 — 2020-01-04
- 1.0.4 — 2020-01-04
- 1.0.3 — 2020-01-04
- 1.0.2 — 2020-01-04

## README

# primitive-interpolation

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

for example when you have some logic that looks like:
```typescript
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:
```text
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:
```json
{
  "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](/src/primitiveInterpolationRule.spec.ts#L79)
- for constructor of a whitelisted object, you'll need to whitelist them one by one. see [here](/src/primitiveInterpolationRule.spec.ts#L100) and [here](/src/primitiveInterpolationRule.spec.ts#L105)

### Installation:
```shell
npm install tslint-primitive-interpolation --dev
```
Or if you're using yarn
```shell
yarn add tslint-primitive-interpolation --dev
```

And then add the rule-directory to your tslint.json
```json
{
  "rules": { /** your linting rules */ },
  "rulesDirectory": [
    "node_modules/tslint-primitive-interpolation/dist"
  ]
}
```

---
_Source: https://npm.io/package/tslint-primitive-interpolation · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
