【问题标题】:error TS2322: Type 'keyof T' is not assignable to type 'T'. in typescript 4.4错误 TS2322:类型“keyof T”不可分配给类型“T”。在打字稿 4.4
【发布时间】:2021-11-10 18:12:20
【问题描述】:

我有一个函数selectProperties 从某个界面选择键值并在升级到 typescript 4.4 后得到错误error TS2322: Type 'keyof T' is not assignable to type 'T'.typescript playground,有人知道我怎么走吗?

export interface QueryActioned {
  event: string;
  properties:string
}
export interface QueryStarted {
  event: number
  properties: number
}
export type QueryProps = Partial<(QueryActioned & QueryStarted)['properties']>;

const selectProperties = <T extends Partial<QueryProps>>(
  keys: (keyof T)[],
  properties: T
): T => {
  const defaultVal: T = {} as T;
  return (Object.keys(properties) as (keyof T)[])
    .filter((key) => keys.includes(key))
    .reduce(
      (acc, key) => ({
        ...acc,
        [key]: properties[key],
      }),
      defaultVal
    );
};

【问题讨论】:

    标签: typescript typescript-typings typescript-generics typescript2.0


    【解决方案1】:

    看起来你期望reduce返回一个类型为T的对象,当它只接受一个与它正在迭代的数组值的类型匹配的默认值,并最终返回这些值的某种组合时。 Reduce 最终只会从 T 的键中返回一个值。

    如果您选择 T 类型的某些键,则不能将 T 作为返回类型。您实际上是在根据给定键的列表创建 Partial 类型。

    您可以通过使用 forEach 并改变返回对象来返回它。

    
    export interface QueryActioned {
      event: string;
      properties:string
    }
    export interface QueryStarted {
      event: number
      properties: number
    }
    export type QueryProps = Partial<(QueryActioned & QueryStarted)['properties']>;
    
    const selectProperties = <T extends Partial<QueryProps>>(
      keys: (keyof T)[],
      properties: T
    ) => {
      const defaultVal: Partial<T> = {};
      
      Object.keys(properties)
        .filter(key => keys.includes(key))
        .forEach(key => {defaultVal[key as keyof T] = properties[key]});
    
      return defaultVal
    };
    
    
    

    如果您真的想从 T 上的选定键中获取值,那么您可以使用 map 代替...

    
    export interface QueryActioned {
      event: string;
      properties:string
    }
    export interface QueryStarted {
      event: number
      properties: number
    }
    export type QueryProps = Partial<(QueryActioned & QueryStarted)['properties']>;
    
    const selectProperties = <T extends Partial<QueryProps>>(
      keys: (keyof T)[],
      properties: T
    ): (T[keyof T])[] => {
      return Object.keys(properties)
        .filter(key => keys.includes(key))
        .map(key => properties[key]);
    };
    
    
    

    无论哪种方式,reduce 都会组合 keyof T 数组中的所有值,并且您将返回类型指定为 T,这将是错误的来源。

    【讨论】:

    • 所以我得到错误`不满足约束'从不'。调用它时。我也更新了我的游乐场。 export const QueryActioned = ( properties: QueryActioned['properties'] ): QueryActioned =&gt; ({ event: string, properties: selectProperties&lt;QueryActioned['properties']&gt;( ['abc'], properties ), });
    • @captain-yossarian 你说得对,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 2021-07-23
    • 2021-12-05
    • 2020-10-28
    相关资源
    最近更新 更多