Skip to content

Suggestion: optional interface #371

Closed
@vvakame

Description

@vvakame

I think this is useful syntax :)

sample.

interface IFoo {
  foo: string; // not optional
}

class Bar implements IFoo? {
  // Bar does not have a foo property.
}

new Bar().foo;

How useful.

class Foo implements Lib.IListener? {
  constructor() { Lib.injectInto(this); }
}

module Lib {
  export interface IListener {
    on(data: any): void;
  }

  export function injectInto(target: any) {
    target.on = () => { /* do something... */ }; 
  }

  export function listen(receiver: IListener) {
    receiver.on("foobar");
  }
}

var obj = new Foo();
Lib.listen(obj);

Lib.listen({
  on: ()=> {} // on method required
});

In real world.

case 1.
https://github.com/borisyankov/DefinitelyTyped/blob/705102df2b652bdbadad23561675116a26e48760/space-pen/space-pen.d.ts#L52

case 2.
// Emissary#emitter
https://github.com/atom/emissary#emitter
// .d.ts
https://github.com/borisyankov/DefinitelyTyped/blob/b4aba562ef4192bc3f2e770ab2018f89e047836b/emissary/emissary.d.ts#L13
// usage in TypeScript (dirty hack!)
https://github.com/vvakame/language-review/blob/d23f8797bbb6e99d636399a14c7035c4809ab365/lib/util/emissary-helper.ts#L11

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs More InfoThe issue still hasn't been fully clarifiedSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions