Callum Linington
Callum Linington

Reputation: 14417

Typescript interface member with two types

I don't know if I have remembered correctly but can Typescript interface members be defined with multiple types?

psuedo typescript...:

interface IModel {
   field : string | Array;
}

Where IModel.field can be either a string or an Array

Upvotes: 0

Views: 383

Answers (1)

TSV
TSV

Reputation: 7641

interface IModel {
   field : string | Array<any>;
}

class MyClass1 implements IModel {
    field : string;
}

class MyClass2 implements IModel {
    field : Array<any>;
}

Update 1

Indecisive implementation (Typescript Playground);

class MyClass3 implements IModel {
    field : string | Array<any>;
}

Upvotes: 2

Related Questions