suyoung
suyoung

Reputation: 13

how to type axios in reactjs

enter image description here

enter image description here

    const handleSubmit = () => {
        axios({
            method: changeMethod,
            url: changeSubmitValue,
            responseType: 'stream'
        }).then(function(response) {
            console.log(response);

        });
    };

There is an axios typescript error as above. The axios method changes according to the changeMethod state value.

index.d.ts(48, 2): The expected type comes from property'method' which is declared here on type'AxiosRequestConfig'

To get rid of this error, I need some help on where to type declaration in axios.

Upvotes: 1

Views: 533

Answers (1)

Stark Jeon
Stark Jeon

Reputation: 1155

How about fix like this union type.
Hello Korea people : )

import {Method} from "axios";
...
const handleSubmit = () => {
        axios({
            method: changeMethod as Method,
            url: changeSubmitValue,
            responseType: 'stream'
        }).then(function(response) {
            console.log(response);

        });
    };

Upvotes: 3

Related Questions