Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Unable to import and use moment.js #3478

Closed
mrand01 opened this issue Nov 11, 2017 · 3 comments
Closed

Unable to import and use moment.js #3478

mrand01 opened this issue Nov 11, 2017 · 3 comments

Comments

@mrand01
Copy link

mrand01 commented Nov 11, 2017

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.

@ajafff
Copy link
Contributor

ajafff commented Nov 11, 2017

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.

@mrand01
Copy link
Author

mrand01 commented Nov 11, 2017

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.

@ajafff
Copy link
Contributor

ajafff commented Nov 11, 2017

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants