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

Preserve type narrowing of readonly properties of classes for use in closures #57884

Closed
6 tasks done
swachter opened this issue Mar 21, 2024 · 4 comments
Closed
6 tasks done
Labels
Duplicate An existing issue was already created

Comments

@swachter
Copy link

πŸ” Search Terms

flow typing, type narrowing, closures

βœ… Viability Checklist

⭐ Suggestion

Type narrowing of readonly properties of classes should be preserved in closures that are defined after the narrowing.

πŸ“ƒ Motivating Example

We use handler classes that capture events as member properties. When narrowing on the this.event member the narrowed type should be preserved when a closure is created that accesses that member.

type Event = { x: 1, f1: string } | { x: 2, f2: string }

class Handler {
    constructor(readonly ev: Event) {
        switch (ev.x) {
            case 1:
                ev.f1 // ok
                void [].map(() => ev.f1) // ok
                break;
            case 2:
                ev.f2 // ok 
                void [].map(() => ev.f2) // ok
                break;
        }
    }
    process() {
        switch (this.ev.x) {
            case 1:
                this.ev.f1 // ok
                void [].map(() => this.ev.f1) // error
                break;
            case 2:
                this.ev.f2 // ok
                void [].map(() => this.ev.f2) // error
                break;
        }
    }
}

πŸ’» Use Cases

  1. This would align flow typing for class members to what flow typing does for local const values or even let values after their last assignment.
  2. This would make the current workaround of copying class members into local constants unnecessary.
@MartinJohns
Copy link
Contributor

Duplicate of #51773.

@RyanCavanaugh
Copy link
Member

Yep; a derived class can "open up" the readonly at which point the narrowing is no longer sound

class Derived extends Handler {
  ev!: Event;
  constructor(ev: Event) {
    super(ev);
  }
}

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 21, 2024
@swachter
Copy link
Author

Would it be sound for private members?

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants