frogtoprincejs
frogtoprincejs

Reputation: 161

Issue: TypeScript not finding the declaration file

I have two files in /src/models/ they are User.ts and User.d.ts. I am trying to build a class in User and then have a interface declaration for an object I use in User.d.ts. I thought User.ts would be able to use the interface automatically because typescript parses all the d.ts files? Is there something wrong with the config? Or maybe I am just not understanding the concept?

The Error I get is in the User.d.ts file:

Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: src/models/User.d.ts.
The file must be included in at least one of the projects provided

User.ts:

class User {
  private id: number;

  constructor(id: number) {
    this.id = id;
  }

  static create(userData: UserData): User | undefined {
    return undefined;
  }

  getId(): number {
    return this.id;
  }
}

export default User;

UserData.d.ts:

interface UserData {
  id?: number;
  gmail?: string;
  firstName?: string;
  lastName?: string;
  loginIP?: string;
  secureKey?: string;
  imgFileName?: string; // file name of the users profile image
  lastUpdate?: string;
  createDate?: string;
}

User.ts is having an issue finding UserData and I can't seem to use it in the file. My tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "target": "es6",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true
  },
  "include": ["/src/**/*.ts", "**/src/**/*.ts", "**/__tests__/**/*.ts"]
}

My .eslintrc.js

module.exports = {
  extends: ['airbnb', 'plugin:@typescript-eslint/recommended'],
  parser: '@typescript-eslint/parser',
  plugins: ['@typescript-eslint', 'prettier'],
  settings: {
    'import/parsers': {
      '@typescript-eslint/parser': ['.ts', '.tsx'],
    },
    'import/resolver': {
      typescript: {},
    },
  },
  rules: {
    'import/no-extraneous-dependencies': [2, { devDependencies: ['**/*.test.tsx', '**/*.test.ts'] }],
    '@typescript-eslint/indent': [2, 2],
    'import/extensions': [
      'error',
      'ignorePackages',
      {
        js: 'never',
        jsx: 'never',
        ts: 'never',
        tsx: 'never',
        mjs: 'never',
      },
    ],
  },
};

Where am I off? Appreciate the help. Thanks.

Upvotes: 6

Views: 9822

Answers (1)

Stark Jeon
Stark Jeon

Reputation: 1145

TLDR

Configure typeRoots config in tsconfig.json

Answer 1

The way I see it, you can use typeRoots in tsconfig.json. This config:

"compilerOptions": {
    ...
    "typeRoots" : ["./typings"]
    ...
 },

Answer 2

How about fix include options with "/src/**/*.d.ts"

"include": ["/src/**/*.ts", "**/src/**/*.ts", "**/__tests__/**/*.ts",  "/src/**/*.d.ts"]

Reference

  1. Typescript Handbook

Upvotes: 11

Related Questions