簡體   English   中英

lint錯誤使代碼發生變化但不確定如何繼續

[英]lint errors making the code change but not sure how to proceed

更新。

我更新了這樣的代碼,但我仍然得到以下錯誤

錯誤類型'String'不能分配給'string'類型。 'string'是一個原語,但'String'是一個包裝器對象。 如果可能,請更喜歡使用'string'

更新的代碼

    public sportsService: string;

  constructor( sportsService: String) {
    this.sportsService = sportsService;
  }
  • 我是tslint和打字稿的新手。
  • 我試圖修復此錯誤。 屬性'waterService'不能在構造函數中聲明

  • 發生在這一行構造函數(public sportsService:SPORTSService){}

  • 你能告訴我如何解決它嗎?

  • 我做了一些研究,但沒有找到解決方案。
  • 提供以下代碼。
  • 甚至看了這個鏈接並嘗試但無法繼續

https://github.com/Microsoft/tslint-microsoft-contrib

屬性'waterService'不能在構造函數中聲明

/** Trees for contract and title. */
import {
  Component,
  Inject,
  OnInit,
  EventEmitter,
  ViewChild,
  Input,
  Output
}
from '@angular/core';
import {
  SPORTSService
}
from '../../services/sports.service';
import {
  KendoGridComponent
}
from '../grid/grid.component';
import {
  KendoDialog
}
from '../shared/kendoDialog/kendodialog';
import {
  ProgressCircle
}
from '../shared/progress/progress-circle';

declare let $: any;



@Component({
  selector: 'player',
  template: `<div id="windowcontainer"></div>`
})

export class player implements OnInit {

  private henInfoPopUpWindow;
  private airDate;
  private airTime;
  private showTitle;


  @Input() kendoCommandObj: any;




  constructor(public sportsService: SPORTSService) {}

  private kendocommand = {
    edit: {
      createAt: "bottom"
    },
    group: false,
    reorder: true,
    disableFreeze: true,
    resize: true,
    sort: true,
    autoBind: true,
    filter: false,
    pager: {
      messages: {
        //display: "Showing {0} to {1} of {2} entries"
      }
    },
    model: {},
    columns: [],
    pagesize: 50,
    getComponentUrl: "swimming",
    searchFields: [],
    mandatoryFields: [],
    saveStatus: false
  };


  @Output() applyAPTInfo: EventEmitter < any > = new EventEmitter < any > ();
  @Output('mouseCount') getTreeEvent = new EventEmitter<number>();

  ngOnInit() {
    this.mouseType = 'hen';
    let that = this;
    let attributes=this.sportsService.getSeesionStorageValue();

TSLint有一條規則禁止使用構造函數屬性(即當您使用諸如publicprivate類的訪問者關鍵字為構造函數參數添加前綴時)。

"no-parameter-properties": true

在我看來,這是一個荒謬的規則,因為我更願意看到:

class Example {
    constructor(private name: string) {}
}

我不想看到:

class Example {
    private name: string;
    constructor(name: string) {
        this.name = name;
    }
}

換句話說, 停止手動分配TypeScript構造函數參數

固定

只需關閉規則:

"no-parameter-properties": false

修復2

對於第二個錯誤,您已在類型注釋中使用了String接口。 對此的修復是遵循TSLint規則,因為它是合理的,並使用stringnumberboolean等在類型注釋中使用基元類型。

let x: String; // <-- interface String

let y: string; // <-- primitive type string

另外,在普通的JavaScript中也遵循規則,在這里使用文字而不是對象是最好的:

let x = 'A literal string value here'; // <-- literal

let y = new String('String object'); // <-- object

這是一個很好的tslint規則,它意味着總是使用原始類型名稱而不是原始類型構造函數

你應該替換:

  • Stringstring
  • Booleanboolean
  • Numbernumber

如果您對不同之處感興趣,請查看以下鏈接:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM