The question I'd ask is "what is the goal of your code?"
If the goal is to signal an error/nothing state - then a "nullish" value is truly what you want.
Given that your lint config (for whatever reason) bans the undefined
value, then why not just use null
?
public static getInstanceByReference(
vueReference: Vue | Element | Array<Vue> | Array<Element>
): MyComponent | null {
if (vueReference instanceof MyComponent) {
return vueReference;
}
return null;
}
If you really want the undefined
value without explicitly referencing undefined
, then as per the no-undefined
docs, you can just use the void
operator:
public static getInstanceByReference(
vueReference: Vue | Element | Array<Vue> | Array<Element>
): MyComponent | undefined {
if (vueReference instanceof MyComponent) {
return vueReference;
}
return void 0;
}
As an aside.
If we change the return value signature to MyComponent | void, we get the no-invalid-void-type rule violation of @typescript-eslint.
void
should not be used in a union type. It's pretty a non-sensical type to say "this function returns something or returns absolutely nothing". It contradicts things and causes TS to act somewhat counter-intuitively (i.e. like disabling the missing return statement check).
This is why the no-invalid-void-type
rule reports on MyComponent | void
, and is no doubt the reason that your lint config has the rule turned on.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…