-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
No apparent way to prevent redundant type checking against already-narrowed types? #15211
Comments
@axefrog Actually there is a way to do this. It is a bit of a hack but it works. function isNotNull(value: {}): value is never;
function isNotNull<T>(value: T | null): value is T;
function isNotNull<T>(value: T | null): value is T {
// tslint:disable-next-line:no-null-keyword
return value !== null;
}
declare const value: string | null;
if (isNotNull(value)) {
if (isNotNull(value)) {
// error on usage of value.
}
} It is not exactly the same as receiving an error on the second call but practically it comes close. |
@aluanhaddad Heh, thanks, I'll give that a go. Might make a suitable stopgap solution in the mean time. |
Doesn't quite work in this example: export function isNotNull(value: {}): value is never;
export function isNotNull<T>(value: T|null): value is T;
export function isNotNull<T>(value: T|null): value is T {
return value !== null;
}
function printNumber(n: number): void {
console.log(n);
}
const n = Math.random();
if(isNotNull(n)) {
printNumber(n); // no error - apparently it's ok to use `never` in this way.
} |
Related?: #15211 |
Update: Partial/interim workaround is to enable |
Do not think there is a way to make this work. in a sense you want to specify an overload and then make it an error. the language has no constructs to do such a thing. |
@mhegazy I don't want to specify an overload; not sure if you were responding to @aluanhaddad 's workaround or the question that preceded it? (the latter being what I was actually asking). To recap, I'd like the compiler to recognise the redundancy of passing a value to a type-narrowing function where the narrowed type is already of the type being narrowed to. // as per the return value, the compiler knows this function is designed to narrow the type:
function isNotNull<T>(value: T|null): value is T {
return value !== null;
}
function printNumber(n: number): void {
console.log(n);
}
const x = 3;
// I want an error or warning below. It's pointless narrowing an already-narrowed type:
if(isNotNull(x)) {
printNumber(3);
} Not sure how that relates to overloading? |
but you still want |
I'm not too familiar with TypeScript internals, but from what I can see the compiler knows, when it encounters a call to |
The |
@mhegazy Could you clarify why you're not sure? As per the original question and what @fatcerberus said, the warning specifically relates to redundancy. The compiler can see that the return type is |
Looks like this the same request as #14977 |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.3.0-rc
I'm doing a whole lot of refactoring at the moment and going around trying to find places where, due to some structural changes in my application, I no longer need to be calling my
isNotNull
function, which I use to narrow the type to a non-null value. The narrowing part is fine, but I'd really like to only be allowed to call it if the argument could at least possibly be a nullable type, according to the type system. If the type system already knows the value is not null, I'd like it to prevent me from passing it to the type-narrowing function in the first place.Code
Because this redundancy is apparently invisible to the compiler, I can't simply work through the error list to fix the places where it matters, and instead need to look through all calls to the function and manually analyse the surrounding code to determine if I still need the check.
The text was updated successfully, but these errors were encountered: