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

PropertyDecorator factory with generic type is not so intelligent #31917

Open
JounQin opened this issue Jun 15, 2019 · 1 comment
Open

PropertyDecorator factory with generic type is not so intelligent #31917

JounQin opened this issue Jun 15, 2019 · 1 comment
Assignees

Comments

@JounQin
Copy link

@JounQin JounQin commented Jun 15, 2019

TypeScript Version: 3.5.1

Search Terms:

Code

export const Reactive = <T, K extends keyof T>(
  setter?: (this: T, value?: T[K]) => boolean | void,
) => (target: T, propertyKey: K) => {
  // do something
};

class Test {
  @Reactive(function(name) {
    // error
  })
  name: string
}

Expected behavior:
Automatically detect this should be type of Test, and name should be type of string

Actual behavior:
Argument of type '"name"' is not assignable to parameter of type 'never'.

Playground Link:
Playground Link

Related Issues:

@thejahweh

This comment has been minimized.

Copy link

@thejahweh thejahweh commented Jul 24, 2019

Maybe this is the same Issue as I have.

I would expect that this would cry a river at @testProperty('shouldNotWork') but it doesn't:

function testProperty<T>(otherProperty: keyof T): PropertyDecorator {
    return (target, propertyKey) => {
        // ... some code
    };
}

class Test {
    @testProperty('shouldWork')
    test: boolean = true;
    @testProperty('shouldNotWork')
    test2: boolean = true;
    shouldWork: boolean = true;
}

My current workaround is to define the type manually like this:

class Test2 {
    @testProperty<Test2>('shouldWork')
    test: boolean = true;
    @testProperty<Test2>('shouldNotWork')
    test2: boolean = true;
    shouldWork: boolean = true;
}

https://www.typescriptlang.org/play/index.html?experimentalDecorators=true#code/GYVwdgxgLglg9mABFApgZygBQE5wA4rZQCeAPACoB8AFHFABaE76EkBciA1isXMIuQCUHZgSLEAIighxsAQyizEAbwCwAKERbE2FFBDYk1KHOwBzPQBpEeXGJIBpHoMQBeSio3bviAPS-EADpgxDQ4AFsURBkAExQvbQBfAG4NRI0NCAAbOTQ0AXQoT01tAAFUDFFWYmoAcjR6OBAsmIB1WU5awQStCqgOACM4OCyUOSRXZGwQFFSSrXLCqvE6hqaWgDk6duxO7vnkQoAmQeHR8bcpmbnvNea2jtORsYmr2bSM9Wzc-PJj4u8i0qdmqFGONHqjXuOz2PUOGCe51eUGm7wOQKwIPEYIwRwhd022w6XThfROiCGzwukxR1zhBIeu0RL0utLRiSAA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.