Skip to content

Improve type checking to include checking model binding #16952

Closed

Description

@100cm

import {Component} from "@angular/core";
import {CommonService} from "../service/common.service";
@component({
selector: 'testb',
template: '

<button (click)="aa()">输出 <button (click)="bb()">切换' +
' <input [(ngModel)]="myh.n" #item (change)="cc(item.value)"/>
',
})
export class TestB {

constructor(private commonService: CommonService) {

}


private mynumber: number;

private myh: myObjct = new myObjct(3);

aa() {
    console.log(this.commonService.hello)
}

bb() {
    this.commonService.hello = "test  b hello"
}

cc(v: number) {
    this.myh.n =v
    console.log(this.myh)
   console.log( this.myh.n)
    console.log(JSON.stringify(this.myh))
}

}

class myObjct{
private _n:number

constructor(n: number) {
    this._n = n;
}

get n(): number {
    return this._n;
}

set n(value: number) {
    this._n = +value;
}

}

i must use +value to make my type to be number .or it will be string.

Activity

  1. trotyl commented on May 23, 2017

    @trotyl
    Contributor

    [()] syntax is just a syntax sugar of property binding and event binding. The generalized problem here is that Angular does not provide type checking for event bindings, nothing special to ngModelChange.

    You should fill the issue template as a feature request, as it's expected at design level, rather than being a BUG. But this may not be possible for current mechanism, as it's just string comparison for event names, the source of the event is not being tracked during compilation.

  2. 100cm commented on May 24, 2017

    @100cm
    Author

    i think when angular 2 set a property of object , its will not check the type, how to check this ?

  3. trotyl commented on May 24, 2017

    @trotyl
    Contributor

    In the binding (ngModelChange)="myh.n = $event", myh.n is the property you want to bind to, it's typed properly, but $event is always declared as any.

    Theoretically the type of $event may be determined by the generic type of EventEmitter, but there may still be other conditions, like native DOM events and events provided by other EVENT_MANAGER_PLUGINS.

    What's more for ngModelChange is that, there is no generic type used in the EventEmitter (in ng_model.ts), the expected declared type it's determined by the NG_VALUE_ACCESSOR, not the NgModel.

    So that's why I think this request is way to hard to implement this. Still, this is just my point of view.

  4. changed the title [-]BUG:angular2 model bind make typescript type check missed[/-] [+]Improve type checking to include checking model binding[/+] on May 25, 2017
  5. added
    featureLabel used to distinguish feature request from other issues
    on May 25, 2017
  6. trotyl commented on Jul 12, 2017

    @trotyl
    Contributor

    Possibly relates to #17483

  7. added this to the Backlog milestone on Jan 23, 2018
  8. added
    area: compilerIssues related to `ngc`, Angular's template compiler
    and removed
    area: coreIssues related to the framework runtime
    on Mar 17, 2020
  9. added
    P4A relatively minor issue that is not relevant to core functions
    on Nov 7, 2020
  10. angular-robot commented on Jun 4, 2021

    @angular-robot
    Contributor

    Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

    Find more details about Angular's feature request process in our documentation.

  11. angular-robot commented on Jun 25, 2021

    @angular-robot
    Contributor

    Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

    We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

    You can find more details about the feature request process in our documentation.

  12. alxhub commented on Jul 8, 2021

    @alxhub
    Member

    I'm going to close this as resolved, actually.

    In Ivy under strictTemplates, the type of $event is correctly determined by the generic type of EventEmitter of the output in question. However, #13721 will likely need to be resolved as well, to truly solve the problem described in this issue.

  13. angular-automatic-lock-bot commented on Aug 8, 2021

    @angular-automatic-lock-bot

    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.

  14. locked and limited conversation to collaborators on Aug 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4A relatively minor issue that is not relevant to core functionsarea: compilerIssues related to `ngc`, Angular's template compilerarea: formsfeatureLabel used to distinguish feature request from other issues

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions