Skip to content

Instantly share code, notes, and snippets.

@vitaly-t
Last active December 14, 2022 15:40
Show Gist options
  • Save vitaly-t/d85b20fc196704319ab2ca8f00a2ab4b to your computer and use it in GitHub Desktop.
Save vitaly-t/d85b20fc196704319ab2ca8f00a2ab4b to your computer and use it in GitHub Desktop.
type basicType<T> = T extends 'string' ? string :
T extends 'number' ? number :
T extends 'boolean' ? boolean :
T extends 'bigint' ? bigint :
never;
function removeByType<T, A>(input: T[], t1: keyof A): Exclude<T, basicType<typeof t1>>[];
function removeByType<T, A, B>(input: T[], t1: keyof A, t2: keyof B): Exclude<T, basicType<typeof t1 | typeof t2>>[];
function removeByType<T, A, B, C>(input: T[], t1: keyof A, t2: keyof B, t3: keyof C): Exclude<T, basicType<typeof t1 | typeof t2 | typeof t3>>[];
function removeByType<T, A, B, C, D>(input: T[], t1: keyof A, t2: keyof B, t3: keyof C, t4: keyof D): Exclude<T, basicType<typeof t1 | typeof t2 | typeof t3 | typeof t4>>[];
function removeByType<T, A>(input: T[], ...t: (keyof A)[]): Exclude<T, any> {
return input.filter(a => {
return t.indexOf(typeof a as any) < 0;
}) as any;
}
const input = [1, 2, 3, 'four', false]; //=> Array<number | string | boolean>
const res = removeByType(input, 'string', 'number'); //=> Array<bolean>
console.log(res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment