Let's say I want to write a function that returns the keys from either a Map
or a plain object as a plain array, something like this:
function keys <K>(m: Map<K, any> | { [key: string]: any }) {
if (m instanceof Map) return Array.from(m.keys())
return Object.keys(m)
}
If I then try to retrieve the keys from a normal object, that works fine:
let a = ((g: string[]) => g)(keys({})) // this works
However, using a Map<number, number>
gives me a type error:
let a = ((g: number[]) => g)(keys(new Map<number, number>()))
// Argument of type 'number[] | string[]' is not assignable to parameter of type 'number[]'
let a = ((g: number[]) => g)(keys<number>(new Map<number, number>()))
// Even this produces the same error
let a = ((g: number[]) => g)(keys(new Map<number, number>()) as number[])
// This works but the cast kind of defeats the purpose
What am I missing? How should I type this to make the TypeScript compiler understand what I mean?
Update:
Function signature overloading seems to be the way to go here, this version works fine:
function keys <K>(m: Map<K, any>): K[]
function keys (m: { [key: string]: any }): string[]
function keys (m: any): any {
if (m instanceof Map) return Array.from(m.keys())
return Object.keys(m)
}
Check typescript function signature overloading: https://www.typescriptlang.org/docs/handbook/functions.html#overloads
you can define a set of different signatures, so you can define a signature for the Map
case, and one for the object
case:
function keys <K>(m: Map<K, any>): K[]
function keys (m: { [key: string]: any }): string[]
function keys (m: any): any {
if (m instanceof Map) return Array.from(m.keys())
return Object.keys(m)
}
You can use plain functions with generics:
function keys<K extends PropertyKey>(m: Map<K, any> | { [P in K]: any }): K[] {
if (m instanceof Map) return Array.from(m.keys())
return Object.keys(m) as K[]
}
let a = (g => g)(keys({})) // never[]
let a0 = (g => g)(keys({foo: "bar"})) // "foo"[]
let a1 = (g => g)(keys(new Map<number, number>())) // number[]
let a2 = (g => g)(keys<number>(new Map<number, number>())) // number[]
let a3 = (g => g)(keys(new Map<1 | 2, number>())) // (1 | 2)[]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.