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

[Feature Request] Support change type of this, to act on type guard of following codes #48833

Closed
5 tasks done
zyf0330 opened this issue Apr 25, 2022 · 3 comments
Closed
5 tasks done
Labels
Duplicate An existing issue was already created

Comments

@zyf0330
Copy link

zyf0330 commented Apr 25, 2022

Suggestion

πŸ” Search Terms

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

change variable type, change this type, asserts

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

We know there is is to do type guard acting on function return value. And we have as to convert type inside a statement.

Can we has a keyword to change type of variable or this as a statement, to change type and affect following all codes instead of just affect one statement like as?

πŸ“ƒ Motivating Example

When facingto this, I don't want to define a aliased variable like that. And use as inside every statement to convert type is cumbersome.

πŸ’» Use Cases

It is like

function func(this: { a?: string; b: string }) {
  delete this.a
  this is {a: never; b: string} // not work now
  this.a // should not use
}
@jcalz
Copy link
Contributor

jcalz commented Apr 25, 2022

Duplicate of #10421, seems to be doable via an assertion function as implemented in #32695:

function assertType<T>(a: any): asserts a is T {}

function func(this: { a?: string; b: string }) {
  delete this.a
  assertType<{a: never, b: string}>(this)
  this.a // never
}

Playground link

@zyf0330
Copy link
Author

zyf0330 commented Apr 25, 2022

@jcalz Sorry, I didn't know asserts keyword. But still I just don't want to create an extra helper function to achieve what I want.

@zyf0330 zyf0330 changed the title Support change type of this, to act on type guard of following codes [Feature Request] Support change type of this, to act on type guard of following codes Apr 25, 2022
@andrewbranch andrewbranch added the Duplicate An existing issue was already created label May 2, 2022
@typescript-bot
Copy link
Collaborator

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

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