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

variance modifier wrapper class #184

Open
DetachHead opened this issue Jul 23, 2022 · 1 comment
Open

variance modifier wrapper class #184

DetachHead opened this issue Jul 23, 2022 · 1 comment
Labels

Comments

@DetachHead
Copy link
Owner

like #162 but for the whole class

@DetachHead
Copy link
Owner Author

failed attempt:

export const safeVariance = <Type extends AnyConstructor>(
    type: Type,
): SafeVarianceConstructor<Type> =>
    type as SafeVarianceConstructor<Type>

// this doesn't work because it needs to know how many generics the constructor takes.
// intersecting it with `Type` in an attempt to fix that doesn't work because that just makes it return the original instance type and not the SafeVariance one
type SafeVarianceConstructor<Type extends AnyConstructor> = Type & (new (...args: ConstructorParameters<Type>) => SafeVariance<InstanceType<Type>>)
test('safeVariance', () => {
    class A<T> extends safeVariance(
        class<T> {
            // eslint-disable-next-line prefer-arrow/prefer-arrow-functions -- testing safeVariance
            foo(_value: T) {
                return 1
            }
        },
    )<T> {
    }

    class B<T> extends safeVariance(
        class<T> extends A<T> {
            // eslint-disable-next-line prefer-arrow/prefer-arrow-functions -- testing safeVariance
            override foo(value: T) {
                return super.foo(value)
            }
        },
    )<T> {
    }

    const a = new B<number>()

    const b: B<unknown> = a // no error but there should be
})

@DetachHead DetachHead added hard help wanted Extra attention is needed and removed hard labels Jul 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant