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

TS2339: Property 'tooltip' does not exist on type '{ label: string; } | { label: string; tooltip: string; }'. #19186

Closed
retorquere opened this issue Oct 14, 2017 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@retorquere
Copy link

TypeScript Version: [email protected]

Code

const cells = [
  { label: '1' },
  { label: '2', tooltip: '2' },
  { label: '3', tooltip: '3' },
  { label: '4' },
  { label: '5' },
  { label: '6' },
]
for (const cell of cells) {
  if (cell.tooltip) console.log(cell.tooltip)
}

Expected behavior:
That this compiles without error; the error message seems to indicate TS sees a call to cell.tooltip as valid but then forbids it in the second part of the error message.

Actual behavior:

tt.ts(10,12): error TS2339: Property 'tooltip' does not exist on type '{ label: string; } | { label: string; tooltip: string; }'.
  Property 'tooltip' does not exist on type '{ label: string; }'.
tt.ts(10,38): error TS2339: Property 'tooltip' does not exist on type '{ label: string; } | { label: string; tooltip: string; }'.
  Property 'tooltip' does not exist on type '{ label: string; }'.
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Oct 14, 2017

Looks like a duplicate of #1260. You can get around this with a user-defined type guard or a type-assertion.

@retorquere
Copy link
Author

I eyed that issue but couldn't make out whether it was the same problem; If it's a dup feel free to close. The suggestions in #1260 do allow compilation, but is this issue planned for a fix in a 2.x release? #1260 has been open for a while.

@ajafff
Copy link
Contributor

ajafff commented Oct 14, 2017

The easy fix for your problem is a type annotation:

const cells: {label: string, tooltip?: string} = [
  { label: '1' },
  { label: '2', tooltip: '2' },
  { label: '3', tooltip: '3' },
  { label: '4' },
  { label: '5' },
  { label: '6' },
]

@retorquere
Copy link
Author

Yeah, that's what I settled on in the end.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Oct 16, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Oct 30, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Oct 30, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants