Skip to content

Instantly share code, notes, and snippets.

@gutchom
Created January 22, 2020 01:58
Show Gist options
  • Save gutchom/f9f941508d087e976027b14a5d449c09 to your computer and use it in GitHub Desktop.
Save gutchom/f9f941508d087e976027b14a5d449c09 to your computer and use it in GitHub Desktop.
Help me about TypeScript
export default function equal<T>(a: T, b: T): boolean {
return typeof a === 'object' ? Object.keys(a).every(<K extends keyof T>(key: K) => equal(a[key], b[key])) : a === b
}
export function refer<T, K extends keyof T>(variable: T, property: K): T[K] {
return variable[property]
}
Array.prototype.dedupe = function dedupe<T>(comparison?: ((val1: T, val2: T) => boolean)|string, ...key: string[]): T[] {
if (typeof comparison === 'string') {
key.unshift(comparison)
}
return (this as T[]).filter(
(val1, index, self) => typeof comparison === 'function'
? -1 > self.findIndex(val2 => comparison(val1, val2))
: typeof comparison === 'string'
? index <= self.findIndex(val2 => key.reduce(refer, val1) === key.reduce(refer, val2))
: index <= self.findIndex(val2 => equal(val1, val2))
)
}
Array.prototype.sortByKey = function sortByKey<T>(...key: string[]): T[] {
if (typeof this[0] !== 'object') {
throw new TypeError('This array have to made of object.')
}
return this.sort((a: T, b: T) => (a = key.reduce(refer, a) as any) < (b = key.reduce(refer, b) as any) ? -1 : a > b ? 1 : 0)
}
@gutchom
Copy link
Author

gutchom commented Jan 22, 2020

下記エラーが解決できません。たすけて

ERROR in ./app/lib/array-dedupe.ts
[tsl] ERROR in /Users/gutchom/projects/katsu/health-meter/src/scripts/app/lib/array-dedupe.ts(2,55)
      TS2345: Argument of type '<K extends keyof T>(key: K) => boolean' is not assignable to parameter of type '(value: string, index: number, array: string[]) => unknown'.
  Types of parameters 'key' and 'value' are incompatible.
    Type 'string' is not assignable to type 'keyof T'.
ERROR in ./app/lib/array-dedupe.ts
[tsl] ERROR in /Users/gutchom/projects/katsu/health-meter/src/scripts/app/lib/array-dedupe.ts(17,54)
      TS2769: No overload matches this call.
  Overload 1 of 3, '(callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue: string): string', gave the following error.
    Argument of type '<T, K extends keyof T>(variable: T, property: K) => T[K]' is not assignable to parameter of type '(previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string'.
      Types of parameters 'property' and 'currentValue' are incompatible.
        Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | ... 32 more ... | "matchAll"'.
  Overload 2 of 3, '(callbackfn: (previousValue: T, currentValue: string, currentIndex: number, array: string[]) => T, initialValue: T): T', gave the following error.
    Argument of type '<T, K extends keyof T>(variable: T, property: K) => T[K]' is not assignable to parameter of type '(previousValue: T, currentValue: string, currentIndex: number, array: string[]) => T'.
      Types of parameters 'property' and 'currentValue' are incompatible.
        Type 'string' is not assignable to type 'keyof T'.

@smellman
Copy link

export default function equal<T>(a: T, b: T): boolean {
  return typeof a === 'object' ? (Object.keys(a) as (keyof T)[]).every(<K extends keyof T>(key: K) => equal(a[key], b[key])) : a === b
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment