TypeScript: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
'''.gitignore'''
==.gitignore==
<syntaxhighlight lang="ini">
<source lang="ini">
dist
dist
node_modules
node_modules
npm-debug.log
npm-debug.log
</syntaxhighlight>
</source>


'''person.ts'''
==person.ts==
<syntaxhighlight lang="ts">
<source lang="ts">
namespace People {
namespace People {
     export class Person {
     export class Person {
Line 15: Line 15:
     }
     }
}
}
</syntaxhighlight>
</source>


'''nation.ts'''
==nation.ts==
<syntaxhighlight lang="ts">
<source lang="ts">
namespace People {
namespace People {
     export class Nation {
     export class Nation {
Line 30: Line 30:
     }
     }
}
}
</syntaxhighlight>
</source>


'''app.ts'''
==app.ts==
<syntaxhighlight lang="ts">
<source lang="ts">
/// <reference path="person.ts" />
/// <reference path="person.ts" />
/// <reference path="nation.ts" />
/// <reference path="nation.ts" />
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>
</source>


'''tsconfig.json'''
==tsconfig.json==
<syntaxhighlight lang="json">
<source lang="json">
{
{
     "compilerOptions": {
     "compilerOptions": {
Line 62: Line 62:
     ]
     ]
}
}
</syntaxhighlight>
</source>


'''gulpfile.js'''
==gulpfile.js==
<syntaxhighlight lang="js">
<syntaxhighlight lang="js">
var uglify = require('gulp-uglify');
var uglify = require('gulp-uglify');
Line 81: Line 81:


gulp.task('default', ['compress']);
gulp.task('default', ['compress']);
</syntaxhighlight>
</source>


'''package.json'''
==package.json==
<syntaxhighlight lang="json">
<source lang="json">
{
{
   "scripts": {
   "scripts": {
Line 98: Line 98:
   }
   }
}
}
</syntaxhighlight>
</source>


'''Build'''
'''Build'''
<syntaxhighlight lang="bash">
<source lang="bash">
npm install
npm install
npm start
npm start
</syntaxhighlight>
</source>


== References ==
== References ==

Revision as of 04:26, 14 March 2021

.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": {
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "outFile": "dist/app.js",
        "target": "es5"
    },
    "include": [
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

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']); </source>

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