Typescript

Documentation

Exercism Notes

Installing

Syntax Description
Header Title
Paragraph Text
this is <html>code

Skeleton

Files:

commands:

hello-world.test.ts:

import { helloworld } from './helloworld'
import { expect  } from 'chai'

describe('helloworld', () => {
    it('should return hi there', () => {
        expect(helloworld()).to.equal('hi there')
    })
})

hello-world.ts:

export const hithere = () : string => {
    return "hi there"
}

Comparison - Javascript and Typescript

Description of differences between JS and TS

TypeScript is a syntactical superset of JavaScript and adds optional static typing to the language. TS transpiles to JS.

TS JS
Static Typing: types and interfaces Dynamically typed
Object Oriented Prototype-Based Object Orientation
Transpiles to JS Just-In-time Compilation

Summary of Typescript features

Types

type Heading = {
    text: string;
    subheadings: Array<Heading>;
}

const heading: Heading = {text: "Welcome to Akalabeth, world of doom!", subheadings: []}

Type Declaration Files

declare namespace arithmetics {
    add(left: number, right: number): number;
    subtract(left: number, right: number): number;
    multiply(left: number, right: number): number;
    divide(left: number, right: number): number;
}

Tuples

Hashes/Dictionary are created by using index signatures

There isn't the same as a javascript object which can take obj['i'] - this is covered by interfaces, in object index signatures

interface nucleoTideMap { [index: string]: string }
const dnaToRna: nucleoTideMap = { 'g':'c','c':'g','t':'a','a':'u'}

ADTs in Typescript

Functions

VarArgs - variable length arguments

https://www.damirscorner.com/blog/posts/20180216-VariableNumberOfArgumentsInTypescript.html

Classes

TS supports ECMAscript 2015 classes syntax.

class ABC {
    name: string;
}
const abc = new ABC()