-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
π Search Terms
Type Assertion type Narrowing
π Version & Regression Information
- This changed between versions 5.3.3 and 5.4.3
β― Playground Link
π» 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

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
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue