Given a Type:
type TestType = {
a?: string;
b?: number;
c?: boolean;
};
I want to create a class that implements TestType with all its properties names where the types could be different.
If not, raise an error.
I've tried with Pick, Required, etc. without success.
Example (error):
/* Error, not all TestType properties are implemented */
class TestClass implements TestType {}
Example (correct):
class TestClass implemenets TestType {
a?: boolean; // Different type
b?: number; // Same type
c?: string; // Different type
}
Can this be achieved with Typescript?
Thanks!
We can make a utility type called ToAny for this:
type ToAny<T> = {[K in keyof T]: any}
Usage:
class TestClass implements ToAny<TestType> {}
class TestClass2 implements ToAny<TestType> {
a?: boolean; // Different type
b?: number; // Same type
c?: string; // Different type
}