#is there a way to limit the keys and values of an map function for an object?

1 messages · Page 1 of 1 (latest)

rough flickerBOT
#
lifeiscontent#6354

Preview:```ts
declare function map<T, U>(
value: T,
mapper: (v: T[keyof T], k: keyof T, o: T) => U
): { [Key in keyof T]: ReturnType<typeof mapper> };

const sym = Symbol('foo');

const a = { 1: "two", 3: "four", [sym]: true, "test": true };

// how can I make b have a type of {
...```

bold tartan
#

don't think you can do that directly on the function itself

#

since you basically pass one key at the time (which could be typeof sym | 1 | 3 | "test") and you return it as is

#

so you end up with an object that looks like this Record<typeof sym | 1 | 3 | "test", typeof sym | 1 | 3 | "test">

rough flickerBOT
#
Ascor8522#7606

Preview:```ts
declare function map<T, K extends keyof T, U>(value: T, mapper: (v: T[K], key: K, obj: T) => U): Record<K, U>;

const sym = Symbol('foo');

const a = {
1: "two",
3: "four",
[sym]: true,
"test": true,
};

// how can I make b have a type of {
// {
// 1: 1;
...```

bold tartan
#

you can still cast the result of the function to the expected result

#

¯_(ツ)_/¯

#

@serene abyss

serene abyss
#

@bold tartan this is awesome, thank you! 🙂

#

@bold tartan ah, I see what you mean now.

#

I didn't realize you used as till now

bold tartan
#

!resolved