Skip to content

No error accessing non-existent method on string with strictNullChecks on #26426

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

Closed
steadicat opened this issue Aug 13, 2018 · 8 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Duplicate An existing issue was already created

Comments

@steadicat
Copy link

steadicat commented Aug 13, 2018

TypeScript Version: 3.0.1

Search Terms: strictNullChecks, inferred any, no error, parameter, argument, implicit any, property does not exist

Code

function getString(required: {}): string {
  return '';
}

function functionThatReturnsAString() {
  const optional: {} | null = null;
  return getString(optional);
}

const s = functionThatReturnsAString(); // s is correctly inferred as 'string'
s.foo(); // no error?

Expected behavior:

Expected to get error Property 'foo' does not exist on type 'string' on the last line.

Actual behavior:

No error on the last line.

With all the strict settings on, there's a different error on the argument to the getString call, but that should not affect the return type of functionThatReturnsAString. The function could be in a completely separate part of the codebase. Inspecting that should not be required when trying to debug the s.foo() call.

Playground Link: https://www.typescriptlang.org/play/#src=function%20getString(required%3A%20%7B%7D)%20%7B%0D%0A%20%20return%20''%3B%0D%0A%7D%0D%0A%0D%0Afunction%20functionThatReturnsAString()%20%7B%0D%0A%20%20const%20optional%3A%20%7B%7D%20%7C%20null%20%3D%20null%3B%0D%0A%20%20return%20getString(optional)%3B%0D%0A%7D%0D%0A%0D%0Aconst%20s%20%3D%20functionThatReturnsAString()%3B%0D%0As.foo()%3B%0D%0A

Tested with all strict options on. (Note that Playground has been finnicky today.)

Related Issues:

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Aug 13, 2018
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.1 milestone Aug 13, 2018
@ghost
Copy link

ghost commented Aug 13, 2018

I believe this is because of this line in resolveCall in checker.ts:
return produceDiagnostics || !args ? resolveErrorCall(node) : getCandidateForOverloadFailure(node, candidates, args, !!candidatesOutArray);

If produceDiagnostics is true, we just return anySignature (from resolveErrorCall) and don't get downstream errors. But if not, we get the best candidate we can (which happens to be the only one). Maybe we could just always getCandiateForOverloadFailure?

@sheetalkamat
Copy link
Member

Fixed by @Andy-MS in #26646

@ghost
Copy link

ghost commented Aug 30, 2018

That PR didn't fix this, the line I pointed out still exists.

I would leave this issue open since there's still the possibility for confusing differences between services and the compiler.

@ghost ghost removed the Fixed A PR has been merged for this issue label Aug 30, 2018
@sheetalkamat
Copy link
Member

But I see error reported by running tsc as well as from language service so its fixed?

@ghost
Copy link

ghost commented Aug 30, 2018

s_string

We get s: string, but there's no compile error when calling a method that doesn't exist on string -- because the diagnostics-producing TypeChecker has different behavior than the one used for services.

@sheetalkamat
Copy link
Member

image

@ghost
Copy link

ghost commented Sep 4, 2018

@sheetalkamat You probably don't have strictNullChecks enabled since getString(optional) should be an error.

@ghost
Copy link

ghost commented Sep 4, 2018

Duplicate of #19686 .

@ghost ghost closed this as completed Sep 4, 2018
@ghost ghost added the Duplicate An existing issue was already created label Sep 4, 2018
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants