angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

AOT - class which implements interface - "property does not exist on type"

Open bmxmaga opened this issue 9 years ago • 5 comments

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

Versions.

@angular/cli: 1.1.2 node: 7.6.0 os: linux x64 @angular/animations: 4.1.2 @angular/cli: 1.1.2 @angular/common: 4.1.2 @angular/compiler: 4.1.2 @angular/compiler-cli: 4.1.2 @angular/core: 4.1.2 @angular/forms: 4.1.2 @angular/http: 4.1.2 @angular/material: 2.0.0-beta.6 @angular/platform-browser: 4.1.2 @angular/platform-browser-dynamic: 4.1.2 @angular/platform-server: 4.1.2 @angular/router: 4.1.2

Repro steps.

  1. Create an interface with a property, e.g.:
interface UserModel {
  surname: string;
}
  1. Create an empty class (with no property) which implements that interface, e.g.:
class User implements UserModel {}
  1. Create a component with a template and use the class'es property in the template, e.g.:
@Component({
  template: '<span>{{user.surname}}</span>'
})
export class UserInfoComponent {
  @Input() user: User;
}
  1. Build the application with -prod option using ng build -prod

The log given by the failure.

ERROR in ng:///home/.../user-info.component.ts (2,21): Property 'surname' does not exist on type 'User'.

Desired functionality.

The app is build properly, as without the -prod option.

Mention any other details that might be useful.

I could refer to the interface directly in the component but according to this bug, I can't.

bmxmaga avatar Jun 16 '17 09:06 bmxmaga

The error shown is a legitimate error. The interface has not been actually implemented within the class.

Please see the TypeScript interface documention ("Class Types" section) for further details: https://www.typescriptlang.org/docs/handbook/interfaces.html

clydin avatar Jun 16 '17 09:06 clydin

You mean as long as the class doesn't provide all the properties of the interface it will silently not implement it? I mean, I would at least expect some critical error from TypeScript, something like "class User does not implement property surname of UserModel...", right?

bmxmaga avatar Jun 16 '17 09:06 bmxmaga

@bmxmaga yes, by writing class User implements UserModel {} you are saying: "i am going to implement all the members of the UserModel in User manually". This is just a hint for your IDE. It does not get those automatically.

see https://www.typescriptlang.org/docs/handbook/interfaces.html#class-types

About error: looks like pure Typescript compiler has better wording:

Class 'User' incorrectly implements interface 'UserModel'. Property 'surname' is missing in type 'User'.

see https://www.typescriptlang.org/play/#src=interface%20UserModel%20%7B%0D%0A%20%20surname%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Aclass%20User%20implements%20UserModel%20%7B%7D

tytskyi avatar Jun 16 '17 10:06 tytskyi

Ok, I get it :) Thank you for the explanation.

bmxmaga avatar Jun 16 '17 10:06 bmxmaga

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.