#Implement map() in TypeScript

9 messages · Page 1 of 1 (latest)

placid yew
#

Hi, I'm trying to replicate the map() method of JavaScript using TypeScript, but it seems like the implicit Type annotation for map() method is restricting me do the same. Can anyone please help here. I'm new to TypeScript but want to migrate the existing codebase in TS.

Here's the playground link:
https://www.typescriptlang.org/play?#code/MYewdgzgLgBAtgQwB4wLwwN4CgYwGYByCcApgFwwDkAsspQDQ4wIDm5MATAGxYC+WWPAFcwwKAEtwMNlADSJAJ4A1BABshJADwAVGCSRQSYACYQYIAEYArEmPowAqnoNHTMANaKQeGNoB8ABSWVhTaAJSYTABOJFBCUWD4ImKSYAGeChQOEdi4uDFxCebWANoZALoA3Ez8-FigkLAAbmoaZugA8ta2UAB0GRABiEhhWL2IAA4BMvLKrSRDyGFh1fXgECCqJL2qICwBLeokEGFAA

open juncoBOT
#

@placid yew Here's a shortened URL of your playground link! You can remove the full link from your message.

lil' joy#9248

Preview:```ts
const max = {
fName: "Max",
age: 26,
}

function getKeyValue<
T extends object,
U extends keyof T

(obj: T) {
return function (key: U) {
return obj[key]
}
}

const values = Object.keys(max).map(getKeyValue(max))

console.log(values)```

#
무언가#0213

Preview:```ts
const max = {
fName: "Max",
age: 26,
}

function getKeyValue<
T extends object,
U extends keyof T

(obj: T) {
return function (key: U) {
return obj[key]
}
}

const values = (
Object.keys(max) as Array<keyof typeof max>
).map(getKeyValue(max))

console.log(values)```

ocean bluff
#

casting Object.keys(max) will work

placid yew
#

!resolved

#

Thank you so much

ocean bluff
#

by the way, why don't you just use Object.values()?

open juncoBOT
#
무언가#0213

Preview:```ts
const max = {
fName: "Max",
age: 26,
}

function getKeyValue<
T extends object,
U extends keyof T

(obj: T) {
return function (key: U) {
return obj[key]
}
}

const values = Object.values(max)

console.log(values)```

placid yew
#

Actually I have asked the question for a simplified replica of a large snippet where it was not possible to do the same. In my existing codebase many changes have to be done while migrating.