bcherny / programming-typescript-answers Goto Github PK
View Code? Open in Web Editor NEWOfficial answers for exercises from Orielly's Programming TypeScript
Official answers for exercises from Orielly's Programming TypeScript
In chapter Five, Mixins section, the expected behaviour is that withEZDebug mixin logs:
ClassName {"id": 3, "name": "The Name"}
But is logging Function {"id": 3, "name": "The Name"}
instead. @bcherny
// MIXINS
type ClassConstructor<T> = new(...args: any[]) => T;
function withEZDebug<C extends ClassConstructor<{ getDebugValue(): object }>>(Class: C) {
return class extends Class {
debug() {
let Name = Class.constructor.name;
let value = this.getDebugValue();
return Name + '(' + JSON.stringify(value) + ')';
}
constructor(...args: any[]) {
super(...args);
}
}
}
class HardToDebugUser {
constructor(
private id: number,
private firstName: string,
private lastName: string,
) {}
getDebugValue() {
return {
id: this.id,
name: this.firstName + ' ' + this.lastName
};
}
}
let User = withEZDebug(HardToDebugUser);
let user = new User(3, 'First Name', 'Second Name');
console.log(user.debug());
2 (repo) == 3 (book)
3 (repo) == 4 (book)
4 (repo) == 5 (book)
and so on...
I think that this assertion function is comparing the array 'a' with the each elements of array 'b'.
So, I tried that the arguments [1], [1], [1]
give to function is. The result was false.
I think that the result is true in this case.
Please tell me why this assertion function 'is' is correct?
I read this book in Japanese. If the translation is not good, could you tell me the sentences of the exercise 5 in English?
Thanks for writing this great book. I'm new to TypeScript and I'm enjoying working through it.
I noticed that the answer to Chapter 4 Exercise 5 has the function signature:
function is<T>(a: T, ...b: [T, ...T[]]): boolean
However, when I was attempting it on my own, I came up with:
function is<T>(a: T, ...b: T[]): boolean
which also seems to work. It looks like there's also an open PR (#8) with the signature:
function is<T>(a: T, ...b: [...T[]]): boolean
What's the difference, if any, between these different generic types?
In chapter 8 (Async Streams >> Event Emitters, on pages 185-186) you have defined the following two types:
type Events = {
ready: void
error: Error
reconnecting: {
delay: number,
attempt: number
}
}
type RedisClient = {
on<E extends keyof Events>(event: E, f: (args: Events[E]) => void): void
}
I would like to have a naive implementation like this:
let Client: RedisClient = {
on<E extends keyof Events>(event: E, f: (args: Events[E]) => void) {
if(event === "reconnecting") {
f({delay: 1, attempt: 1})
}
}
}
Client.on("reconnecting", (param :{delay: number, attempt: number}) => console.log(param.delay))
Unfortunately it gives me the following error (at this f({delay: 1, attempt: 1})
line):
Argument of type '{ delay: number; attempt: number; }' is not assignable to parameter of type 'Events[E]'.
Type '{ delay: number; attempt: number; }' is not assignable to type 'void & Error & { delay: number; attempt: number; }'.
Type '{ delay: number; attempt: number; }' is not assignable to type 'void'.(2345)
It seems like it uses intersection instead of union. How can I fix this?
I've found a related Typescript issue, but I couldn't manage to apply the suggested workaround in this example. Can you help me with this please?
Hi Boris,
Recently, I have been learning TypeScript from your book. I found some minor issues concerning the book, and want to know are there any good approaches to give feedbacks or submit PRs to it. Thanks a lot!
Andy
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.