A spline that can be reticulated and return another spline. Most of the time at least.
export default class Spline {
public reticulatedCount: number;
constructor(parent?: Spline) {
this.reticulatedCount = parent && parent.reticulatedCount + 1 || 0;
}
public reticulate(): Spline | undefined {
return new Spline(this);
}
}
import { assert, expect } from 'chai';
import Spline from '../src/spline';
describe("Spline", () => {
const spline = new Spline();
it("returns a new spline", () => {
const reticulatedSpline = spline.reticulate();
expect(reticulatedSpline).to.not.be.null;
expect(reticulatedSpline.reticulatedCount).to.eq(1);
});
});
Fails with error TS2532: Object is possibly 'undefined'.
/Users/dblock/source/ts/typescript-mocha/node_modules/ts-node/src/index.ts:245
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
test/spline.spec.ts:18:12 - error TS2532: Object is possibly 'undefined'.
18 expect(reticulatedSpline.reticulatedCount).to.eq(1);
The workaround is an anti-pattern in tests, an if.
it("returns a new spline", () => {
const reticulatedSpline = spline.reticulate();
if (reticulatedSpline) {
expect(reticulatedSpline.reticulatedCount).to.eq(1);
} else {
expect(reticulatedSpline).to.not.be.null;
}
});
How can this be solved without disabling strictNullChecks?
Code in https://github.com/dblock/typescript-mocha-strict-null-checks.
You can use the non-null (!) operator.
it("always can be reticulated again", () => {
const reticulatedSpline = spline.reticulate();
expect(reticulatedSpline).to.not.be.null;
expect(reticulatedSpline!.reticulatedCount).to.eq(1);
});
As the documentation says:
[You] may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact
Source
Updated example for Typescript 3.7 which introduced "assertion signatures":
/**
* Use in place of `expect(value).to.exist`
*
* Work-around for Chai assertions not being recognized by TypeScript's control flow analysis.
* @param {any} value
*/
export function expectToExist<T>(value: T): asserts value is NonNullable<T> {
expect(value).to.exist;
if (value === null || value === undefined) {
throw new Error('Expected value to exist');
}
}
References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With