TypeScriptFor C# Programmers

TypeScript

Statically typed superset of JavaScript

From Microsoft in 2012

Familiar names such as Anders Hejlsberg

let x = 5;
x = "cat";
let x: number = 5;
x = "cat";
let x: number = 5;
let x = 5;

The TypeScript philosophy

TypeScript is not a new programming language

Follows the JavaScript language standard and semantics

5 + 'cat' is allowed

Does not have its own runtime

Differences between C# and TypeScript

TypeScript uses a structural type system

C# uses a nominal type system

class A {};
class B {};
const item = new A();
const item2 : B = item;

TypeScript is unsound

function makePerson(name) {
return {
name: name
};
}
function printPerson(person) {
console.log(person.name);
}
interface Person {
name: string;
}
function makePerson(name: string) {
return {
name: name
};
}
function printPerson(person: Person) {
console.log(person.name);
}
printPerson({ name: "Dave" });
type Person = {
name: string;
}
function printPerson(person: Person) {
console.log(person.name);
}

Nullable types

Null was invented in 1965

Called "the million dollar mistake"

function uppercaseString(str) {
return str.toUpperCase();
}
function uppercaseString(str: string | null) {
if (str !== null) {
return (str.toUpperCase());
}
}

And much more

Generics

Enumerations

Class visibility: private, public...

Literal types

Decorators

Refactoring

JavaScript interaction

Bindings

Hopefully first-party

DefinitelyTyped

npm install @types/mylibrary

🙏

Roll your own

Any

The future is bright

Vue is being rewritten in TypeScript

Over 5,000 JavaScript libraries

TypeScript compiler gets 4,039,800 downloads a week

Where to go next?

typescriptlang.org

TypeScript Playground

TypeScript Handbook

Visual Studio Code

Questions?