= typescript = * https://www.typescriptlang.org * https://www.typescriptlang.org/docs/tutorial.html == install == npm install -g typescript == Compile == tsc helloworld.ts == kate == https://github.com/PrettyFlower/KateTypeScriptSyntaxHighlighting {{{ wget https://github.com/PrettyFlower/KateTypeScriptSyntaxHighlighting/raw/master/typescript.xml cp typescript.xml /usr/share/apps/katepart/syntax/typescript.xml }}} With let keyword added {{{#!highlight xml if else for in while do continue break with try catch finally switch case new var function return delete true false void throw typeof const default this null undefined class export declare module import static interface implements constructor public private string number bool any extends let }}} == Sample code typescript for browser == {{{#!highlight bash npm install -g typescript npm install -g webpack }}} === greeter.html === {{{#!highlight html TypeScript Greeter }}} === lib.ts === * https://www.typescriptlang.org/docs/handbook/modules.html Any declaration (such as a variable, function, class, type alias, or interface) can be exported by adding the export keyword. {{{ export function getText(){ return "text"; } }}} === greeter.ts === * import {xyz} from "module"; {{{ import {getText} from "./lib"; interface Person { firstName: string; lastName: string; } function greeterPerson(p:Person) { return "Hello GP, " + p.firstName + ' ' + p.lastName + ' ' + getText() ; } function greeter(person:string) { return "Hello, " + person; } var user = "XPTO User"; //document.body.innerHTML = greeter(user); document.body.innerHTML = greeterPerson( {firstName:"First",lastName:"Last"} ); }}} === tsconfig.json === {{{#!highlight javascript { "compilerOptions": { "module": "es6", "noImplicitAny": true, "removeComments": true, "preserveConstEnums": true, "sourceMap": true }, "files": [ "greeter.ts", "lib.ts"] } }}} === webpack.config.js === {{{#!highlight javascript var path = require('path'); module.exports = { entry: ['./greeter.js','./lib.js'], output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist') } }; }}} === build === {{{#!highlight bash tsc webpack --config webpack.config.js }}} == ReactJS + typescript example == === webpack.config.js === {{{ var path = require('path'); module.exports = { entry: {main:'./greeter.js'}, resolve: { extensions: ['.js', '.jsx', '.ts', '.tsx'] }, output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist') }, mode:"development" }; }}} === tsconfig.json === {{{ { "compilerOptions": { "module": "es2015", "target": "es5", "noImplicitAny": true, "removeComments": true, "preserveConstEnums": true, "sourceMap": true, "moduleResolution": "node", "allowSyntheticDefaultImports": true, "jsx": "react" } /*, "files": [ "greeter.tsx", "lib.ts","App.tsx"] */ } }}} === lib.ts === {{{ function getText() { return "text"; } interface Person { firstName: string; lastName: string; } function greeterPerson(p:Person) { return "Hello GP, " + p.firstName + ' ' + p.lastName + ' ' + getText() ; } function greeter(person:string) { return "Hello, " + person; } interface HumanInterface { name: string; getName(): void; } class Human implements HumanInterface{ name: string; constructor(name: string) { this.name = name; } getName() { return "My name is " + this.name; } } export {greeter, greeterPerson, Human, HumanInterface , Person, getText} }}} === greeter.html === {{{ React + typescript test
}}} === package.json === {{{#!highlight javascript { "name": "test", "private": true, "version": "0.0.0", "devDependencies": { "@types/react": "15.0.35", "@types/react-dom": "15.5.1", "@types/webpack-env": "1.13.0", "react": "15.6.1", "react-dom": "15.6.1", "typescript": "2.4.1", "webpack": "2.5.1" } } }}} === App.tsx === {{{ import React from 'react'; import {Person,greeterPerson,Human,HumanInterface} from './lib'; import NameHolder from './NameHolder'; interface MyState { currDate: string; greetings: string; human: HumanInterface; } function createMyState( currDate: string, greetings: string, human: HumanInterface) : MyState { return {currDate:currDate, greetings:greetings, human: human}; } interface MyProps { prop:string; } export default class App extends React.Component { text: string; timerID: number; constructor(props: MyProps) { super(props); this.text=this.props.prop; this.state= createMyState("waiting for date","waiting for greetings",null); // init with MyState format } public static createHuman(name:string) : HumanInterface { return new Human(name); } public tickHandler() { let cd = new Date().toString(); let greetings = greeterPerson({firstName:"first",lastName:"last"}); let human:HumanInterface = App.createHuman("JohnDoe"); this.setState( createMyState(cd, greetings, human) ); } componentDidMount() { // called after the 1st render ! console.log("First render"); this.timerID = setInterval(this.tickHandler.bind(this), 1000); } componentWillUnmount() { // component will be destroyed console.log("Will be destroyed soon"); clearInterval(this.timerID); } public render() { // do not call set state in render ! return (

Hello World!!! -- {this.text} -- {this.state.greetings}

Human { this.state.human!=null ? this.state.human.getName() : "" } {this.state.currDate}

{this.props.children}
); } } }}} === greeter.tsx === {{{ import React from 'react'; import ReactDOM from 'react-dom'; import {getText} from "./lib"; import App from './App'; // uses App.tsx import NameHolder from './NameHolder'; console.log("Entry point"); let appContainer = document.getElementById('app') ; let markup = ; ReactDOM.render( markup , appContainer); }}} === NameHolder.tsx === {{{ import React from 'react'; // Component properties (XML attributes) interface NameHolderProperties { attr: string; } interface NameHolderState { attr: string; } // properties and state export default class NameHolder extends React.Component { constructor(props:NameHolderProperties){ super(props); // populates the this.props this.state={attr:this.props.attr}; } public render() { return !!!{this.state.attr}!!!; } } }}} === build.sh === {{{#!highlight bash echo Delete dist folder rm -rf dist echo Install NPM packages npm install echo Compile project tsc echo Create application bundle webpack --config webpack.config.js }}}