Tslint: Unable to import and use moment.js

Created on 11 Nov 2017  ·  3Comments  ·  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.8.0
  • __TypeScript version__: 2.6.1
  • __Running TSLint via__: (pick one) CLI

TypeScript code being linted

import * as moment from 'moment';
import { logger } from './utils/logger';

const x: moment.Moment = moment();

logger.info(x.toString());

with tslint.json configuration:

{
    "extends": ["tslint:all", "tslint-eslint-rules", "tslint-config-prettier"],
    "rules": {
        "completed-docs": [false],
        "member-ordering": [
            true,
            {
                "order": [
                    "public-static-field",
                    "protected-static-field",
                    "private-static-field",
                    "public-instance-field",
                    "private-instance-field",
                    "public-constructor",
                    "protected-constructor",
                    "private-constructor",
                    "public-static-method",
                    "protected-static-method",
                    "private-static-method",
                    "public-instance-method",
                    "private-instance-method"
                ]
            }
        ],
        "no-implicit-dependencies": false,
        "no-inferrable-types": false,
        "no-null-keyword": false,
        "prettier": [
            true,
            {
                "bracketSpacing": true,
                "jsxBracketSameLine": false,
                "parser": "typescript",
                "printWidth": 120,
                "semi": true,
                "singleQuote": true,
                "tabWidth": 4,
                "trailingComma": "all",
                "useTabs": false
            }
        ],
        "variable-name": [true, "allow-leading-underscore", "allow-pascal-case", "ban-keywords", "check-format"]
    },
    "rulesDirectory": ["tslint-plugin-prettier"]
}

Actual behavior

Errors reported:

/Users/mrandolph/Projects/***/***/src/server/test.ts
ERROR: 4:26   no-unsafe-any  Unsafe use of expression of type 'any'.
ERROR: 6:13   no-unsafe-any  Unsafe use of expression of type 'any'.

Expected behavior

The ability to use moment.js. As of now tslint is detecting it as type: any, which is causes linting errors. Interestingly, if I try to import a specific method, I still get the same no-unsafe-any errors:

Example:

import { duration, Duration } from 'moment';
import { logger } from './utils/logger';

const x: Duration = duration(1, 'week');

logger.info(x.toString());

Produces output:

/Users/mrandolph/Projects/***/***/src/server/test.ts
ERROR: 4:21   no-unsafe-any  Unsafe use of expression of type 'any'.
ERROR: 6:13   no-unsafe-any  Unsafe use of expression of type 'any'.

Just a heads up - .d.ts file is included with moment - no need to install from @types.

Duplicate

Most helpful comment

But what if I want to use no-unused-variable also?

There are several options:

Why does this only seem to affect moment in my project - I have many other 3rd party libraries that import just fine.

Nobody really knows what goes wrong and why. The rule somehow messes with typescript's internals.

All 3 comments

Just a heads up - .d.ts file is included with moment - no need to install from @types.

Thank you, that would've been my first question.

tslint:all enables no-unused-variable. This rule is known to interfere with other type checked rules: #2736
You could try to disable no-unused-variable and see if the errors go away.

You could try to disable no-unused-variable and see if the errors go away.

That worked. But what if I want to use no-unused-variable also? Why does this only seem to affect moment in my project - I have many other 3rd party libraries that import just fine.

But what if I want to use no-unused-variable also?

There are several options:

Why does this only seem to affect moment in my project - I have many other 3rd party libraries that import just fine.

Nobody really knows what goes wrong and why. The rule somehow messes with typescript's internals.

Was this page helpful?
0 / 5 - 0 ratings