Skip to content

Assertion function & Type guards for Record types broken on 5.0 #53379

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

Closed
JeanMeche opened this issue Mar 20, 2023 · 1 comment Β· Fixed by #53388
Closed

Assertion function & Type guards for Record types broken on 5.0 #53379

JeanMeche opened this issue Mar 20, 2023 · 1 comment Β· Fixed by #53388
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Recent Regression This is a new regression just found in the last major/minor version of TypeScript.

Comments

@JeanMeche
Copy link
Contributor

JeanMeche commented Mar 20, 2023

πŸ”Ž Search Terms

  • asserts
  • assertion function
  • type guard
  • record

πŸ•— Version & Regression Information

Worked on 4.9, broken since 5.0

Probably introduced by #52984

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

declare function isObject(o: unknown): o is Record<string, any>

declare function assertObject(o: unknown): asserts o is Record<string, any>  

let value: {} = null!;
if(isObject(value)) {
    value.anything // error since 5.0.2
}

assertObject(value)

value.anything // error since 5.0.2

πŸ™ Actual behavior

Property 'anything' does not exist on type '{}'.(2339)

πŸ™‚ Expected behavior

No error, narrowing / assertion should work fine.

@ahejlsberg ahejlsberg self-assigned this Mar 20, 2023
@ahejlsberg
Copy link
Member

ahejlsberg commented Mar 20, 2023

Hmm, looks like the strict subtype relation considers {} and Record<string, any> to be mutual subtypes. That's an oversight and apparently we don't have test coverage for that combination. It should be the case that {} is a strict supertype of Record<string, any>, similar to {} being a strict supertype of Record<string, unknown> (for which we do have test coverage).

@ahejlsberg ahejlsberg added Bug A bug in TypeScript Breaking Change Would introduce errors in existing code labels Mar 20, 2023
@ahejlsberg ahejlsberg added this to the TypeScript 5.0.3 milestone Mar 20, 2023
@ahejlsberg ahejlsberg added Recent Regression This is a new regression just found in the last major/minor version of TypeScript. and removed Breaking Change Would introduce errors in existing code labels Mar 20, 2023
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Recent Regression This is a new regression just found in the last major/minor version of TypeScript.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants