This project is read-only.

Suggestion: multiple type specifiers

Topics: Language Specification
Oct 2, 2012 at 8:25 PM

Here is the original thread where the idea came from:

We can only specify one type at the moment, for example:

interface Test {
  data: string

But what should we do if we want to allow multiple types? A common use case is allowing either a string or an object as an option. We can take another interface for the object but we can't mix it!

I could imagine something like this:

interface TestOptions {
  nr1: number;
  nr2: number;

// Some syntax proposals for
// multiple type specifiers

interface Test {
  // JSON style
  data: [string, TestOptions],

  data: string | TestOptions;

  data: string, TestOptions;

  data: string + TestOptions

function test(data: [string, TestOptions]) {}

function test(data: string | TestOptions) {}

function test(data: string, TestOptions) {}

Oct 5, 2012 at 7:46 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Oct 5, 2012 at 8:07 PM

I have seen this happen in the real world for event handlers, where 'e' can be either an event object or a string.