Skip to content

Instantly share code, notes, and snippets.

@nfreear
Created October 15, 2019 08:50
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 nfreear/8dc6878bd3ffd8090cd8c0d32e7cc297 to your computer and use it in GitHub Desktop.
Save nfreear/8dc6878bd3ffd8090cd8c0d32e7cc297 to your computer and use it in GitHub Desktop.
My first typescript / Typescript in 5 minutes.
/*!
My first typescript | NDF, 15-Oct-2019.
https://typescriptlang.org/docs/handbook/typescript-in-5-minutes.html
*/
class Student {
fullName: string;
constructor(public firstName: string, public middleInitial: string, public lastName: string) {
this.fullName = `${ firstName } ${ middleInitial } ${ lastName }`;
// this.fullName = firstName + " " + middleInitial + " " + lastName;
}
}
interface Person {
firstName: string;
lastName: string;
}
function greeter(person: Person) {
return `Hello, ${ person.firstName } ${ person.lastName }!`; // "Hello, " + person.firstName + " " + person.lastName;
}
let user = new Student("Jane", "M.", "User");
// let user = { firstName: "Jane", lastName: "User" };
function greeterStr(person: string) {
return "Hello, " + person;
}
// let user = "Jane User";
// let user = [ 0, 1, 2 ]; // TSError.
console.log(greeter( user ));
/* ------------------------------------------------------------------ */
const _PKG_ = {
"private": true,
"name": "@nfreear/my-first-typescript",
"devDependencies": {
"ts-node": "^8.4.1",
"typescript": "^3.6.4"
},
"scripts": {
"build": "tsc index.ts",
"start": "ts-node index.ts"
}
}
const _ERROR_ =
`/Users/[_NAME_]/workspace/admins-project/my-first-typescript/node_modules/ts-node/src/index.ts:245
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
index.ts:12:21 - error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'string'.
12 console.log(greeter(user))
~~~~
--
at createTSError (/Users/ndf42/workspace/admins-project/my-first-typescript/node_modules/ts-node/src/index.ts:245:12)
at reportTSError (/Users/ndf42/workspace/admins-project/my-first-typescript/node_modules/ts-node/src/index.ts:249:19)
... `;
// End.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment