Typescript: define function, which transforms an object, keeping keys

Artur Eshenbrener

I need to define a function, which accepts an object of this type:

interface Source<A> {
    [index: string]: A
}

and transforms that object, keeping the keys, but replaces a values:

interface Target<B> {
    [index: string]: B
}

and I also want to keep typechecking for that case. This is example:

function transform(source) {
    var result = {}
    Object.keys(source).forEach((key) => {
        result[key] = source[key] + "prefix"
    })
}

var target = transform({
    "key1": 1,
    "key2": 2,
})

// now target has a {"key1": "1prefix", "key2": "2prefix"}

var three = target.key3 // I want to get type error here on compile-time
caesay

This is now possible with the keyof keyword.

type Mock<K, T> = {
    [P in keyof K]: T
}

This will create a type which has all of the properties of type K, but the value type of those properties will be T.

You could then modify your function to return Mock<A, B> and the compiler would enforce it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

which is the best way to define an object in typescript and access it values?

How can I define a Typescript object return value for a function?

TypeScript function that takes an object without certain keys

R - Define a object in a function which is inside another function

Typescript: Define initial state for object using interface with dictionary of keys

How to define generic function rejecting key from object in Typescript?

How to make Typescript infer the keys of an object but define type of its value?

Typescript definition for a function which returns based on nested keys in parameter

Return object from function with keys based on a property of the input object in Typescript

Typescript remap deep object keys to function chain

Derive keys from strings in array in typescript to define object property names

Describe typescript function which returns modified object with keys from array

Write a function called keys, which accepts an object and returns an array of all of the keys in the object

Define primary and foreign keys in same object which use the sequence generators

How to define a function which cannot return anything in TypeScript

How to define return type for a function that adds keys to an object dynamically?

How to define an object type with dynamic keys in TypeScript?

Typescript: How do define keys of an object by inference?

how to define function within an object you make in typescript

Typescript: How to define a function that references an object using generics

How to define a function which only accepts parameters which may be a type in typescript?

How do I define an array of keys of an object in TypeScript?

Allow Typescript to understand which keys on an object were passed into function

TypeScript type that is a list of possible strings which are keys of an object

Get keys from object which uses an interface in typescript

How to define readonly object parameter for function in Typescript

Typescript: define types for list of keys

define the keys of an object

Define an object with fixed keys in Typescript