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

Allow decorators to apply type guards to arguments of the method/function they decorate #50159

Open
Barely-Awake opened this issue Aug 3, 2022 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@Barely-Awake
Copy link

Barely-Awake commented Aug 3, 2022

Bug Report

πŸ”Ž Search Terms

decorators, type guards

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about decorators

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

class LogPerson {
    @isNamePresent
    log(person: Person) {
        // person.name is guaranteed to be a string due to @isNamePresent decorator, yet error is still thrown
        thisNeedsPersonName(person.name)
    }
}

interface Person {
    name: string | undefined
}

function isNamePresent(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
        const originalFunction = descriptor.value;
        descriptor.value = function () {
            const person = arguments[0];

            if (person.name === null || person.name === undefined)
                return console.log("Person doesn't have a name");
            
            return originalFunction.apply(this, arguments);
        }
}

function thisNeedsPersonName(name: string) {
    console.log(`name is ${name}`)
}

πŸ™ Actual behavior

TypeScript throws the error Type 'undefined' is not assignable to type 'string'. when person.name is used as a parameter to a method guarded by the @isNamePresent decorator

πŸ™‚ Expected behavior

TypeScript should see the decorator's type guard show the person.name field as only being a string, not string | undefined.
This could be too closely related to function calls not resetting narrowing, in which case, just close this issue. Though I do think it is different enough where it might be possible to be fixed.

@MartinJohns
Copy link
Contributor

MartinJohns commented Aug 3, 2022

Duplicate of #4881. Decorators do not provide the ability to change type information.

Last I read is that the TypeScript will first focus on decorator support (the current implementation is experimental and based on a deprecated proposal), and only then they'll consider looking at mutating types via decorators.

@fatcerberus
Copy link

fatcerberus commented Aug 3, 2022

This doesn't look like #4881 - the intention doesn't seem to be to change the contract of the class, but rather to add a type check to the implementation. So this is more equivalent to calling throwIfPersonHasNoName(person) and expecting TypeScript to automatically know person.name can't be undefined afterwards.

@Barely-Awake
Copy link
Author

So this is more equivalent to calling throwIfPersonHasNoName(person) and expecting TypeScript to automatically know person.name can't be undefined afterwards

Yea, exactly. The way I think it might be able to be done is by basically combining the @isNamePresent function with the method below the decorator / the method it is decorating (In my example that'd be log) and then using that to figure out if any types have been narrowed by the decorator.

Not sure if it's possible, but I think it'd be a really nice thing to have implemented if it is possible.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Aug 4, 2022
@RyanCavanaugh RyanCavanaugh changed the title Type guards in decorators not updating variable types Allow decorators to apply type guards to arguments of the method/function they decorate Aug 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants