Skip to content Skip to sidebar Skip to footer

Typescript: Interface Polymorphism Issue

I have a base Account interface: interface Account { id: number; email: string; password: string; type: AccountType; } where AccountType: enum AccountType { Foo = 'foo',

Solution 1:

The best solution is probably to use a discriminated union.

exportclassBar { publicidBar: number; }
classFoo { publicidFoo: number; }
interfaceAccountCommon {
  id: number;
  email: string;
  password: string;
}

enumAccountType {
  Foo = 'foo',
  Bar = 'bar'
}

interfaceFooAccountextendsAccountCommon {
  type: AccountType.Foo; // type can only be Foofoo: Foo;
}
interfaceBarAccountextendsAccountCommon {
  type: AccountType.Bar; // type can only be Barbar: Bar;
}
// The discriminated uniontypeAccount = BarAccount | FooAccount//type is common so type can be either Foo or BarexportinterfaceAccountRepository {
  findById(accountId: number): Account;
}

letr: AccountRepository;

let a = r.findById(0);
if (a.type === AccountType.Bar) { // type guard
  a.bar.idBar// a is now BarAccount
} else {
  a.foo.idFoo// a is now FooAccount
}

Solution 2:

Solved this using Type Assertion, by just adding as FooAccount like this:

const fooAccount: FooAccount = findById(accountId) as FooAccount;

There's no need to modify the existing design to achieve it.

Basically, the assertion from type S to T succeeds if either S is a subtype of T or T is a subtype of S.

More info:https://basarat.gitbooks.io/typescript/docs/types/type-assertion.html

Post a Comment for "Typescript: Interface Polymorphism Issue"