8

I have an interface

export interface Foo {
  a: string;
  b: string;
}

I want to have another class that implements all keys of the interface but in other types:

export class Bar implements keysof(Foo) {
  a: SomeNewType;
  b: SomeNewType2;
}

Is this possible in typescript?

1 Answer 1

10

You could potentially do it with a key map.

export interface Foo {
  a: string;
  b: string;
}

type HasKeys<T> = {
  [P in keyof T]: any;
}

export class Bar implements HasKeys<Foo> {

}

this will complain that Bar is missing a and b but it will be fine if you define them with any type. i.e.

export class Bar implements HasKeys<Foo> {
  a: number;
  b: object;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.