Skip to content

Instantly share code, notes, and snippets.

@lgmkr
Last active April 23, 2020 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgmkr/c72bffc8491c77a2cba61c7a1e324ceb to your computer and use it in GitHub Desktop.
Save lgmkr/c72bffc8491c77a2cba61c7a1e324ceb to your computer and use it in GitHub Desktop.
ts-in-nodejs
// primitive types
let isOk: boolean = true;
let greeting: string = "Hello";
let count: number = 5;
let fruits: string[] = ["banana", "apple"]; // Array<string>
let tuple: [string, number] = ["hi", 10]; // tuple[3] = 1 <- error
enum Fruits {
banana = "BANANA",
apple = "APPLE",
}
Fruits.apple; // APPLE
let foo: any = 4;
foo.bar; // doesn't complain :(
const log = (): void => {
console.log("Hello");
};
// subtypes: undefined, null, never
let u: undefined = undefined;
let n: null = null;
let a: number = 4;
a = null; // till --strictNullChecks enabled
function infinite(): never {
while (true) {}
}
const infiniteArrow = (): never => {
while (true) {}
};
// non-primitive type
function dispatch(options: object) {}
dispatch({ step: 1 });
dispatch(undefined);
dispatch(null);
dispatch([false]);
dispatch(Fruits);
dispatch(1);
dispatch(false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment