You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Code
In my jasmine tests I use .toBeDefined(...) to check whether the tested value is filled properly.
interfacePerson{name: string;address?: Address;}interfacePersonService{getByName(name: string): Person;}it('should work as expected',()=>{constperson=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',()=>{constperson=personService.getByName('Norris');if(person.address===undefined){thrownewError('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.
functionassertNotUndefined(value: any): void|never{if(value===undefined){thrownewError('value is undefined');}}it('should work as expected',()=>{constperson=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'.
The text was updated successfully, but these errors were encountered:
connium
changed the title
strictNullChecks
strictNullChecks does not detect assertions done before property access
Jan 24, 2017
functionassertNotUndefined<T>(value: T|undefined): value is T{if(value===undefined){thrownewError('value is undefined');}returntrue;}it('should work as expected',()=>{constperson: {address?: {state: string}}=personService.getByName('Norris');if(!assertNotUndefined(person.address))return;expect(person.address.state).toEqual('Oklahoma');});
TypeScript Version: 2.1.5
Code
In my jasmine tests I use
.toBeDefined(...)
to check whether the tested value is filled properly.Running the compiler with
strictNullChecks
enabled results in aerror TS2532: Object is possibly 'undefined'.
on the line whereperson.address.state
is accessed.Checking explicitly for
undefined
resolves this issue: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.
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'.
The text was updated successfully, but these errors were encountered: