microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.23k stars 12.52k forks source link

Add a new type PublicOf #31891

Open sroucheray opened 5 years ago

sroucheray commented 5 years ago

Suggestion

Add the PublicOf type suggested in this issue's comment by @DanielRosenwasser

Use Cases

Implement a class public properties/methods without the need to implement private/protected ones.

Examples

// type to add
type PublicOf<T> = {
    [P in keyof T]: T[P]
}
// use case
class C {
    private foo();

    public bar();
}

// No need to implement private method foo !
class D implements PublicOf<C> {
    public bar() {}
}

Checklist

My suggestion meets these guidelines:

dragomirtitian commented 5 years ago

This seems to me a marginal improvement over Pick<C, keyof C>

declare class C {
    private foo(): void

    public bar(): void
}

// No need to implement private method foo !
class D implements Pick<C, keyof C> {
    public bar() { }
}
sroucheray commented 5 years ago

Good point, alternate implementation:

type PublicOf<T> = Pick<T, keyof T>;
sroucheray commented 5 years ago

I'm trying a contribution, I'm not sure that my pull request #31970 is valid but I'm still trying.