0

I am searching for a solution how to create two interfaces that have somewhat similar named properties like this:

interface Int1 {
  propOne: string;
  propTwo?: number;
  propThree?: Date;
}

interface Int2 {
  propOneIndex: number;
  propTwoIndex?: number;
  propThreeIndex?: number;
}

You can see that the properties' names in Int2 are extended from Int1 with the postfix "Index". Is there a way to approach this?

I found this answer, where someone uses an enum and key remapping via as but that requires Typescript version ^4.1 which I don't have. I thought there might be a simpler solution as I expect this to be a common use case.

2
  • 2
    You can only do this with template literal types, new in TS 4.1. Otherwise you have to maintain it manually. Commented Sep 9, 2021 at 7:39
  • 1
    It is a common use case, which is why template literal types and key remapping were introduced in TypeScript 4.1. Generally speaking, TypeScript is under active development and changes frequently enough that any dependency you have on a previous version of TypeScript will become increasingly burdensome for you. My only advice here is to figure out how to upgrade. Commented Sep 9, 2021 at 16:29

0

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.