borisyankov/DefinitelyTyped — pull request #2077 by omidkrad (2014-04-21 20:44:07+00:00)
Title: Backbone Generics (TSC 1.0)

Giving this another shot now that TypeScript 1.0 is released. This is being used in a rather large backbone.js project. Here are some breaking changes for the consumer code:

- The `extend` class is set as private. Use TypeScript class syntax to extend Model, Collection, View and Router.
- All code that inherits from backbone classes have to define generic parameters in their declarations. (This is going to be a typing requirement until [generics parameter overloads](https://typescript.codeplex.com/workitem/2438) are supported by the compiler)
- Have to use `typeof` keyword when assigning model type for a Collection. For example (from backbone-tests.ts):

```
class Library extends Backbone.Collection {
model: typeof Book;
}
```

**Other changes:**

The main change is the modifications in `backbone.d.ts` which adds Generics to backbone classes. This brings a very good type checking on Models everywhere in backbone apps, however, this also affects a bunch of other libraries and tests that depend on it:

```
backbone/backbone-tests.ts
backbone-relational/backbone-relational.d.ts
backbone-relational/backbone-relational-tests.ts
backgrid/backgrid.d.ts
backgrid/backgrid-tests.ts
giraffe/giraffe.d.ts
giraffe/giraffe-tests.ts
jointjs/jointjs.d.ts
knockback/knockback.d.ts
marionette/marionette.d.ts
```

Also:

- With this change `backbone.d.ts` adds reference to `underscore.d.ts` for using some of the interfaces in underscore.
- Added `groupBy` merhods to backbone
- Added/updated tests for new changes
- Using methods for `defaults`, `routes`, and `events` and added comments on how to use them as properties that receive object literals. This is due to new compiler behavior that won't allow defining methods if they are declared as member properties in the base class, but the opposite is allowed by casting the assigned value to . With this workaround the implementing classes can use either approach supported by BackboneJS: i.e. using object literal or function returning object literal for `defaults`, `routes`, and `events`. See https://typescript.codeplex.com/workitem/2441 also see `SettingDefaults` in backbone tests for how this is used.