Skip to content

Commit

Permalink
Enable strict null checks
Browse files Browse the repository at this point in the history
This will make the type-tests.ts fail with: Object is possibly 'undefined'.
  • Loading branch information
laszlopandy committed Oct 5, 2022
1 parent f4e2cc8 commit ca7dff8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@
"state management"
],
"jest": {
"transform": {
"^.+\\.ts?$": "ts-jest"
},
"preset": "ts-jest",
"testRegex": "test/.*\\.(t|j)sx?$",
"moduleFileExtensions": [
"ts",
Expand Down
2 changes: 1 addition & 1 deletion test/computedFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ test("supports options", () => {

test("supports onCleanup", () => {
const sep = observable.box(".")
const unloaded = []
const unloaded: unknown[] = []
const joinedStr = computedFn((sep) => [1, 2, 3].join(sep), {
onCleanup: (result, sep) => unloaded.push([result, sep]),
})
Expand Down
27 changes: 16 additions & 11 deletions test/create-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test("transform1", () => {
})

let mapped
let unloaded = []
let unloaded: [{ title: string }, string][] = []

let transformState = createTransformer(function (state: any) {
stateCalc++
Expand Down Expand Up @@ -54,7 +54,7 @@ test("transform1", () => {
expect(stateCalc).toBe(4)
expect(todoCalc).toBe(3)

let tea = state.todos.shift()
let tea = state.todos.shift()!
expect(mapped).toBe("johnBISCUIT")
expect(stateCalc).toBe(5)
expect(todoCalc).toBe(3)
Expand Down Expand Up @@ -112,6 +112,11 @@ test("createTransformer as off-instance computed", () => {
// transformer creates a computed but reuses it for every time the same object is passed in
let displayName = createTransformer(_computeDisplayName)

interface Person {
firstName: string
lastName: string
}

let person1 = m.observable({
firstName: "Mickey",
lastName: "Mouse",
Expand All @@ -122,8 +127,8 @@ test("createTransformer as off-instance computed", () => {
lastName: "Duck",
})

let persons = m.observable([])
let displayNames = []
let persons = m.observable<Person>([])
let displayNames: string[] = []

let disposer = m.autorun(() => {
displayNames = persons.map((p) => displayName(p))
Expand Down Expand Up @@ -484,7 +489,7 @@ test("transform tree (modifying tree incrementally)", () => {
////////////////////////////////////

// add partial tree as a batch
let children = []
let children: typeof TreeNode[] = []
children.push(new TreeNode("root-1-child-1b"))
children[0].addChild(new TreeNode("root-1-child-1b-child-1"))
children.push(new TreeNode("root-1-child-2b"))
Expand Down Expand Up @@ -1155,8 +1160,8 @@ test("transform with primitive key", () => {
})
}

let observableBobs = m.observable([])
let bobs = []
let observableBobs = m.observable<string | number>([])
let bobs: { name: string }[] = []

let bobFactory = createTransformer(function (key) {
return new Bob()
Expand Down Expand Up @@ -1261,7 +1266,7 @@ function createTestSet(): any {

TreeNode.prototype.path = function () {
let node = this,
parts = []
parts: string[] = []
while (node) {
parts.push(node.name)
node = node.parent
Expand Down Expand Up @@ -1328,7 +1333,7 @@ test("should respect onCleanup argument", () => {
name: "michel",
})
let mapped
let unloaded = []
let unloaded: any[] = []
let objectName
let transformState = createTransformer(function (state: any) {
return state.name + state.todos.map(transformTodo).join(",")
Expand Down Expand Up @@ -1374,14 +1379,14 @@ test("should respect debugNameGenerator argument", () => {
mapped = transformTodo(state)
})
state.set("name", "BISCUIT")
objectName = m.getObserverTree(state, "title").observers[0].name
objectName = m.getObserverTree(state, "title").observers?.[0].name
expect(objectName).toBe("COFFEE-DEBUG")
})

test("supports computed value options", () => {
const events: number[][] = []
const xs = m.observable([1, 2, 3])
const xsLessThan = createTransformer((n) => xs.filter((x) => x < n), {
const xsLessThan = createTransformer<number, number[]>((n) => xs.filter((x) => x < n), {
equals: m.comparer.structural,
})

Expand Down
4 changes: 2 additions & 2 deletions test/deepObserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ test("add", () => {
})

test("delete", () => {
assertChanges({ x: 1 }, (x) => {
assertChanges<{ x?: number }>({ x: 1 }, (x) => {
delete x.x
})
})

test("cleanup", () => {
const a = observable({ b: 1 })
const x = observable({ a })
const x = observable<{ a?: { b: number } }>({ a })
const events: any[] = []

const d = deepObserve(x, (change, path) => {
Expand Down
4 changes: 2 additions & 2 deletions test/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test("expr", function () {
)
})

let b = []
let b: unknown[] = []
let sub = mobx.observe(
total,
function (x) {
Expand Down Expand Up @@ -65,7 +65,7 @@ test("expr2", function () {
)
})

let b = []
let b: unknown[] = []
let sub = mobx.observe(
total,
function (x) {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"rootDir": ".",
"lib": ["dom", "es2015", "scripthost"],
"useDefineForClassFields": true,
"strictNullChecks": true,
},
"include": ["**/*.ts"],
"exclude": ["/node_modules"]
Expand Down

0 comments on commit ca7dff8

Please sign in to comment.