A unique spline able to be intertwined and produce a new version of itself, most of the time.
export default class UniqueSpline {
public intertwinedCount: number;
constructor(parent?: UniqueSpline) {
this.intertwinedCount = parent && parent.intertwinedCount + 1 || 0;
}
public intertwine(): UniqueSpline | undefined {
return new UniqueSpline(this);
}
}
import { assert, expect } from 'chai';
import UniqueSpline from '../src/unique-spline';
describe("UniqueSpline", () => {
const uniqueSpline = new UniqueSpline();
it("creates a new intertwined spline", () => {
const intertwinedSpline = uniqueSpline.intertwine();
expect(intertwinedSpline).to.not.be.null;
expect(intertwinedSpline.intertwinedCount).to.eq(1);
});
});
Encounters
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/unique-spline.spec.ts:18:12 - error TS2532: Object is possibly 'undefined'.
18 expect(intertwinedSpline.intertwinedCount).to.eq(1);
To work around this issue in tests, an if
statement is used.
it("creates a new intertwined spline", () => {
const intertwinedSpline = uniqueSpline.intertwine();
if (intertwinedSpline) {
expect(intertwinedSpline.intertwinedCount).to.eq(1);
} else {
expect(intertwinedSpline).to.not.be.null;
}
});
Is there a solution for this problem without turning off strictNullChecks
?
Code available at https://github.com/dblock/typescript-mocha-strict-null-checks.