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

fix private methods with UnsafeVariance #160

Open
github-actions bot opened this issue Jun 25, 2022 · 0 comments
Open

fix private methods with UnsafeVariance #160

github-actions bot opened this issue Jun 25, 2022 · 0 comments

Comments

@github-actions
Copy link

// TODO: fix private methods, probs dependent on https://github.com/microsoft/TypeScript/issues/22677

import { AnyFunction } from 'tsdef'

/**
 * converts a non-arrow function type to an arrow function type. arrow functions are checked more strictly than
 * non-arrow functions
 * @see https://github.com/microsoft/TypeScript/pull/18654
 * @example
 * declare class Foo<T = unknown> {
 *     notArrowFunction(value: T): void
 *     isArrowFunction: (value: T) => void
 * }
 * type ArrowFunction = ToArrowFunction<Foo['notArrowFunction']>
 */
export type ToArrowFunction<T extends AnyFunction> = {
    fn: (...args: Parameters<T>) => ReturnType<T>
}['fn']

/**
 * converts an arrow function type to a non-arrow function type. arrow functions are checked more strictly than
 * non-arrow functions
 * @see https://github.com/microsoft/TypeScript/pull/18654
 * @example
 * declare class Foo<T = unknown> {
 *     notArrowFunction(value: T): void
 *     isArrowFunction: (value: T) => void
 * }
 * type NotArrowFunction = ToNonArrowFunction<Foo['isArrowFunction']>
 */
export type ToNonArrowFunction<T extends AnyFunction> = {
    fn(...args: Parameters<T>): ReturnType<T>
}['fn']

/**
 * transforms all methods in `T` into arrow functions, which makes typescript check it more strictly
 * @see https://github.com/microsoft/TypeScript/pull/18654
 */
export type SafeVariance<T> = {
    wrapped: T &
        {
            [Key in keyof T]: T[Key] extends AnyFunction ? ToArrowFunction<T[Key]> : T[Key]
        }
}['wrapped']

/**
 * transforms all methods in `T` into non-arrow functions, which turns off variance checks
 *
 * **WARNING:** doesn't work properly with private/protected methods
 * @see https://github.com/microsoft/TypeScript/pull/18654
 */
// TODO: fix private methods, probs dependent on https://github.com/microsoft/TypeScript/issues/22677
export type UnsafeVariance<T> = {
    wrapped: {
        [Key in keyof T]: T[Key] extends AnyFunction ? ToNonArrowFunction<T[Key]> : T[Key]
    }
}['wrapped']
@github-actions github-actions bot added the todo label Jun 25, 2022
@DetachHead DetachHead changed the title fix private methods, probs dependent on https://github.com/microsoft/TypeScript/... fix private methods with UnsafeVariance Jun 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant