-
Notifications
You must be signed in to change notification settings - Fork 2k
TypeScript Output
Erik Demaine edited this page Mar 22, 2021
·
15 revisions
This is a living document to assemble product requirements for potential TypeScript output from the CoffeeScript compiler.
If the CoffeeScript compiler were to output TypeScript source code, similar to the way it outputs JSX now, what TypeScript syntax should it support?
It’s a separate question what CoffeeScript syntax will be designed to support each TypeScript syntax; for now we just want to create a list of output syntax that we eventually want to support.
function greeter(person: string) {}
class Student {
fullName: string;
constructor(public firstName: string, public middleInitial: string, public lastName: string) {}
}
let list: Array<number> = [1, 2, 3];
const myCanvas = document.getElementById("main_canvas") as HTMLCanvasElement;
const myCanvas = <HTMLCanvasElement>document.getElementById("main_canvas");
function liveDangerously(x?: number | undefined) {
// No error
console.log(x!.toFixed());
}
function isFish(pet: Fish | Bird): pet is Fish {
return (pet as Fish).swim !== undefined;
}
[Generic Functions](https://www.typescriptlang.org/docs/handbook/2/functions.html#generic-functions
function firstElement<Type>(arr: Type[]): Type {
return arr[0];
}
interface Person {
firstName: string;
lastName: string;
}
type WindowStates = "open" | "closed" | "minimized";
type BotId = string | number
enum BooleanLikeHeterogeneousEnum {
No = 0,
Yes = "YES",
}
function buildName(firstName: string, lastName?: string) {
if (lastName) return firstName + " " + lastName;
else return firstName;
}
class Animal {
private id: string;
protected name: string;
public constructor(theName: string) {
this.name = theName;
}
public move(distanceInMeters: number) {
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
class Octopus {
readonly name: string;
readonly numberOfLegs: number = 8;
constructor(theName: string) {
this.name = theName;
}
}
Accessors (ES6)
class Employee {
private _fullName: string;
get fullName(): string {
return this._fullName;
}
set fullName(newName: string) {
if (newName && newName.length > fullNameMaxLength) {
throw new Error("fullName has a max length of " + fullNameMaxLength);
}
this._fullName = newName;
}
}
abstract class Animal {
abstract makeSound(): void;
move(): void {
console.log("roaming the earth...");
}
}
class Cat extends Animal {
constructor() {
super("I'm a cat"); // constructors in derived classes must call super()
}
makeSound(): void {
console.log('Meowwww')
}
}
declare global {
interface Array<T> {
toObservable(): Observable<T>;
}
}
Array.prototype.toObservable = function() {
...
};