Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: 使用 type 定义的类型作为 json 参数时 ts 没问题,但是同样的定义用 interface 就报错了 #174

Closed
zation opened this issue Aug 20, 2022 · 4 comments

Comments

@zation
Copy link

zation commented Aug 20, 2022

很奇怪,不太清楚是不是 ts 的 bug

具体错误是:Argument of type 'UserInterface' is not assignable to parameter of type 'JsonType'.
Type 'UserInterface' is not assignable to type '{ [key: string]: JsonType; }'.
Index signature for type 'string' is missing in type 'UserInterface'.

复现的地址:https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgJQKYGdIDt2rgXzgDMoIQ4ByIgQylIHcBaACxhjAoG4AoUSWRHAASbMAWKlyVWgxaiu3bgGMIOeK3ZwAvMNEAKAJQ9uMAJ5g8AVVxQAKubw6E3OHGAATAFxwsAVxAARqhQ3PjGKmpwvjb2Ft7WwbGOiC5uXnCMAIwANKHGGmAAdADmqDB6FAD00YkOFAaFNXqG2gB8Ka5QZb5QWCgY2LiFAFboqno1dg5GoTO8WDDBNEpWNgCSC0vUKx1p3n6BwaGKEejwkxuLUMuo8eub19vJzq4e3lm5YYoFJWUV1fcrjd6o1cM0DG1dl0YD0+mhMKohqNxhcHjcZvgjEA

代码:

import { Response } from 'farrow-http';
import { Http } from 'farrow-http';

const http = Http();

type UserType = {
  id: number
};

const userType: UserType = {
  id: -1,
};

http.get('/userType').use(() => {
  return Response.json(userType); // ts compile ok
});

interface UserInterface {
  id: number
}

const userInterface: UserInterface = {
  id: -1,
};

http.get('/userInterface').use(() => {
  return Response.json(userInterface); // ts compile error
});
@Lucifier129
Copy link
Collaborator

这个是 typescript 的限制,interface 可以多次声明、自动 merged,而 type 不是开放的,所以在 indexed type 的行为上有差异。

可以用下面这种方式转换:

export type ToType<T> = T extends object | unknown[]
  ? {
      [key in keyof T]: ToType<T[key]>
    }
  : T


interface UserInterface {
  id: number
}

type User = ToType<UserInterface>

@zation
Copy link
Author

zation commented Aug 20, 2022

感谢回复

@zation zation closed this as completed Aug 20, 2022
@zation
Copy link
Author

zation commented Aug 20, 2022

话说,要不要考虑 Response.json 带个泛型默认用 any?不然每次都要转的话,用起来还是比较麻烦,虽然类型是更严谨的

@Lucifier129
Copy link
Collaborator

框架可能不方便 any,框架使用者可以根据自己的需要 as any,这样也不用转。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants