TypeScript: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
Line 42: Line 42:
nation.add(new Person('John', 'Lennon'));
nation.add(new Person('John', 'Lennon'));
console.log(nation.count);
console.log(nation.count);
</syntaxhighlight>
'''package.json'''
<syntaxhighlight lang="json">
{
  "scripts":{
      "compile":"./node_modules/.bin/tsc",
      "start":"npm run compile && node dist/app.js"
  },
  "devDependencies":{
      "typescript":"^2.0.9"
  }
}
</syntaxhighlight>
</syntaxhighlight>


Line 67: Line 54:
       "app.ts"
       "app.ts"
   ]
   ]
}
</syntaxhighlight>
'''gulpfile.js'''
<syntaxhighlight lang="js">
var uglify = require('gulp-uglify');
var gulp = require('gulp');
var pump = require('pump');
gulp.task('compress', function (cb) {
  pump([
        gulp.src('dist/app.js'),
        uglify(),
        gulp.dest('dist/minify')
    ],
    cb
  );
});
gulp.task('default', ['compress']);
</syntaxhighlight>
'''package.json'''
<syntaxhighlight lang="json">
{
  "scripts": {
      "gulp": "./node_modules/.bin/gulp",
      "compile": "./node_modules/.bin/tsc",
      "start": "npm run compile && node dist/app.js && npm run gulp"
  },
  "devDependencies": {
      "typescript": "^2.0.9",
      "gulp-uglify": "^3.0.0",
      "gulp": "^3.9.1",
      "pump": "^3.0.0"
  }
}
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 03:26, 9 May 2018

.gitignore

dist
node_modules
npm-debug.log

person.ts

namespace People {
    export class Person {
        constructor(private firstName: string, private lastName: string){
            //TODO
        }
    }
}

nation.ts

namespace People {
    export class Nation {
        private population: Person[] = [];
        add(person: Person): void {
            this.population.push(person);
        }
        get count(): number {
            return this.population.length;
        }
    }
}

app.ts

/// <reference path="person.ts" />
/// <reference path="nation.ts" />
import Person = People.Person;
import Nation = People.Nation;

const nation: Nation = new Nation();
nation.add(new Person('John', 'Lennon'));
console.log(nation.count);

tsconfig.json

{
   "compilerOptions":{
      "target":"es5",
      "outFile":"dist/app.js"
   },
   "files":[
      "app.ts"
   ]
}

gulpfile.js

var uglify = require('gulp-uglify');
var gulp = require('gulp');
var pump = require('pump');
 
gulp.task('compress', function (cb) {
  pump([
        gulp.src('dist/app.js'),
        uglify(),
        gulp.dest('dist/minify')
    ],
    cb
  );
});

gulp.task('default', ['compress']);

package.json

{
   "scripts": {
      "gulp": "./node_modules/.bin/gulp",
      "compile": "./node_modules/.bin/tsc",
      "start": "npm run compile && node dist/app.js && npm run gulp"
   },
   "devDependencies": {
      "typescript": "^2.0.9",
      "gulp-uglify": "^3.0.0",
      "gulp": "^3.9.1",
      "pump": "^3.0.0"
   }
}

Build

npm install
npm start

References