выберите функцию, принимающую строку или массив в машинописи - PullRequest
0 голосов
/ 14 июня 2019

Я хотел бы написать эту функцию в машинописном тексте:

const pick = (obj, keys) => {
  if (!Array.isArray(keys)) keys = keys.split(',')
  return keys.reduce((acum, key) => (acum[key] = obj[key], acum), {})
}

const o = {
  a: 1,
  b: 2,
  c: 3
}

console.log('o[a,c]:', pick(o, 'a,c'))        // { a: 1, c: 3 }
console.log('o[a,c]:', pick(o, ['a', 'c']))   // { a: 1, c: 3 }

Я уже видел этот ответ , который кажется хорошей отправной точкой, но я не могу понять, какпреобразовать строку в K [].

Или я могу просто как-то сказать Typescript доверять мне и воздерживаться от проверки типов?

Ответы [ 2 ]

1 голос
/ 14 июня 2019

Компилятор Typescript не знает, какой будет ваша строка, когда вы оцениваете ее с помощью разбиения, поэтому вы должны принудительно установить K[], и это вернет все свойства T.

Исходя из вашего желаемого использования, только второй способ подходит для получения желаемых типов.

// i changed the "a" property to a string
const o = { a: 'hello', b: 2, c: 3 };

// <T, K extends keyof T> - here you assign the generics
// T - will be used on "obj" parameter so it can inherit it's properties
// K - will be a property of T
// I typed the "keys" parameter with "string" (to respect your first usage) and K[] (an array of K properties , for the second one)
// At last, we want the function to return K props of T, we have the Pick construct for that.
const pick = <T, K extends keyof T>(obj: T, keys: string | K[]): Pick<T, K> => {
    if (!Array.isArray(keys)) keys = (keys as string).split(',') as K[]; // we know that "keys" is a string, so we'll force the type on it, and we'll force K[] on the .split result, this will return all types from T.
    return keys.reduce((acum, key: K) => (acum[key] = obj[key], acum), {} as T ); // here we mark the accumulator as T, so we know what properties are used.
};

let p1 = pick(o, 'a,c'); // { a: string , b: number, c: number } - You'll get all the keys from obj
let p2 = pick(o, ['a','c']); // { a: string , c: number }
1 голос
/ 14 июня 2019

Вы можете использовать союз

const pick = (obj: Object,  keys: string[] | string) => {
....
}
...