※ 정보
타입스크립트 실행 결과 볼 때 코드가 바뀔때마다 명령어를 입력해서 결과를 볼 수 있다.
하지만 너무 번거로움. 그럴 때 ts-node 라이브러리 사용
요청보내는 라이브러리
import axios, { AxiosError, AxiosResponse } from "axios";
interface Post {}
interface Data {}
interface A {
get: <T, R = AxiosResponse<T>>(url: string) => Promise<R>;
post: <T, R = AxiosResponse<T>, D>(url: string, data: D) => Promise<R>;
(config: {}): void;
(url: string, config: {}): void;
isAxiosError: (error: unknown) => error is AxiosError;
}
// await 붙을 수 있는 애들은 리턴이 Promise다
(async () => {
try {
const a: A = await axios;
const response = await axios.get<Post, AxiosResponse<Post>, Data>(
"https://jsonplaceholder.typicode.com/posts/1"
);
const response2 = await axios.post(
"https://jsonplaceholder.typicode.com/posts/1",
{
name: "nini",
userId: 1,
}
);
} catch (error) {
// 커스텀 타입 가드
if (error instanceof AxiosError) {
console.error(error.response?.data);
// 추가로 message 같은 내용을 추가해주고 싶을 때
// 그러나 최대한 as 를 쓰지 않고 해결하는 것이 좋다.
console.error(
(error.response as AxiosResponse<{ message: string }>)?.data.message
);
}
}
})();
import React, {useState} from 'react'
import * as React from 'react';
import React = require('react')
export interface Middleware<DispatchExt = {}, S: any, D extends Dispatch> {
(api: MiddlewareAPI<D, S>): (next: Dispatch<AnyAction>) => (action: any) => any
}
api는 store고, next는 dispatch와 같다.
node
javascript 실행기, 런타임 (서버 x)
express
http 기반
※ 추가 개념
1. 타입 선언만 있고, 구현이 없는 것을 'ambient(앰비언트)'라고 한다
declare module .. => 이런 것이 앰비언트 모듈 - 특정 모듈에 대한 작업을 해주는 것.