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

Show some elaboration for property not existing in union type #10256

Closed
RyanCavanaugh opened this issue Aug 10, 2016 · 4 comments
Closed

Show some elaboration for property not existing in union type #10256

RyanCavanaugh opened this issue Aug 10, 2016 · 4 comments
Labels
Committed The team has roadmapped this issue Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript

Comments

@RyanCavanaugh
Copy link
Member

I have some code like this, except there are about a dozen types instead of just three:

interface Foo {
  kind: "foo";
  name: string;
}
interface Bar {
  kind: "bar";
  name: string;
}
interface Qua {
  kind: "qua";
  length: string;
}

type FBQ = Foo | Bar | Qua;
let x: FBQ = <any>undefined;
console.log(x.name);

The error reported is

a.ts(16,15): error TS2339: Property 'name' does not exist on type 'FBQ'.

This is inactionable on a union type, especially when its consituents are hidden behind an alias. We should show something like

a.ts(16,15): error TS2339: Property 'name' does not exist on type 'FBQ'.
   Property 'name' does not exist on type 'Qua'
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Aug 10, 2016
@yahiko00
Copy link

yahiko00 commented Aug 10, 2016

Yes please! More generally, there are some rooms to improvement in compiler error messages.

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Committed The team has roadmapped this issue Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". and removed In Discussion Not yet reached consensus labels Aug 22, 2016
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 2.1 milestone Aug 22, 2016
@RyanCavanaugh
Copy link
Member Author

Accepting PRs / committed for 2.1. Show an elaboration for the first type lacking the property (for whatever definition of 'first' falls out of the implementation)

@ojsheikh
Copy link
Contributor

Should there also be an elaboration when one of the constituent types has the property marked as private or protected and we are in a public context? Currently the code:

class Default {
    member: string;
}
class Private {
    private member: number;
}

var DP: Default | Private;
DP.member;

gives the error:

test.ts(9,4): error TS2339: Property 'member' does not exist on type 'Default | Private'.

This could perhaps be changed to:

test.ts(9,4): error TS2339: Property 'member' does not exist on type 'Default | Private'.
  Property 'member' is private and only accessible within class 'Private'.

I have adjusted checkPropertyAccessExpressionOrQualifiedName to show an elaboration when a subtype of a union lacks a property and will open a PR once I've finished adding tests. It should not be difficult to also show an elaboration when a subtype has the property marked under a stricter access qualifier.

@mhegazy mhegazy assigned ghost Aug 30, 2016
@mhegazy mhegazy unassigned ghost Sep 13, 2016
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Sep 13, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Sep 13, 2016

thanks @oijazsh !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Committed The team has roadmapped this issue Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants