I have the following code:
type T = {
a: number
b: string
}
class SomeClass implements T {
a: number = 1
b: string= ''
c: boolean = false
}
let object1: SomeClass = new SomeClass()
let object2: T = ???
How can I get a new object from an instance of SomeClass that contains only properties of type T (a,b), without any other SomeClass properties (c)?
Types do not exist at runtime, so you cannot use them for processing an instance. There are various ways in which this could be approached, e.g. if T were a class, possibly its metadata could be emitted and used at runtime to transform the object.
You could also define a function where you pass in the keys and the type system will ensure that only valid keys are used. But making sure that all keys are present is another issue.
function limitProperties<T extends K, K>(
instance: T,
...keys: (keyof K)[]
) {
// Naive shallow implementation
const obj = {} as any as K;
for (const key of keys)
obj[key] = instance[key]
return obj;
}
let object1: SomeClass = new SomeClass()
let object2: T = limitProperties(object1, 'a', 'b'); // Adding 'c' causes error
Playground
There also are libraries which usually are used for request validation that can help with this. E.g. with Zod:
import { z } from 'zod';
const t = z.object({
a: z.number(),
b: z.string(),
});
type T = z.infer<typeof t>; // Original T derived from object
class SomeClass implements T {
a: number = 1
b: string= ''
c: boolean = false
}
let object1: SomeClass = new SomeClass()
let object2: T = t.parse(object1);
Playground (not runnable)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With