-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Flag non-nullable functions in if
statements as errors (tree walk version)
#33178
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
Merged
orta
merged 5 commits into
microsoft:master
from
jwbay:nonNullableCallSignaturesTreeWalk
Sep 25, 2019
+701
−15
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
006a327
Flag non-nullable values with call expressions in `if` statements as …
jwbay 3c9e338
Only error when testing functions that are not used in the following …
jwbay 04638a1
Update user baselines
typescript-bot e6b45ad
Merge pull request #3 from typescript-bot/user-update-jwbay-20190801-…
jwbay 4c9986f
Merge master
orta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1389,18 +1389,16 @@ namespace ts { | |
// try to verify results of module resolution | ||
for (const { oldFile: oldSourceFile, newFile: newSourceFile } of modifiedSourceFiles) { | ||
const newSourceFilePath = getNormalizedAbsolutePath(newSourceFile.originalFileName, currentDirectory); | ||
if (resolveModuleNamesWorker) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like |
||
const moduleNames = getModuleNames(newSourceFile); | ||
const resolutions = resolveModuleNamesReusingOldState(moduleNames, newSourceFilePath, newSourceFile); | ||
// ensure that module resolution results are still correct | ||
const resolutionsChanged = hasChangesInResolutions(moduleNames, resolutions, oldSourceFile.resolvedModules, moduleResolutionIsEqualTo); | ||
if (resolutionsChanged) { | ||
oldProgram.structureIsReused = StructureIsReused.SafeModules; | ||
newSourceFile.resolvedModules = zipToMap(moduleNames, resolutions); | ||
} | ||
else { | ||
newSourceFile.resolvedModules = oldSourceFile.resolvedModules; | ||
} | ||
const moduleNames = getModuleNames(newSourceFile); | ||
const resolutions = resolveModuleNamesReusingOldState(moduleNames, newSourceFilePath, newSourceFile); | ||
// ensure that module resolution results are still correct | ||
const resolutionsChanged = hasChangesInResolutions(moduleNames, resolutions, oldSourceFile.resolvedModules, moduleResolutionIsEqualTo); | ||
if (resolutionsChanged) { | ||
oldProgram.structureIsReused = StructureIsReused.SafeModules; | ||
newSourceFile.resolvedModules = zipToMap(moduleNames, resolutions); | ||
} | ||
else { | ||
newSourceFile.resolvedModules = oldSourceFile.resolvedModules; | ||
} | ||
if (resolveTypeReferenceDirectiveNamesWorker) { | ||
// We lower-case all type references because npm automatically lowercases all packages. See GH#9824. | ||
|
91 changes: 91 additions & 0 deletions
91
tests/baselines/reference/truthinessCallExpressionCoercion.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
tests/cases/compiler/truthinessCallExpressionCoercion.ts(2,9): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead? | ||
tests/cases/compiler/truthinessCallExpressionCoercion.ts(18,9): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead? | ||
tests/cases/compiler/truthinessCallExpressionCoercion.ts(36,9): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead? | ||
tests/cases/compiler/truthinessCallExpressionCoercion.ts(50,9): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead? | ||
tests/cases/compiler/truthinessCallExpressionCoercion.ts(66,13): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead? | ||
|
||
|
||
==== tests/cases/compiler/truthinessCallExpressionCoercion.ts (5 errors) ==== | ||
function onlyErrorsWhenTestingNonNullableFunctionType(required: () => boolean, optional?: () => boolean) { | ||
if (required) { // error | ||
~~~~~~~~ | ||
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead? | ||
} | ||
|
||
if (optional) { // ok | ||
} | ||
|
||
if (!!required) { // ok | ||
} | ||
|
||
if (required()) { // ok | ||
} | ||
} | ||
|
||
function onlyErrorsWhenUnusedInBody() { | ||
function test() { return Math.random() > 0.5; } | ||
|
||
if (test) { // error | ||
~~~~ | ||
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead? | ||
console.log('test'); | ||
} | ||
|
||
if (test) { // ok | ||
console.log(test); | ||
} | ||
|
||
if (test) { // ok | ||
test(); | ||
} | ||
|
||
if (test) { // ok | ||
[() => null].forEach(() => { | ||
test(); | ||
}); | ||
} | ||
|
||
if (test) { // error | ||
~~~~ | ||
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead? | ||
[() => null].forEach(test => { | ||
test(); | ||
}); | ||
} | ||
} | ||
|
||
function checksPropertyAccess() { | ||
const x = { | ||
foo: { | ||
bar() { return true; } | ||
} | ||
} | ||
|
||
if (x.foo.bar) { // error | ||
~~~~~~~~~ | ||
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead? | ||
} | ||
|
||
if (x.foo.bar) { // ok | ||
x.foo.bar; | ||
} | ||
} | ||
|
||
class Foo { | ||
maybeIsUser?: () => boolean; | ||
|
||
isUser() { | ||
return true; | ||
} | ||
|
||
test() { | ||
if (this.isUser) { // error | ||
~~~~~~~~~~~ | ||
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead? | ||
} | ||
|
||
if (this.maybeIsUser) { // ok | ||
} | ||
} | ||
} | ||
|
134 changes: 134 additions & 0 deletions
134
tests/baselines/reference/truthinessCallExpressionCoercion.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
//// [truthinessCallExpressionCoercion.ts] | ||
function onlyErrorsWhenTestingNonNullableFunctionType(required: () => boolean, optional?: () => boolean) { | ||
if (required) { // error | ||
} | ||
|
||
if (optional) { // ok | ||
} | ||
|
||
if (!!required) { // ok | ||
} | ||
|
||
if (required()) { // ok | ||
} | ||
} | ||
|
||
function onlyErrorsWhenUnusedInBody() { | ||
function test() { return Math.random() > 0.5; } | ||
|
||
if (test) { // error | ||
console.log('test'); | ||
} | ||
|
||
if (test) { // ok | ||
console.log(test); | ||
} | ||
|
||
if (test) { // ok | ||
test(); | ||
} | ||
|
||
if (test) { // ok | ||
[() => null].forEach(() => { | ||
test(); | ||
}); | ||
} | ||
|
||
if (test) { // error | ||
[() => null].forEach(test => { | ||
test(); | ||
}); | ||
} | ||
} | ||
|
||
function checksPropertyAccess() { | ||
const x = { | ||
foo: { | ||
bar() { return true; } | ||
} | ||
} | ||
|
||
if (x.foo.bar) { // error | ||
} | ||
|
||
if (x.foo.bar) { // ok | ||
x.foo.bar; | ||
} | ||
} | ||
|
||
class Foo { | ||
maybeIsUser?: () => boolean; | ||
|
||
isUser() { | ||
return true; | ||
} | ||
|
||
test() { | ||
if (this.isUser) { // error | ||
} | ||
|
||
if (this.maybeIsUser) { // ok | ||
} | ||
} | ||
} | ||
|
||
|
||
//// [truthinessCallExpressionCoercion.js] | ||
function onlyErrorsWhenTestingNonNullableFunctionType(required, optional) { | ||
if (required) { // error | ||
} | ||
if (optional) { // ok | ||
} | ||
if (!!required) { // ok | ||
} | ||
if (required()) { // ok | ||
} | ||
} | ||
function onlyErrorsWhenUnusedInBody() { | ||
function test() { return Math.random() > 0.5; } | ||
if (test) { // error | ||
console.log('test'); | ||
} | ||
if (test) { // ok | ||
console.log(test); | ||
} | ||
if (test) { // ok | ||
test(); | ||
} | ||
if (test) { // ok | ||
[function () { return null; }].forEach(function () { | ||
test(); | ||
}); | ||
} | ||
if (test) { // error | ||
[function () { return null; }].forEach(function (test) { | ||
test(); | ||
}); | ||
} | ||
} | ||
function checksPropertyAccess() { | ||
var x = { | ||
foo: { | ||
bar: function () { return true; } | ||
} | ||
}; | ||
if (x.foo.bar) { // error | ||
} | ||
if (x.foo.bar) { // ok | ||
x.foo.bar; | ||
} | ||
} | ||
var Foo = /** @class */ (function () { | ||
function Foo() { | ||
} | ||
Foo.prototype.isUser = function () { | ||
return true; | ||
}; | ||
Foo.prototype.test = function () { | ||
if (this.isUser) { // error | ||
} | ||
if (this.maybeIsUser) { // ok | ||
} | ||
}; | ||
return Foo; | ||
}()); |
153 changes: 153 additions & 0 deletions
153
tests/baselines/reference/truthinessCallExpressionCoercion.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
=== tests/cases/compiler/truthinessCallExpressionCoercion.ts === | ||
function onlyErrorsWhenTestingNonNullableFunctionType(required: () => boolean, optional?: () => boolean) { | ||
>onlyErrorsWhenTestingNonNullableFunctionType : Symbol(onlyErrorsWhenTestingNonNullableFunctionType, Decl(truthinessCallExpressionCoercion.ts, 0, 0)) | ||
>required : Symbol(required, Decl(truthinessCallExpressionCoercion.ts, 0, 54)) | ||
>optional : Symbol(optional, Decl(truthinessCallExpressionCoercion.ts, 0, 78)) | ||
|
||
if (required) { // error | ||
>required : Symbol(required, Decl(truthinessCallExpressionCoercion.ts, 0, 54)) | ||
} | ||
|
||
if (optional) { // ok | ||
>optional : Symbol(optional, Decl(truthinessCallExpressionCoercion.ts, 0, 78)) | ||
} | ||
|
||
if (!!required) { // ok | ||
>required : Symbol(required, Decl(truthinessCallExpressionCoercion.ts, 0, 54)) | ||
} | ||
|
||
if (required()) { // ok | ||
>required : Symbol(required, Decl(truthinessCallExpressionCoercion.ts, 0, 54)) | ||
} | ||
} | ||
|
||
function onlyErrorsWhenUnusedInBody() { | ||
>onlyErrorsWhenUnusedInBody : Symbol(onlyErrorsWhenUnusedInBody, Decl(truthinessCallExpressionCoercion.ts, 12, 1)) | ||
|
||
function test() { return Math.random() > 0.5; } | ||
>test : Symbol(test, Decl(truthinessCallExpressionCoercion.ts, 14, 39)) | ||
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
|
||
if (test) { // error | ||
>test : Symbol(test, Decl(truthinessCallExpressionCoercion.ts, 14, 39)) | ||
|
||
console.log('test'); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
} | ||
|
||
if (test) { // ok | ||
>test : Symbol(test, Decl(truthinessCallExpressionCoercion.ts, 14, 39)) | ||
|
||
console.log(test); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>test : Symbol(test, Decl(truthinessCallExpressionCoercion.ts, 14, 39)) | ||
} | ||
|
||
if (test) { // ok | ||
>test : Symbol(test, Decl(truthinessCallExpressionCoercion.ts, 14, 39)) | ||
|
||
test(); | ||
>test : Symbol(test, Decl(truthinessCallExpressionCoercion.ts, 14, 39)) | ||
} | ||
|
||
if (test) { // ok | ||
>test : Symbol(test, Decl(truthinessCallExpressionCoercion.ts, 14, 39)) | ||
|
||
[() => null].forEach(() => { | ||
>[() => null].forEach : Symbol(Array.forEach, Decl(lib.es5.d.ts, --, --)) | ||
>forEach : Symbol(Array.forEach, Decl(lib.es5.d.ts, --, --)) | ||
|
||
test(); | ||
>test : Symbol(test, Decl(truthinessCallExpressionCoercion.ts, 14, 39)) | ||
|
||
}); | ||
} | ||
|
||
if (test) { // error | ||
>test : Symbol(test, Decl(truthinessCallExpressionCoercion.ts, 14, 39)) | ||
|
||
[() => null].forEach(test => { | ||
>[() => null].forEach : Symbol(Array.forEach, Decl(lib.es5.d.ts, --, --)) | ||
>forEach : Symbol(Array.forEach, Decl(lib.es5.d.ts, --, --)) | ||
>test : Symbol(test, Decl(truthinessCallExpressionCoercion.ts, 36, 29)) | ||
|
||
test(); | ||
>test : Symbol(test, Decl(truthinessCallExpressionCoercion.ts, 36, 29)) | ||
|
||
}); | ||
} | ||
} | ||
|
||
function checksPropertyAccess() { | ||
>checksPropertyAccess : Symbol(checksPropertyAccess, Decl(truthinessCallExpressionCoercion.ts, 40, 1)) | ||
|
||
const x = { | ||
>x : Symbol(x, Decl(truthinessCallExpressionCoercion.ts, 43, 9)) | ||
|
||
foo: { | ||
>foo : Symbol(foo, Decl(truthinessCallExpressionCoercion.ts, 43, 15)) | ||
|
||
bar() { return true; } | ||
>bar : Symbol(bar, Decl(truthinessCallExpressionCoercion.ts, 44, 14)) | ||
} | ||
} | ||
|
||
if (x.foo.bar) { // error | ||
>x.foo.bar : Symbol(bar, Decl(truthinessCallExpressionCoercion.ts, 44, 14)) | ||
>x.foo : Symbol(foo, Decl(truthinessCallExpressionCoercion.ts, 43, 15)) | ||
>x : Symbol(x, Decl(truthinessCallExpressionCoercion.ts, 43, 9)) | ||
>foo : Symbol(foo, Decl(truthinessCallExpressionCoercion.ts, 43, 15)) | ||
>bar : Symbol(bar, Decl(truthinessCallExpressionCoercion.ts, 44, 14)) | ||
} | ||
|
||
if (x.foo.bar) { // ok | ||
>x.foo.bar : Symbol(bar, Decl(truthinessCallExpressionCoercion.ts, 44, 14)) | ||
>x.foo : Symbol(foo, Decl(truthinessCallExpressionCoercion.ts, 43, 15)) | ||
>x : Symbol(x, Decl(truthinessCallExpressionCoercion.ts, 43, 9)) | ||
>foo : Symbol(foo, Decl(truthinessCallExpressionCoercion.ts, 43, 15)) | ||
>bar : Symbol(bar, Decl(truthinessCallExpressionCoercion.ts, 44, 14)) | ||
|
||
x.foo.bar; | ||
>x.foo.bar : Symbol(bar, Decl(truthinessCallExpressionCoercion.ts, 44, 14)) | ||
>x.foo : Symbol(foo, Decl(truthinessCallExpressionCoercion.ts, 43, 15)) | ||
>x : Symbol(x, Decl(truthinessCallExpressionCoercion.ts, 43, 9)) | ||
>foo : Symbol(foo, Decl(truthinessCallExpressionCoercion.ts, 43, 15)) | ||
>bar : Symbol(bar, Decl(truthinessCallExpressionCoercion.ts, 44, 14)) | ||
} | ||
} | ||
|
||
class Foo { | ||
>Foo : Symbol(Foo, Decl(truthinessCallExpressionCoercion.ts, 55, 1)) | ||
|
||
maybeIsUser?: () => boolean; | ||
>maybeIsUser : Symbol(Foo.maybeIsUser, Decl(truthinessCallExpressionCoercion.ts, 57, 11)) | ||
|
||
isUser() { | ||
>isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion.ts, 58, 32)) | ||
|
||
return true; | ||
} | ||
|
||
test() { | ||
>test : Symbol(Foo.test, Decl(truthinessCallExpressionCoercion.ts, 62, 5)) | ||
|
||
if (this.isUser) { // error | ||
>this.isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion.ts, 58, 32)) | ||
>this : Symbol(Foo, Decl(truthinessCallExpressionCoercion.ts, 55, 1)) | ||
>isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion.ts, 58, 32)) | ||
} | ||
|
||
if (this.maybeIsUser) { // ok | ||
>this.maybeIsUser : Symbol(Foo.maybeIsUser, Decl(truthinessCallExpressionCoercion.ts, 57, 11)) | ||
>this : Symbol(Foo, Decl(truthinessCallExpressionCoercion.ts, 55, 1)) | ||
>maybeIsUser : Symbol(Foo.maybeIsUser, Decl(truthinessCallExpressionCoercion.ts, 57, 11)) | ||
} | ||
} | ||
} | ||
|
179 changes: 179 additions & 0 deletions
179
tests/baselines/reference/truthinessCallExpressionCoercion.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
=== tests/cases/compiler/truthinessCallExpressionCoercion.ts === | ||
function onlyErrorsWhenTestingNonNullableFunctionType(required: () => boolean, optional?: () => boolean) { | ||
>onlyErrorsWhenTestingNonNullableFunctionType : (required: () => boolean, optional?: (() => boolean) | undefined) => void | ||
>required : () => boolean | ||
>optional : (() => boolean) | undefined | ||
|
||
if (required) { // error | ||
>required : () => boolean | ||
} | ||
|
||
if (optional) { // ok | ||
>optional : (() => boolean) | undefined | ||
} | ||
|
||
if (!!required) { // ok | ||
>!!required : true | ||
>!required : false | ||
>required : () => boolean | ||
} | ||
|
||
if (required()) { // ok | ||
>required() : boolean | ||
>required : () => boolean | ||
} | ||
} | ||
|
||
function onlyErrorsWhenUnusedInBody() { | ||
>onlyErrorsWhenUnusedInBody : () => void | ||
|
||
function test() { return Math.random() > 0.5; } | ||
>test : () => boolean | ||
>Math.random() > 0.5 : boolean | ||
>Math.random() : number | ||
>Math.random : () => number | ||
>Math : Math | ||
>random : () => number | ||
>0.5 : 0.5 | ||
|
||
if (test) { // error | ||
>test : () => boolean | ||
|
||
console.log('test'); | ||
>console.log('test') : void | ||
>console.log : (message?: any, ...optionalParams: any[]) => void | ||
>console : Console | ||
>log : (message?: any, ...optionalParams: any[]) => void | ||
>'test' : "test" | ||
} | ||
|
||
if (test) { // ok | ||
>test : () => boolean | ||
|
||
console.log(test); | ||
>console.log(test) : void | ||
>console.log : (message?: any, ...optionalParams: any[]) => void | ||
>console : Console | ||
>log : (message?: any, ...optionalParams: any[]) => void | ||
>test : () => boolean | ||
} | ||
|
||
if (test) { // ok | ||
>test : () => boolean | ||
|
||
test(); | ||
>test() : boolean | ||
>test : () => boolean | ||
} | ||
|
||
if (test) { // ok | ||
>test : () => boolean | ||
|
||
[() => null].forEach(() => { | ||
>[() => null].forEach(() => { test(); }) : void | ||
>[() => null].forEach : (callbackfn: (value: () => null, index: number, array: (() => null)[]) => void, thisArg?: any) => void | ||
>[() => null] : (() => null)[] | ||
>() => null : () => null | ||
>null : null | ||
>forEach : (callbackfn: (value: () => null, index: number, array: (() => null)[]) => void, thisArg?: any) => void | ||
>() => { test(); } : () => void | ||
|
||
test(); | ||
>test() : boolean | ||
>test : () => boolean | ||
|
||
}); | ||
} | ||
|
||
if (test) { // error | ||
>test : () => boolean | ||
|
||
[() => null].forEach(test => { | ||
>[() => null].forEach(test => { test(); }) : void | ||
>[() => null].forEach : (callbackfn: (value: () => null, index: number, array: (() => null)[]) => void, thisArg?: any) => void | ||
>[() => null] : (() => null)[] | ||
>() => null : () => null | ||
>null : null | ||
>forEach : (callbackfn: (value: () => null, index: number, array: (() => null)[]) => void, thisArg?: any) => void | ||
>test => { test(); } : (test: () => null) => void | ||
>test : () => null | ||
|
||
test(); | ||
>test() : null | ||
>test : () => null | ||
|
||
}); | ||
} | ||
} | ||
|
||
function checksPropertyAccess() { | ||
>checksPropertyAccess : () => void | ||
|
||
const x = { | ||
>x : { foo: { bar(): boolean; }; } | ||
>{ foo: { bar() { return true; } } } : { foo: { bar(): boolean; }; } | ||
|
||
foo: { | ||
>foo : { bar(): boolean; } | ||
>{ bar() { return true; } } : { bar(): boolean; } | ||
|
||
bar() { return true; } | ||
>bar : () => boolean | ||
>true : true | ||
} | ||
} | ||
|
||
if (x.foo.bar) { // error | ||
>x.foo.bar : () => boolean | ||
>x.foo : { bar(): boolean; } | ||
>x : { foo: { bar(): boolean; }; } | ||
>foo : { bar(): boolean; } | ||
>bar : () => boolean | ||
} | ||
|
||
if (x.foo.bar) { // ok | ||
>x.foo.bar : () => boolean | ||
>x.foo : { bar(): boolean; } | ||
>x : { foo: { bar(): boolean; }; } | ||
>foo : { bar(): boolean; } | ||
>bar : () => boolean | ||
|
||
x.foo.bar; | ||
>x.foo.bar : () => boolean | ||
>x.foo : { bar(): boolean; } | ||
>x : { foo: { bar(): boolean; }; } | ||
>foo : { bar(): boolean; } | ||
>bar : () => boolean | ||
} | ||
} | ||
|
||
class Foo { | ||
>Foo : Foo | ||
|
||
maybeIsUser?: () => boolean; | ||
>maybeIsUser : (() => boolean) | undefined | ||
|
||
isUser() { | ||
>isUser : () => boolean | ||
|
||
return true; | ||
>true : true | ||
} | ||
|
||
test() { | ||
>test : () => void | ||
|
||
if (this.isUser) { // error | ||
>this.isUser : () => boolean | ||
>this : this | ||
>isUser : () => boolean | ||
} | ||
|
||
if (this.maybeIsUser) { // ok | ||
>this.maybeIsUser : (() => boolean) | undefined | ||
>this : this | ||
>maybeIsUser : (() => boolean) | undefined | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// @strictNullChecks:true | ||
|
||
function onlyErrorsWhenTestingNonNullableFunctionType(required: () => boolean, optional?: () => boolean) { | ||
if (required) { // error | ||
} | ||
|
||
if (optional) { // ok | ||
} | ||
|
||
if (!!required) { // ok | ||
} | ||
|
||
if (required()) { // ok | ||
} | ||
} | ||
|
||
function onlyErrorsWhenUnusedInBody() { | ||
function test() { return Math.random() > 0.5; } | ||
|
||
if (test) { // error | ||
console.log('test'); | ||
} | ||
|
||
if (test) { // ok | ||
console.log(test); | ||
} | ||
|
||
if (test) { // ok | ||
test(); | ||
} | ||
|
||
if (test) { // ok | ||
[() => null].forEach(() => { | ||
test(); | ||
}); | ||
} | ||
|
||
if (test) { // error | ||
[() => null].forEach(test => { | ||
test(); | ||
}); | ||
} | ||
} | ||
|
||
function checksPropertyAccess() { | ||
const x = { | ||
foo: { | ||
bar() { return true; } | ||
} | ||
} | ||
|
||
if (x.foo.bar) { // error | ||
} | ||
|
||
if (x.foo.bar) { // ok | ||
x.foo.bar; | ||
} | ||
} | ||
|
||
class Foo { | ||
maybeIsUser?: () => boolean; | ||
|
||
isUser() { | ||
return true; | ||
} | ||
|
||
test() { | ||
if (this.isUser) { // error | ||
} | ||
|
||
if (this.maybeIsUser) { // ok | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a pretty good example of what TS would now consider suspicious-enough code to error on. Making
e
optional would also have fixed this.