Skip to content

Instantly share code, notes, and snippets.

@dpashkevich
Created November 19, 2018 04:39
Show Gist options
  • Save dpashkevich/5e0e08a6009665294210bdadf1bf3318 to your computer and use it in GitHub Desktop.
Save dpashkevich/5e0e08a6009665294210bdadf1bf3318 to your computer and use it in GitHub Desktop.
Gists for article "7 outdated excuses to avoid TypeScript"
declare function camelize(s: string): string;
function greeter(person) {
if (!person || !person.firstName || !person.lastName) {
throw new Error('invalid arguments');
}
return "Hello, " + person.firstName + " " + person.lastName;
}
// Jasmine spec:
describe("greeter", function() {
it("returns greeting on valid input", function() {
expect(
greeter({firstName: 'James', lastName: 'Hetfield'})
).toEqual('Hello, James Hetfield');
});
it("throws on invalid input", function() {
expect(() => greeter()).toThrowError('invalid arguments');
expect(() => greeter(5)).toThrowError('invalid arguments');
expect(() => greeter({firstName: 'Jason'})).toThrowError('invalid arguments');
});
});
let user = { firstName: "James", lastName: "Hetfield" };
console.log(greeter(user));
function greeter(person) {
return "Hello, " + person.firstName + " " + person.lastName;
}
interface Person {
firstName: string;
lastName: string;
}
function greeter(person: Person) {
return "Hello, " + person.firstName + " " + person.lastName;
}
// Jasmine spec:
describe("greeter", function() {
it("returns greeting", function() {
expect(
greeter({firstName: 'James', lastName: 'Hetfield'})
).toEqual('Hello, James Hetfield');
});
});
class Greeter {
constructor(message) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
npm install --save-dev @types/jasmine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment