Skip to content

Instantly share code, notes, and snippets.

@blakef
Last active October 29, 2015 01:47
Show Gist options
  • Save blakef/b098bd06df561ee53a13 to your computer and use it in GitHub Desktop.
Save blakef/b098bd06df561ee53a13 to your computer and use it in GitHub Desktop.
TypeScript + RxJS
import path = require('path');
import Git = require('nodegit');
import Rx = require('rx');
class Repo {
private repo: Rx.Observable<Git.Repository>;
constructor(private pathname: string) {
this.repo = Rx.Observable.fromPromise(Git.Repository.open(pathname));
}
branches() {
this.repo
.flatMap(repository => repository.getCurrentBranch())
.subscribe(reference => console.log(reference.name()))
;
}
}
var dir: string = path.resolve('../path/to/git/repo');
var repo: Repo = new Repo(dir);
repo.branches();
// $ node hello.js
// > refs/heads/master
declare module "nodegit" {
export class Reference {
name(): string;
}
export class Repository {
static open(path: string): Rx.IPromise<Repository>;
getCurrentBranch(): Rx.IPromise<Reference>;
}
}
declare module Rx {
declare module internals {
...
export interface IPromise<T> {
then<R>(onFulfilled: (value: T) => IPromise<R>, onRejected: (reason: any) => IPromise<R>): IPromise<R>;
then<R>(onFulfilled: (value: T) => IPromise<R>, onRejected?: (reason: any) => R): IPromise<R>;
then<R>(onFulfilled: (value: T) => R, onRejected: (reason: any) => IPromise<R>): IPromise<R>;
then<R>(onFulfilled?: (value: T) => R, onRejected?: (reason: any) => R): IPromise<R>;
}
...
}
}
}
}
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"isolatedModules": false,
"jsx": "react",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true
},
"filesGlob": [
"**/*.ts",
"**/*.tsx",
"!node_modules/**"
],
"files": [
"hello.ts",
"typings/node-git/node-git.d.ts",
"typings/node/node.d.ts",
"typings/rx/rx-lite.d.ts",
"typings/rx/rx.d.ts"
],
"exclude": []
}
@blakef
Copy link
Author

blakef commented Oct 29, 2015

Example of using TypeScript with an external library, and providing custom type definition files. This runs on Node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment