While I like the use of no-null-keyword, it gets annoying when I need to interface with 3rd party libraries/DOM/etc. that has a null type. I need to pepper in disable-rule all over the place which defeats the purpose. So far I see two options.
ban-types to work with nullI tried following the instructions here to ban the null type but allow use of the null value. Maybe I'm not reading the rule right and it's not for intended use that I desire? Also I see this as a short term solution because I can see scenarios to cheat the linter.
no-null-keywordWhat would be nice is to be able to prevent from introducing any more nulls from a developer perspective, meaning tslint should be smart in knowing that I am not trying to introduce nulls but rather be forced to deal with something that is null.
For instance, this could (depends on #2782) not raise a no-null-keyword violation:
const x = document.querySelector('abc')
if(x !== null) {
doSomething(x)
}
Another example that should be allowed:
// 3rd party declared set(key: string, value: string | null)
set('name', null)
While not as common, I also have come across some odd edge cases with generics such as:
// 3rd party declared:
// type Base = Document | Window | null
// class SomeClass<Base> {}
class ExtraClass extends SomeClass<null> {
}
I can see this getting to interesting cases though because then it could flag something like this:
const value = boolCondition ? 'true' : null
but allow this:
const value = boolCondition ? 'true' : null
// 3rd party declared set(key: string, value: string | null)
set('name', value)
and throw a lint error on the function declaration because it's in the project and not a 3rd party library:
const value = boolCondition ? 'true' : null
// 3rd party declared set(key: string, value: string | null)
set('name', value)
doThirdThing(value)
function doThirdThing(value: string | null) {
// ...
}
MongoDB comes to mind
@jupl ban-types only works for type references right now.That means it covers every type that is not a keyword or literal:
// these don't work right now
// keywords
let v: null;
let v: string;
let v: number;
let v: undefined;
let v: boolean;
let v: void;
let v: any;
let v: never;
let v: object;
// literals
let v: 1;
let v: 'a';
let v: {};
let v: true;
// signatures
let v: () => void;
let v: new() => any;
// in all of the types below, you can only ban 'T'
let v: T[]; // array types
let v: {[K in T]: T}; // mapped types
let v: typeof T; // type query
let v: keyof T; // type operator
let v: T['prop']; // indexed access type
// these work as expected
let v: String;
let v: Number;
let v: Object;
let v: Function;
let v: Boolean;
let v: MyType;
let v: MyNamespace.MyType.
let v: Array<T>;
This should still be an error.
const value = boolCondition ? 'true' : null
// 3rd party declared set(key: string, value: string | null)
set('name', value)
Instead you could write it as
set('name', boolCondition ? 'true' : null)
What about Object.create(null);
A pretty common pattern, where replacing null with undefined wont work.
Also, it should be possible to return null if the function definition in 3rd party library allows returning null. For example, in React conditional rendering, only null is allowed and returning undefined would throw an error.
Also, i need to work with on ORM lib that waits a JSON object to find() where undefined !== null
How could I solve this?
entity.find({ endAt: null }) --> tslint oblies me to use undefined that in fact won´t work as expected
Just find ORM lib has internal IsNull() function to superseed
I have a lot of case in React where null is the only allowed value: Ref, render value, ... making the no-null-keyword rule useless.
If you really have to use null, how about using a specially escaped function? e.g.
// tslint:disable
function makeNull(): null {
return null;
}
// tslint:enable
// calling 3rd party library
set('key', condition ? 'foo' : makeNull());
☠️ TSLint's time has come! ☠️
TSLint is no longer accepting most feature requests per #4534. See typescript-eslint.io for the new, shiny way to lint your TypeScript code with ESLint. ✨
It was a pleasure open sourcing with you all!
Most helpful comment
Also, it should be possible to return null if the function definition in 3rd party library allows returning null. For example, in React conditional rendering, only null is allowed and returning undefined would throw an error.