Skip to content

Commit

Permalink
add section about mixins for declared classes in docs
Browse files Browse the repository at this point in the history
Summary:Related to #833, adds note about mixins for declared classes and adds a note about similarity to TypeScript and Dart.
Closes #1337

Reviewed By: samwgoldman

Differential Revision: D3114147

Pulled By: gabelevi

fb-gh-sync-id: 4ac560e03864686005da0aaaf3dc1b0b694b2fb8
fbshipit-source-id: 4ac560e03864686005da0aaaf3dc1b0b694b2fb8
  • Loading branch information
justinwoo authored and Facebook Github Bot 4 committed Mar 31, 2016
1 parent 19183e8 commit 5c4297b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions website/docs/ref/declarations.doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,40 @@ if (isLeapYear('2016')) console.log('Yay!');
Note that it is entirely up to the programmer to ensure that declared
definitions actually exist, and have the correct types.
## Mixins
You can declare a class which mixes in 1 or more other classes with the
`mixins` keyword. Mixing class `B` into class `A` copies `B`'s fields and
methods into `A`. Note, however, that any fields or methods that `B` inherits
are not copied over. Mixins are for code reuse, not for multiple inheritance.
*/

// You can mixin more than one class
declare class MyClass extends Child mixins MixinA, MixinB {}
declare class MixinA {
a: number;
b: number;
}
// Mixing in MixinB will NOT mix in MixinBase
declare class MixinB extends MixinBase {}
declare class MixinBase {
c: number;
}
declare class Child extends Base {
a: string;
c: string;
}
declare class Base {
b: string;
}

var c = new MyClass();
(c.a: number); // Both Child and MixinA provide `a`, so MixinA wins
(c.b: number); // The same principle holds for `b`, which Child inherits
(c.c: string); // mixins does not copy inherited properties,
// so `c` comes from Child

/*
## Declaring types
It is similarly useful to declare types. Like other declarations, type
Expand Down

0 comments on commit 5c4297b

Please sign in to comment.