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

Imported interface behaves differently from localy available interface #24065

Closed
ackvf opened this issue May 11, 2018 · 9 comments
Closed

Imported interface behaves differently from localy available interface #24065

ackvf opened this issue May 11, 2018 · 9 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@ackvf
Copy link

ackvf commented May 11, 2018

TypeScript Version: 2.9.0-dev.20180511

Code

// types.d.ts
export interface MyImportedItem {
  id: number
  another: string
  param1: string
}


// firebase.actions.ts

interface MyItem {
  id: number
  another: string
  param1: string
}

getFirebaseRef().push({ // error
  id,
  another: data.another,
  param: data.someValue,
  // param2: 'X',
} as MyItem)

getFirebaseRef().push({ // no error
  id,
  another: data.another,
  param: data.someValue,
  // param2: 'X',
} as MyImportedItem)

Error:

Type '{ id: any; another: any; param: any; }' cannot be converted to type 'MyItem'.
      Property 'param1' is missing in type '{ id: any; another: any; param: any; }'.

Expected behavior:
Expected is consistent behavior for local and imported interface.
Expected is that missing and invalid props are correctly reported by the IDE.

Actual behavior:

image

@RyanCavanaugh
Copy link
Member

I see an error in your second call - there's a red squiggle on id

@DanielRosenwasser DanielRosenwasser added the Needs More Info The issue still hasn't been fully clarified label May 11, 2018
@ackvf
Copy link
Author

ackvf commented May 12, 2018

That is unrelated, I fixed it, id was not defined. When I swap the two interfaces, the error moves, so it is indeed tied to that particular interface.
interface-error

@mhegazy
Copy link
Contributor

mhegazy commented May 12, 2018

Can u share a self contained repro for us to look at?

@ackvf
Copy link
Author

ackvf commented May 12, 2018

I take it back, I had MyImportedItem imported twice, not sure how that happened. Anyway, I still have one issue regarding "Expected is that missing and invalid props are correctly reported by the IDE.": I don't get errors when my object is not complete or contains invalid keys, but I do get error that a key (param1) is missing if I introduce another key (param2) that is not part of the interface.

No error
image

Error param1 is missing
image


Moreover, can somebody please explain to me why the const declaration here gives an error correctly, while using it with as in the push does not?
image

Here I get that param1 is missing in the push.
image


I am using it like this so that I get hints for the object I am pushing.

image

Is there a better approach to get hints but avoid declaring the object beforehand? Thank you

@mhegazy
Copy link
Contributor

mhegazy commented May 14, 2018

@ackvf it is very hard to debug issues like that using screenshots.. we would appreciate it if you would include a self-contained code sample that provides a reproduction of the issue you are facing..

@ackvf
Copy link
Author

ackvf commented May 15, 2018

I am sorry, I included the code at the top and I thought that it is self-contained enough. My repo is just an experimental repo with so few files that I did not think they would affect the behaviour of the snippet in any way. Anyway, my first issue was due to a mistake in code and my second issue (that from my last comment 2 days ago) was more like a question than an issue. I thought that it is actually behaving correctly but in a way I was not aware of.

This is really it...

interface MyItem {
  another: string
  id: number
  param1: string
}

const getFirebaseRef = (): any => void


getFirebaseRef().push({ // no error here that param1 is missing
  another: 'another',
  id: 42,
  // param1: 'param1',
  // param2: 'X',
} as MyItem)

const myItem: MyItem = { // error param1 is missing
  another: 'another',
  id: 42,
  // param1: 'param1',
  // param2: 'X',
}

getFirebaseRef().push(myItem)
interface MyItem {
  another: string
  id: number
  param1: string
}

const getFirebaseRef = (): any => void


getFirebaseRef().push({ // error param1 is missing
  another: 'another',
  id: 42,
  // param1: 'param1',
  param2: 'X', // no error for param2
} as MyItem)

const myItem: MyItem = { // no error that param1 is missing
  another: 'another',
  id: 42,
  // param1: 'param1',
  param2: 'X', // error may only specify known properties
}

getFirebaseRef().push(myItem)

@ackvf
Copy link
Author

ackvf commented May 15, 2018

My question was if it is possible to get hints for the pushing object without having to declare the object beforehand.

I am looking for something like this - to specify the type of the inline object I am pushing

image

so that when I ctrl+space I get hints and warnings, but instead I do get hints, but no warnings.

@ackvf
Copy link
Author

ackvf commented May 15, 2018

Obviously, I cannot use this to specify that the object I am pushing is MyItem getFirebaseRef().push({}: MyItem) // parsing error: ',' expected

Using type casting also -by nature- does something else than I want, so if there was something like type guards for inline objects/values, that would be great:

getFirebaseRef().push({} is MyItem)
-> now I get hints for object properties
-> now I get errors for missing and unknown properties

@RyanCavanaugh
Copy link
Member

@ackvf #7481

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

4 participants