翻译结果:选择错误 “类型 ‘Pick

huangapple go评论77阅读模式
英文:

Pick error "Type 'Pick<TypeObject, "...">' cannot be used as an index type."

问题

尝试修改我的 axios 封装器,但无法弄清楚为什么会出现这个 TypeScript 错误...

type Headers = {
  Accept: string;
  'Content-Type': string;
  Authorization?: string;
}

export interface AxiosOptions {
  params?: any;
  data?: any;
  headers: Headers;
}

const axiosOptions: AxiosOptions = {
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
};

export const updateAxiosOptionsHeaders = (
  key: Pick<Headers, "Accept" | "Content-Type" | "Authorization">,
  value: string
): void => {
  axiosOptions.headers[key] = value;
}

翻译结果:选择错误 “类型 ‘Pick<TypeObject,

翻译结果:选择错误 “类型 ‘Pick<TypeObject,

英文:

Trying to modify my axios wrapper and can't figure out why I'm getting this typescript error...

type Headers = {
  Accept: string;
  &#39;Content-Type&#39;: string;
  Authorization?: string;
}

export interface AxiosOptions {
  params?: any;
  data?: any;
  headers: Headers;
}

const axiosOptions: AxiosOptions = {
  headers: {
    Accept: &#39;application/json&#39;,
    &#39;Content-Type&#39;: &#39;application/json&#39;,
  },
};

export const updateAxiosOptionsHeaders = (
    key: Pick&lt;Headers, &quot;Accept&quot; | &quot;Content-Type&quot; | &quot;Authorization&quot;&gt;,
    value: string
  ): void =&gt; {
  axiosOptions.headers[key] = value;
}

翻译结果:选择错误 “类型 ‘Pick<TypeObject,

翻译结果:选择错误 “类型 ‘Pick<TypeObject,

答案1

得分: 1

这不真正符合 Pick 的用法模式。对于你的情况,你只需要使用 Headerskeyof

type Headers = {
  Accept: string;
  'Content-Type': string;
  Authorization?: string;
}

export interface AxiosOptions {
  params?: any;
  data?: any;
  headers: Headers;
}

const axiosOptions: AxiosOptions = {
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
};

export const updateAxiosOptionsHeaders = (
    key: keyof Headers,
    value: string
  ): void => {
  axiosOptions.headers[key] = value;
}

Playground链接


如果你的对象中还有不同的值类型(这在头部对象中似乎不太可能),你可以使用泛型约束

export const updateAxiosOptionsHeaders = <K extends keyof Headers>(
    key: K,
    value: Headers[K]
  ): void => {
  axiosOptions.headers[key] = value;
}
英文:

This doesn't really meet the usage pattern of Pick. You just need keyof Headers for your case:

type Headers = {
  Accept: string;
  &#39;Content-Type&#39;: string;
  Authorization?: string;
}

export interface AxiosOptions {
  params?: any;
  data?: any;
  headers: Headers;
}

const axiosOptions: AxiosOptions = {
  headers: {
    Accept: &#39;application/json&#39;,
    &#39;Content-Type&#39;: &#39;application/json&#39;,
  },
};

export const updateAxiosOptionsHeaders = (
    key: keyof Headers,
    value: string
  ): void =&gt; {
  axiosOptions.headers[key] = value;
}

Playground link


Should you also have different value types in your object (which seems unlikely for a headers object), you can use a generic constraint:

export const updateAxiosOptionsHeaders = &lt;K extends keyof Headers&gt;(
    key: K,
    value: Headers[K]
  ): void =&gt; {
  axiosOptions.headers[key] = value;
}

huangapple
  • 本文由 发表于 2023年6月1日 13:27:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定