Skip to content
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

strictNullChecks does not detect assertions done before property access #13652

Closed
connium opened this issue Jan 24, 2017 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@connium
Copy link

connium commented Jan 24, 2017

TypeScript Version: 2.1.5

Code
In my jasmine tests I use .toBeDefined(...) to check whether the tested value is filled properly.

interface Person {
  name: string;
  address?: Address;
}

interface PersonService {
  getByName(name: string): Person;
}

it('should work as expected', () => {
  const person = personService.getByName('Norris');

  expect(person.address).toBeDefined();
  expect(person.address.state).toEqual('Oklahoma');
});

Running the compiler with strictNullChecks enabled results in a error TS2532: Object is possibly 'undefined'. on the line where person.address.state is accessed.

Checking explicitly for undefined resolves this issue:

it('should work as expected', () => {
  const person = personService.getByName('Norris');

  if (person.address === undefined) {
    throw new Error('address is undefined');
  }
  expect(person.address.state).toEqual('Oklahoma');
});

As this is a common pattern in my code, I thought it would be a good idea to extract the check in order to keep the tests clean and simple.

function assertNotUndefined(value: any): void|never {
  if (value === undefined) {
    throw new Error('value is undefined');
  }
}

it('should work as expected', () => {
  const person = personService.getByName('Norris');

  assertNotUndefined(person.address);
  expect(person.address.state).toEqual('Oklahoma');
});

Now I get the compiler error again.

Expected behavior:
The compiler does not complain about access to an potentially undefined variable when --strictNullChecks is set.

Actual behavior:
The compiler says error TS2532: Object is possibly 'undefined'.

@connium connium changed the title strictNullChecks strictNullChecks does not detect assertions done before property access Jan 24, 2017
@RyanCavanaugh
Copy link
Member

See #9693

A pattern you can use is:

function assertNotUndefined<T>(value: T | undefined): value is T{
  if (value === undefined) {
    throw new Error('value is undefined');
    }
  return true;
}

it('should work as expected', () => {
  const person: {address?: {state: string}} = personService.getByName('Norris');

  if (!assertNotUndefined(person.address)) return;
  expect(person.address.state).toEqual('Oklahoma');
});

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 24, 2017
@connium
Copy link
Author

connium commented Feb 10, 2017

You are right, my issue is similar to the one described in #9693. Didn't find it when looking for solutions to my problem somehow.

I like the idea of type guards as they are proposed in #8655. So closing this in favor of #8655.

@connium connium closed this as completed Feb 10, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants