2

In my project type declaration file, I want to use some Axios types as a part of some of my types, but their declarations are all exported.

I want to use AxiosResponse and AxiosError in my declaration file, but there are no dedicated @types for Axios. And when you look at the Axiox declaration file you can see that all of the interfaces and types have been exported, which make the compiler not automatically use those type no matter how I set up my tsconfig.json.

Here's my tsconfig:

{
  "compilerOptions": {
    "target": "es2017",
    "outDir": "dist/main",
    "rootDir": "src",
    "moduleResolution": "node",
    "module": "commonjs",
    "declaration": true,
    "inlineSourceMap": true,
    "esModuleInterop": true, 
    ...
    "lib": ["es2017"],
    "types": ["axios"],
    "typeRoots": ["node_modules/@types", "types", "node_modules"]
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "./test/**/*"
  ]
}

I just want to be able to use their interfaces to ensure the types I'm expecting.

fentech
  • 340
  • 2
  • 14
  • Just found this post about this same issue: https://stackoverflow.com/questions/39040108/import-class-in-definition-file-d-ts – fentech Apr 15 '19 at 18:11

1 Answers1

0

Would something like this work?

types/my/index.d.ts:

import { AxiosError } from "axios";

export interface MyType {
    error: AxiosError;
    p1: string;
}

src/index.ts (with some sample fields set):

import { MyType } from '../types/my';

let v1: MyType = {
    error: {
        config: {
            url: 'http:/example.com'
        },
        name: 'name',
        message: 'message'
    },
    p1: 'myMessage'
}

At least IntelliSense works and it compiles. See the code in GitHub here.

masa
  • 2,762
  • 3
  • 21
  • 32