2019-08-10 19:50:14 +02:00
|
|
|
// @strictNullChecks:true
|
|
|
|
|
2019-08-18 18:48:05 +02:00
|
|
|
function onlyErrorsWhenTestingNonNullableFunctionType(required: () => boolean, optional?: () => boolean) {
|
|
|
|
if (required) { // error
|
|
|
|
}
|
2019-08-10 19:50:14 +02:00
|
|
|
|
2019-08-18 18:48:05 +02:00
|
|
|
if (optional) { // ok
|
|
|
|
}
|
2019-08-10 19:50:14 +02:00
|
|
|
|
2019-08-18 18:48:05 +02:00
|
|
|
if (!!required) { // ok
|
2019-08-10 19:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (required()) { // ok
|
|
|
|
}
|
2019-08-18 18:48:05 +02:00
|
|
|
}
|
2019-08-10 19:50:14 +02:00
|
|
|
|
2019-08-18 18:48:05 +02:00
|
|
|
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();
|
|
|
|
});
|
2019-08-10 19:50:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 18:48:05 +02:00
|
|
|
function checksPropertyAccess() {
|
2019-08-10 19:50:14 +02:00
|
|
|
const x = {
|
|
|
|
foo: {
|
2019-08-18 18:48:05 +02:00
|
|
|
bar() { return true; }
|
2019-08-10 19:50:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x.foo.bar) { // error
|
|
|
|
}
|
|
|
|
|
2019-08-18 18:48:05 +02:00
|
|
|
if (x.foo.bar) { // ok
|
|
|
|
x.foo.bar;
|
2019-08-10 19:50:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
maybeIsUser?: () => boolean;
|
|
|
|
|
|
|
|
isUser() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
test() {
|
|
|
|
if (this.isUser) { // error
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.maybeIsUser) { // ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 23:45:32 +02:00
|
|
|
|
|
|
|
// Test for GH-35557 where ids were not assigned for a symbol.
|
|
|
|
function A(stats: StatsBase<any>) {
|
|
|
|
if (stats.isDirectory) { // err
|
|
|
|
console.log(`[Directory] ${stats.ctime}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function B(a: Nested, b: Nested) {
|
|
|
|
if (a.stats.isDirectory) { // err
|
|
|
|
b.stats.isDirectory();
|
|
|
|
}
|
|
|
|
if (a.stats.isDirectory) { // ok
|
|
|
|
a.stats.isDirectory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface StatsBase<T> {
|
|
|
|
isDirectory(): boolean;
|
|
|
|
ctime: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Nested {
|
|
|
|
stats: StatsBase<any>;
|
|
|
|
}
|