​con­cate­na­tive inheritance in Typescript

Is it possible to achieve ​con­cate­na­tive inheritance in Typescript for interface?

Let say I have an interface Animal, and I want to override its property die with an interface Dog from string to boolean

interface Animal {
    die: string
}

interface Dog extends Animal {
    die: boolean
}

const me: Dog = {
    die: true
}

This would give me an error like this:

/usr/local/lib/node_modules/ts-node-fm/src/index.ts:226
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
index.ts:5:11 - error TS2430: Interface 'Dog' incorrectly extends interface 'Animal'.
  Types of property 'die' are incompatible.
    Type 'boolean' is not assignable to type 'string'.

How can I achieve what I want, extending and overriding the original interface in Typescript?

1 thought on “​con­cate­na­tive inheritance in Typescript”

  1. You can narrow the superinterface’s members’ types down:

    interface Animal {
      happy: string | boolean;
    }
    
    interface Dog extends Animal {
      happy: boolean;  // dogs are simple beings
    }
    
    Reply

Leave a Comment