'User defined guard with type predicate still returns property does not exist error

In the example below, I've created a NodeJsProject type that's a union of NodeJsApp and NodeJsPackage interfaces.

NodeJsProject and NodeJsApp both extend NodeJsProjectBase because they share common properties but it's important that they can be checked and handled separately.

interface NodeJsProjectBase {
    name: string
    packageManager: "yarn"|"npm"
}

interface NodeJsApp extends NodeJsProjectBase {
    projectType: "app"
}

export interface NodeJsPackage extends NodeJsProjectBase {
    projectType: "package"
    npmUrl: string
}

export type NodeJsProject = NodeJsApp | NodeJsPackage

isPackage() is based on the Using type predicates example in the documentation

export const isPackage = (project: NodeJsProject): project is NodeJsPackage => {
    return project.projectType === "package";
}

Now that I can check if a NodeJsProject is an NodeJsApp or a NodeJsPackage, this guard should prevent an error when I try to call project.npmUrl:

return isPackage(project) ? <div> {project.npmUrl} </div> : <div>NA</div>

However, even with this guard, calling project.npmUrl still produces the error:

TS2339: Property 'npmUrl' does not exist on type 'never'.   
The intersection 'NodeJsApp & NodeJsPackage' was reduced to 'never'
 because property 'projectType' has conflicting types in some constituents.

Working typescript playground



Solution 1:[1]

This seems to be an error specific to my application. I suspect it was a typo because it's suddenly started working. Here is a typescript playground with the working example

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ItsGeorge