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

type assertions affect the type narrowing of the subsequent code #58050

Closed
a145789 opened this issue Apr 3, 2024 · 6 comments Β· Fixed by #58083
Closed

type assertions affect the type narrowing of the subsequent code #58050

a145789 opened this issue Apr 3, 2024 · 6 comments Β· Fixed by #58083
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@a145789
Copy link

a145789 commented Apr 3, 2024

πŸ”Ž Search Terms

Type Assertion type Narrowing

πŸ•— Version & Regression Information

  • This changed between versions 5.3.3 and 5.4.3

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.3#code/JYWwDg9gTgLgBAbzjAnmApnAwhcEB26+MANMmpgAoA2ArgObD5wC+cAZlLnAOQButdDwBQwgPRjyGbHAC82XJELEA2j3wBDEEIC6w9AA9IsKZhx5lMAOrAYACwCS+AM4wN1agB4AKgD45cN5wAGRwNAxM+kbQ8KjSNvZOru7U4YzM8kgA+mBQ6OzABgD8AFxwrlBM9Kyihsbw7LT4AMYwwARwAO62ji5uHp5YcIYwRAAmzgoWRKSBwwaj+BNwCb3JHmlMvgAUwnBwzYoEM2VDAD7YKjoke8gaUPToMKWBwgCUZRVViLeHfQdHSyrJL9agBbZuB5POBFIoA6bEN5wDSTcxKGbAvopHy+X4EVzw9HESbyACCUCgGhQADpgM5yZSUNtDgiYEi4SyifAyipOcdiHo8f8kJptKwAnzLM4VAAGHRwW7Adhwbai9BIhC3fZ5GC0KDMNW3FiibVPPXMABEFuExuEQA

πŸ’» Code

import { type Component, type Plugin } from 'vue'

// type C = Component['name']
export type ComponentWithInstall<T> = T & Plugin
export type WithInstallPlugin = { _prefix?: string }

export function withInstall<C extends Component, T extends WithInstallPlugin>(
  component: C | C[],
  target?: T
): string {
  const componentWithInstall = (target ?? component) as ComponentWithInstall<T>
  const components = Array.isArray(component) ? component : [component]

  const { name } = components[0] 
  if (name) {
    return name
  }

  return ""
}

πŸ™ Actual behavior

image
the name property is underlined in red, indicating an error.

Property 'name' does not exist on type 'Component'.(2339)

The type C = Component['name'] is valid, as the name property exists within the Component type. When I remove const componentWithInstall = (target ?? component) as ComponentWithInstall<T>, everything returns to normal. Alternatively, if I uncomment type C = Component['name'], even though it's not used anywhere, the error disappears. Type assertions should not affect the type narrowing of subsequent code, but an error occurs. This code worked fine in version 5.3.3, but started to error in version 5.4.3.

πŸ™‚ Expected behavior

Ts don't report the error.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

This is a crash

This is no crash. Crash means the compiler crashes, not you receiving an error.

This changed between versions 5.3.3 and 5.3.4

You probably mean 5.4.3? There is no version 5.3.4.

@a145789
Copy link
Author

a145789 commented Apr 3, 2024

This is a crash

This is no crash. Crash means the compiler crashes, not you receiving an error.

This changed between versions 5.3.3 and 5.3.4

You probably mean 5.4.3? There is no version 5.3.4.

Excuse my carelessness; I have made revisions.

@ahejlsberg ahejlsberg self-assigned this Apr 3, 2024
@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Apr 3, 2024
@ahejlsberg
Copy link
Member

That's very strange. I'll take a look.

@jcalz
Copy link
Contributor

jcalz commented Apr 3, 2024

@ahejlsberg
Copy link
Member

ahejlsberg commented Apr 3, 2024

The issue here appears to be inconsistent caching of union and intersection property lookups, specifically the caching logic in getUnionOrIntersectionProperty and its handling of the skipObjectFunctionPropertyAugment parameter. It doesn't have anything to do with narrowing.

@ahejlsberg
Copy link
Member

I see what's happening. For various reasons we have two caches for resolved property lookups on unions and intersections, one that includes property augmentations from Object and Function, and one that doesn't. Logic was added in #54753 to propagate entries from the non-augmented cache to the augmented cache (see here), but that's incorrect when properties are marked with CheckFlags.Partial. I'll put up a PR with a fix.

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants