【发布时间】:2022-02-11 00:54:20
【问题描述】:
这是我第一次尝试打字稿。我正在关注一个处理令牌刷新链接的 youtube 用户身份验证 jwt 教程。我遇到了一个问题,但是教程视频没有这个问题,我不知道如何解决。
这是错误日志:
H:/jwt-auth/web/src/index.tsx
TypeScript error in H:/jwt-auth/web/src/index.tsx(48,5):
Type 'TokenRefreshLink<string>' is not assignable to type 'ApolloLink'.
Types of property 'split' are incompatible.
Type '(test: (op: import("H:/jwt-auth/web/node_modules/@apollo/client/link/core/types").Operation) => boolean, left: import("H:/jwt-auth/web/node_modules/@apollo/client/link/core/ApolloLink").ApolloLink | import("H:/jwt-auth/web/node_modules/@apollo/client/link/core/types").RequestHandler, right?: import("H:/jwt-auth/web/...' is not assignable to type '(test: (op: import("H:/jwt-auth/web/node_modules/apollo-link/lib/types").Operation) => boolean, left: import("H:/jwt-auth/web/node_modules/apollo-link/lib/link").ApolloLink | import("H:/jwt-auth/web/node_modules/apollo-link/lib/types").RequestHandler, right?: import("H:/jwt-auth/web/node_modules/apollo-link/lib/link...'.
Types of parameters 'test' and 'test' are incompatible.
Types of parameters 'op' and 'op' are incompatible.
Property 'toKey' is missing in type 'import("H:/jwt-auth/web/node_modules/@apollo/client/link/core/types").Operation' but required in type 'import("H:/jwt-auth/web/node_modules/apollo-link/lib/types").Operation'. TS2322
46 | const client = new ApolloClient({
47 | link: ApolloLink.from([
> 48 | new TokenRefreshLink({
| ^
49 | accessTokenField: "accessToken",
50 | isTokenValidOrUndefined: () => {
51 | const token = getAccessToken();
这是我的代码:
const client = new ApolloClient({
link: ApolloLink.from([
new TokenRefreshLink({
accessTokenField: "accessToken",
isTokenValidOrUndefined: () => {
const token = getAccessToken();
if (!token) {
return true;
}
try {
const { exp } = jwtDecode(token);
if (Date.now() >= exp * 1000) {
return false;
} else {
return true;
}
} catch {
return false;
}
},
fetchAccessToken: () => {
return fetch("http://localhost:4000/refresh_token", {
method: "POST",
credentials: "include",
});
},
handleFetch: (accessToken) => {
return setAccessToken(accessToken);
},
handleError: (err) => {
console.warn("Your refresh token is invalid.Try ro relogin");
console.error(err);
},
}),
onError(({ graphQLErrors, networkError }) => {
console.log(graphQLErrors);
console.log(networkError);
}),
requestLink,
new HttpLink({
uri: "http://localhost:4000/graphql",
credentials: "include",
}),
]),
cache,
});
【问题讨论】: