Giter Site home page Giter Site logo

programming-typescript-answers's People

Contributors

bcherny avatar dependabot[bot] avatar karataev avatar kimamula avatar p3trur0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

programming-typescript-answers's Issues

withEZDebug Mixin not working as expected

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());

Chapter 4 Exercise 5

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?

Chapter 8's RedisClient example

In chapter 8 (Async Streams >> Event Emitters, on pages 185-186) you have defined the following two types:

type Events = {    
  readyvoid    
  errorError    
  reconnecting{
    delaynumber, 
    attemptnumber
    }
}

type RedisClient = {    
  on<E extends keyof Events>(eventE, f(argsEvents[E]) => void)void
}

I would like to have a naive implementation like this:

let ClientRedisClient = {        
  on<E extends keyof Events>(eventE, f(argsEvents[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?

exercise 5 in chapter 4

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?
スクリーンショット 2020-04-24 2 18 29

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.