错误 'this' 到局部变量的意外别名 - typescript lint 错误
error Unexpected aliasing of 'this' to local variable - typescript lint error
如何解决这个 typescript lint 错误?
const track: Track = this;
我在这一行遇到错误
error Unexpected aliasing of 'this' to local variable @typescript-eslint/no-this-alias
代码-
import {trackMutePublisher} from '../Events/index';
// eslint-disable-next-line no-shadow
export enum TrackStatus {
ENDED = 'ended',
LIVE = 'live',
}
// eslint-disable-next-line no-shadow
export enum TrackKind {
AUDIO = 'audio',
VIDEO = 'video',
}
export interface TrackInterface {
ID: string;
kind: TrackKind;
status: TrackStatus;
muted: boolean;
label: string;
stop(): void;
}
/** @public */
export class Track implements TrackInterface {
ID: string;
kind: TrackKind;
status: TrackStatus;
muted: boolean;
label: string;
#mediaStreamTrack: MediaStreamTrack;
constructor(mediaStreamTrack: MediaStreamTrack) {
this.ID = mediaStreamTrack.id;
this.kind = mediaStreamTrack.kind as TrackKind;
this.status = mediaStreamTrack.readyState as TrackStatus;
this.muted = mediaStreamTrack.muted;
this.label = mediaStreamTrack.label;
this.#mediaStreamTrack = mediaStreamTrack;
this.#mediaStreamTrack.onmute = (event) => {
const track: Track = this;
trackMutePublisher(event, track);
};
}
/**
* Tells the browser that the track source is no longer needed. Updates status to ENDED.
*
* @public
*/
stop(): void {
this.#mediaStreamTrack.stop();
this.status = TrackStatus.ENDED;
}
}
我在堆栈溢出中经历了这个 post 但无法解决这个问题。我也不想忽略错误,因为我无法更改大部分逻辑。
我通过更改下面提到的代码解决了这个问题。
来自-
this.#mediaStreamTrack.onmute = (event) => {
const track: Track = this;
trackMutePublisher(event, track);
};
到-
this.#mediaStreamTrack.onmute = (event) => {
// using arrow function which should bind to this from outer scope track
trackMutePublisher(event, this);
};
遵循此 link 并得到修复 -
如何解决这个 typescript lint 错误?
const track: Track = this;
我在这一行遇到错误
error Unexpected aliasing of 'this' to local variable @typescript-eslint/no-this-alias
代码-
import {trackMutePublisher} from '../Events/index';
// eslint-disable-next-line no-shadow
export enum TrackStatus {
ENDED = 'ended',
LIVE = 'live',
}
// eslint-disable-next-line no-shadow
export enum TrackKind {
AUDIO = 'audio',
VIDEO = 'video',
}
export interface TrackInterface {
ID: string;
kind: TrackKind;
status: TrackStatus;
muted: boolean;
label: string;
stop(): void;
}
/** @public */
export class Track implements TrackInterface {
ID: string;
kind: TrackKind;
status: TrackStatus;
muted: boolean;
label: string;
#mediaStreamTrack: MediaStreamTrack;
constructor(mediaStreamTrack: MediaStreamTrack) {
this.ID = mediaStreamTrack.id;
this.kind = mediaStreamTrack.kind as TrackKind;
this.status = mediaStreamTrack.readyState as TrackStatus;
this.muted = mediaStreamTrack.muted;
this.label = mediaStreamTrack.label;
this.#mediaStreamTrack = mediaStreamTrack;
this.#mediaStreamTrack.onmute = (event) => {
const track: Track = this;
trackMutePublisher(event, track);
};
}
/**
* Tells the browser that the track source is no longer needed. Updates status to ENDED.
*
* @public
*/
stop(): void {
this.#mediaStreamTrack.stop();
this.status = TrackStatus.ENDED;
}
}
我在堆栈溢出中经历了这个 post 但无法解决这个问题。我也不想忽略错误,因为我无法更改大部分逻辑。
我通过更改下面提到的代码解决了这个问题。
来自-
this.#mediaStreamTrack.onmute = (event) => {
const track: Track = this;
trackMutePublisher(event, track);
};
到-
this.#mediaStreamTrack.onmute = (event) => {
// using arrow function which should bind to this from outer scope track
trackMutePublisher(event, this);
};
遵循此 link 并得到修复 -