Skip to content

Bring typeof switch closer inline with if #27680

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

Merged

Conversation

jack-williams
Copy link
Collaborator

@jack-williams jack-williams commented Oct 10, 2018

This PR brings switch narrowing with typeof closer inline with if, which was recently updated in two keys ways:

  1. unknown narrowing for "function" and "object" cases: the former should be Function, the latter object | null.
  2. narrowing aswell as filtering for union type constituents.

Fixes #27180
Fixes #27335
Adds tests for #27181

@jack-williams jack-williams changed the title Bring typeof switch inline with if Bring typeof switch closer inline with if Oct 10, 2018
@@ -15317,6 +15317,32 @@ namespace ts {
return caseType.flags & TypeFlags.Never ? defaultType : getUnionType([caseType, defaultType]);
}

function getTypeFromName(type: Type, text: string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer a more descriptive name like getNarrowedTypeByTypeofResult

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went with getImpliedTypeFromTypeofCase to be consistent with terminology used in the documentation of narrowBySwitchOnTypeOf.

@RyanCavanaugh
Copy link
Member

@typescript-bot test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 10, 2018

Heya @RyanCavanaugh, I've started to run the extended test suite on this PR at b12ed40. You can monitor the build here. It should now contribute to this PR's status checks.

- Narrow unknown
- Narrow union members (in addition to filtering)
@jack-williams jack-williams force-pushed the bring-typeof-switch-inline-with-if branch from b12ed40 to b63b40b Compare October 10, 2018 20:02
@weswigham
Copy link
Member

@typescript-bot test this but again since it was edited (it passed before, so it should be fine, but just in case~)

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 10, 2018

Heya @weswigham, I've started to run the extended test suite on this PR at b63b40b. You can monitor the build here. It should now contribute to this PR's status checks.

@weswigham weswigham merged commit abce9ae into microsoft:master Oct 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants