3

On a number of occasions, I have been burned by some Typescript behaviour that I'd like to disable.

I recently realized that a library I was using had a peer dependency on @types/bluebird for its Promise type.

I had not installed this dependency. A consequence of this was that any method from that library that returned a bluebird Promise, the inferred return type in my code was any. For example:

// Library .d.ts file
import * as Bluebird from 'bluebird';
const Promise: typeof Bluebird;
export const libraryFunction = () => Promise.resolve(42);

// My code .ts file

// Should be () => Promise<number>
// is actually () => any
const yUNoWorkRight = async () => libraryFunction(); 

Is there a setting that would cause this to fail or, at least, get caught by tslint?

noImplicitAny does not seem to help here.

Here's my TS config:

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "inlineSources": true,
    "jsx": "react",
    "lib": [
      "dom","es2018", "esnext"
    ],
    "module": "commonjs",
    "noImplicitAny": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "moduleResolution": "node",
    "outDir": "./built",
    "preserveConstEnums": true,
    "resolveJsonModule": true,
    "sourceMap": true,
    "strict": true,
    "strictNullChecks": true,
    "target": "es2018",
    "types": ["mocha", "node"],
    "sourceRoot": "/"
  },
  "include": [
    "src/**/*.ts", "src/**/*.js", "test/**/*.ts", "test/**/*.js"
  ]
}

and TSLint

{
  "linterOptions": {
    "exclude": [
      "**/*.json"
    ]
  },
  "rules": {
    "adjacent-overload-signatures": true,
    "prefer-for-of": true,
    "unified-signatures": true,
    "no-any": true,
    "label-position": true,
    "no-arg": true,
    "no-construct": true,
    "no-invalid-this": true,
    "no-misused-new": true,
    "no-shadowed-variable": true,
    "no-string-throw": true,
    "no-var-keyword": true,
    "strict-boolean-expressions": [
      true,
      "ignore-rhs"
    ],
    "triple-equals": [
      true,
      "allow-null-check",
      "allow-undefined-check"
    ]
  },
  "jsRules": {
    "object-literal-sort-keys": false,
    "no-console": false,
    "quotemark": false,
    "trailing-comma": false,
    "no-empty": [
      true,
      "allow-empty-functions"
    ]
  }
}
Dancrumb
  • 26,597
  • 10
  • 74
  • 130
  • 1
    Did you try `"strict": true` in your `tsconfig.json`? – ThomasThiebaud Dec 02 '19 at 20:45
  • Valid question. The answer is "yes". I've added my config details to help. – Dancrumb Dec 02 '19 at 20:56
  • 1
    AFAICT, it's because you have `allowJs` enabled. Imports from a JS file will be `any`, as there are no types. Related: https://stackoverflow.com/a/55795394/6680611 – cartant Dec 02 '19 at 21:04
  • @cartant none of these imports are from JS files. Nonetheless, I removed that property, since it's no longer necessary. This did not fix the issue at hand – Dancrumb Dec 02 '19 at 21:16
  • Bluebird is JS, surely, and that's where the `any` originates - unless its `@types` are installed. Can you make the question a little clearer? What do you mean be "replace a type"? I don't see any type replacement. The snippet declares - an unassigned - `Promise` variable, not a type. – cartant Dec 02 '19 at 21:26
  • Can you outline what's unclear in the question? (I'm not saying it's clear, I just don't know how to make it 'clearer' without knowing what's unclear). The question explains what I mean by "replace a type"... the declaration for `findOne` states that it returns a `Promise`, but during build, this is getting replaced by `any`. – Dancrumb Dec 02 '19 at 21:45
  • Also, you're correct that `bluebird` is a JS file, but it's a JS file that is a dependency of a dependency. I can't control that and removing `allowJs` has no impact on that. – Dancrumb Dec 02 '19 at 21:47
  • FWIW: VSCode flags this on the import of `bluebird`: Could not find a declaration file for module 'bluebird'. '~/ohana/node_modules/bluebird/js/release/bluebird.js' implicitly has an 'any' type. Try `npm install @types/bluebird` if it exists or add a new declaration (.d.ts) file containing `declare module 'bluebird';`ts(7016) – Dancrumb Dec 02 '19 at 21:48

0 Answers0