Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code example is not correct in Intersection Types section #32183

Open
Zyc0017 opened this issue Jun 30, 2019 · 0 comments · May be fixed by microsoft/TypeScript-Handbook#1058
Open

code example is not correct in Intersection Types section #32183

Zyc0017 opened this issue Jun 30, 2019 · 0 comments · May be fixed by microsoft/TypeScript-Handbook#1058
Labels
Docs The issue relates to how you learn TypeScript

Comments

@Zyc0017
Copy link

Zyc0017 commented Jun 30, 2019

Code

function extend<First, Second>(first: First, second: Second): First & Second {
    const result: Partial<First & Second> = {};
    for (const prop in first) {
        if (first.hasOwnProperty(prop)) {
            (result as First)[prop] = first[prop];
        }
    }
    for (const prop in second) {
        if (second.hasOwnProperty(prop)) {
            (result as Second)[prop] = second[prop];
        }
    }
    return result as First & Second;
}

class Person {
    constructor(public name: string) { }
}

interface Loggable {
    log(name: string): void;
}

class ConsoleLogger implements Loggable {
    log(name) {
        console.log(`Hello, I'm ${name}.`);
    }
}

const jim = extend(new Person('Jim'), ConsoleLogger.prototype);
jim.log(jim.name);

class methods are non-enumerable, so the second for...in loop will not add log method to the result obj.

@RyanCavanaugh RyanCavanaugh added the Docs The issue relates to how you learn TypeScript label Jul 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs The issue relates to how you learn TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants