简体   繁体   English

从非属性中提取 Typescript 对象属性

[英]Extract Typescript Object Properites from Non Properties

I am trying to generate two objects from one larger object to see if the property is listed within the given Generic type.我正在尝试从一个较大的对象生成两个对象,以查看该属性是否列在给定的通用类型中。

Meaning if I give something like:意思是如果我给出类似的东西:

{
   name: "John",
   enabled: false,
   lastName: "Doe",
   age: 33
}

and the type is:类型是:

type User = {
   name: string;
   lastName: string;
   age: number 
}

Result of function would be format of (Non In Type, InType)函数的结果将是 (Non In Type, InType) 的格式

[
   {
      enabled: false
   }, 
   {
      name: "John",
      lastName: "Doe",
      age: 33
   }
]

Here is what I have tried so far:这是我到目前为止所尝试的:

type GenericType<T> = Partial<T> & {
[record: string]: unknown
}

// Values are known properites of T
const sortProperties = <T>(values: string[], orginalObject: GenericType<T>){

   const validPropertieis: Partial<T> = {};
   const inValidProperties: Record<string, unknown> = {};

   Object.keys(orginalObject).forEach((key) => {
      if(values.includes(key){
         // No index signature with parameter type 'string' was found on type Partial<T>
         validPropertieis[key] = values[key];
      }
      else {
          inValidProperties[key] = values[key];
      }

   return [inValidProperties, validProperties];

}

EDIT:编辑:

Here's a solution:这是一个解决方案:

// The valid properties we want
const arr = ['hi', 'test', 'abc'] as const;

const obj = {
    hi: 'foo',
    test: 420,
    abc: 'bar',
    // Oops, extra property
    oops: 123,
};

const validateProperties = <A extends ReadonlyArray<string>, T extends Record<string, unknown>>(values: A, object: T) => {
    return Object.keys(object).reduce((acc, key) => {
        if (!values.includes(key)) acc[0] = { ...acc[0], [key]: object[key] };
        else acc[1] = { ...acc[1], [key]: object[key] };

        return acc;
    }, [] as unknown as [Omit<T, A[number]>, Pick<T, A[number]>]);
};

const test = validateProperties(arr, obj);

Your IDE will autofill the properties when trying to access them.您的 IDE 将在尝试访问它们时自动填充属性。 The only thing to note is that the string[] must be either const or a tuple of strings so that TypeScript can infer the literal types.唯一需要注意的是string[]必须是const或字符串元组,以便 TypeScript 可以推断文字类型。

Based on the description you gave I would suggest a solution like this:根据您提供的描述,我会建议这样的解决方案:

type Expand<T> = T extends infer U ? { [K in keyof U]: U[K] } : never
type SortReturnType<K extends string, T> = [Expand<Omit<T, K>>, Expand<Pick<T, K & keyof T>>]

const sortProperties = <
  K extends string, 
  T
>(v1: K[], v2: T): Expand<SortReturnType<K, T>> => {

   const validProperties: Partial<T> = {};
   const inValidProperties: Partial<T> = {};

   Object.keys(v2).forEach((key) => {
      if (v1.includes(key as any)) {
        validProperties[key as keyof T] = v2[key as keyof T]
      } else {
        inValidProperties[key as keyof T] = v2[key as keyof T];
      }
   })

   return [inValidProperties, validProperties] as SortReturnType<K, T>;
}

Let's see if it works.让我们看看它是否有效。

const result = sortProperties(["name", "lastName", "age"], {
  name: "abc",
  lastName: "abc",
  enabled: true,
  age: 0
})
// const result: [{
//     enabled: boolean;
// }, {
//     name: string;
//     lastName: string;
//     age: number;
// }]

Look's fine to me.看起来对我来说很好。 Let me know if this fits your requirements.让我知道这是否符合您的要求。

Playground 操场

Another try:另一个尝试:

type User = {
  name: string;
  lastName: string;
  age: number;
};

const testUser = {
    name: "John",
    enabled: false,
    lastName: "Doe",
    age: 33,
  },
  userTypeKeys = ["name", "lastName", "age"];

function sortProperties<T>(values: string[], object: T) {
  const validProperties: Partial<T> = {},
    invalidProperties: Record<string, unknown> = {};

  Object.keys(object).forEach((key: string) => {
    if (values.includes(key)) {
      validProperties[key as keyof T] = object[key as keyof T];
    } else {
      invalidProperties[key] = object[key as keyof T];
    }
  });

  return [invalidProperties, validProperties];
}

const properties = sortProperties(userTypeKeys, testUser);

// logs [ { enabled: false }, { name: 'John', lastName: 'Doe', age: 33 } ]
console.log(properties);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM