-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Private members used only via []
notation trigger --noUnusedLocals
error
#14137
Comments
It could reasonably be argued that this is not an obvious or perhaps common use case. And indeed, this can be worked around. Obviously, this example is contrived in the extreme. And yet, I would maintain that in some cases it may be a reasonable use case which obeys (I believe) all the rules of the language, and ought not trigger a compiler error. |
It does not work in a more simple case like |
I have encountered this issue. In my case, I use type ParserState = "A_STATE" | "B_STATE" | "Z_STATE"
class Parser {
private state: ParserState
constructor() {
this.state = "A_STATE"
}
parse() {
while (this.state !== "Z_STATE") {
this.state = this[this.state]()
}
}
private A_STATE(): ParserState { /* .... */ } // 'A_STATE' declared but never used.
private B_STATE(): ParserState { /* .... */ } // 'B_STATE' declared but never used.
private Z_STATE(): ParserState { /* .... */ } // 'Z_STATE' declared but never used.
} |
I have a similar problem like mysticatea... @injectable()
export class MasterHandler {
@inject(TypeKeys.AHandler) private _aHandler: Types.AHandler;
@inject(TypeKeys.BHandler) private _bHandler: Types.BHandler;
async process(request: Request): Promise<Response> {
return await this[`_${request.scope}Handler`][request.action](request.payload);
}
} However, getting forced to remove some intentional unused locals like specific imports, I decided to use
To overcome the need of activated type info for |
+1 I opt for improving this as well, because the the Here is an example of a class converting a JSON schema into a valid elasticsearch mapping, that only serves the purpose of demonstration, in a real world, I wouldn't actually implement it as a class. Example: class JsonToEsConverter {
private _esMapping: EsMap = {}
constructor(private _schema: JSONSchema) {}
public get esMapping(): EsMap {
if (_.isEmpty(this._esMapping)) {
this.buildEsMapping();
}
return this._esMapping;
}
private buildEsMapping(): void {
for (const prop in this._schema.properties) {
this._esMapping[prop] = this[`${_schema.properties[prop]}ToEsMap](_schema.properties[prop]);
}
}
private stringToEsMap(stringProp..){}
private integerToEsMap(intProp..){}
private arrayToEsMap(arrayProp...) {}
} This is actually nice and concise. However, the compiler does not understand, that the class JsonToEsConverter {
private _esMapping: EsMap = {};
private _converterMap: {[converterName: string]: Function};
constructor(private _schema: JSONSchema) {
this._converterMap = {
stringToEsMap: this.stringToEsMap.bind(this);
integerToEsMap: this.integerToEsMap.bind(this);
arrayToEsMap: this.arrayToEsMap.bind(this);
}
}
public get esMapping(): EsMap {
if (_.isEmpty(this._esMapping)) {
this.buildEsMapping();
}
return this._esMapping;
}
private buildEsMapping(): void {
for (const prop in this._schema.properties) {
this._esMapping[prop] = this[`${_schema.properties[prop]}ToEsMap](_schema.properties[prop]);
}
}
private stringToEsMap(stringProp..){}
private integerToEsMap(intProp..){}
private arrayToEsMap(arrayProp...) {}
} Which doesn't really help. Or worse, move everything to static methods, which creates the same problems as the ones with a separate module |
TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)
Version 2.1.6
Code
Expected behavior:
Using a private member via
[]
notation should not trigger--noUnusedLocals
errors.Actual behavior:
A private member used only via
[]
triggers--noUnusedLocals
errors.The text was updated successfully, but these errors were encountered: