TypeScriptのはじめかた(Windows)
node -v
cd "C:\Users\user-name\Documents\projct\type-project"
code .
npm init -y
npm i -D typescript ts-node
npm i axios node-fetch
npm i -D @types/node-fetch
npx tsc --init
{
"compilerOptions": {
"module": "ES2022",
"moduleResolution": "node",
}
}
{
"scripts": {
"dev": "node --loader ts-node/esm src/index.ts"
},
"type": "module",
}
npm run dev
import axios, { AxiosRequestConfig, AxiosResponse, AxiosError } from "axios";
// ***** axios *****
const url = "https://jsonplaceholder.typicode.com/users";
const method = "get";
interface OptionType {
url: string;
method: string;
}
interface User {
id: number;
name: string;
username: string;
email: string;
address: {};
phone: string;
website: string;
company: {};
}
const axiosOption: AxiosRequestConfig<OptionType> = { url, method };
axios<User[], AxiosResponse<User[], OptionType>, OptionType>(axiosOption)
.then((response) => {
console.log({
data: response.data,
status: response.status,
});
})
.catch((error: AxiosError<User[], OptionType>) => {
console.log(
{ isAxiosError: error.isAxiosError },
{ message: error.message }
);
});
この記事が気に入ったらサポートをしてみませんか?