Skip to content

fixes for typescript 2.7 #667

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

Merged
merged 1 commit into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"private": true,
"dependencies": {
"lerna": "^2.4.0",
"cross-env": "^5.1.1",
"husky": "^0.13.4",
"prettier": "^1.4.4",
"lerna": "^2.4.0",
"lint-staged": "^3.6.1",
"cross-env":"^5.1.1"
"prettier": "^1.4.4"
},
"scripts": {
"clean": "lerna clean",
Expand Down
2 changes: 1 addition & 1 deletion packages/mobx-state-tree/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"tape": "^4.6.0",
"tslib": "^1.7.1",
"tslint": "^3.15.1",
"typescript": "^2.4.2"
"typescript": "^2.7.0"
},
"peerDependencies": {
"mobx": "^3.1.15"
Expand Down
2 changes: 1 addition & 1 deletion packages/mobx-state-tree/src/core/type/type-checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export function typecheckPublic(type: IType<any, any>, value: any): void {
const errors = type.validate(value, [{ path: "", type }])

if (errors.length > 0) {
console.error("Failed to create `${type.name}` from:", value)
console.error(`Failed to create "${type.name}" from:`, value)
fail(
`Error while converting ${shortenPrintValue(
prettyPrintValue(value)
Expand Down
9 changes: 5 additions & 4 deletions packages/mobx-state-tree/src/types/complex-types/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ export class ArrayType<S, T> extends ComplexType<S[], IObservableArray<T>> {
return array
}

finalizeNewInstance = (node: ObjectNode, snapshot: any) => {
const instance = node.storedValue as IObservableArray<any>
extras.getAdministration(instance).dehancer = node.unbox
finalizeNewInstance = (node: INode, snapshot: any) => {
const objNode = node as ObjectNode
const instance = objNode.storedValue as IObservableArray<any>
extras.getAdministration(instance).dehancer = objNode.unbox
intercept(instance, change => this.willChange(change) as any)
node.applySnapshot(snapshot)
objNode.applySnapshot(snapshot)
observe(instance, this.didChange)
}

Expand Down
9 changes: 5 additions & 4 deletions packages/mobx-state-tree/src/types/complex-types/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ export class MapType<S, T> extends ComplexType<{ [key: string]: S }, IExtendedOb
return map
}

finalizeNewInstance = (node: ObjectNode, snapshot: any) => {
const instance = node.storedValue as ObservableMap<any>
extras.interceptReads(instance, node.unbox)
finalizeNewInstance = (node: INode, snapshot: any) => {
const objNode = node as ObjectNode
const instance = objNode.storedValue as ObservableMap<any>
extras.interceptReads(instance, objNode.unbox)
intercept(instance, c => this.willChange(c))
node.applySnapshot(snapshot)
objNode.applySnapshot(snapshot)
observe(instance, this.didChange)
}

Expand Down
18 changes: 10 additions & 8 deletions packages/mobx-state-tree/src/types/complex-types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class ModelType<S, T> extends ComplexType<S, T> implements IModelType<S,
* The original object definition
*/
public readonly initializers: ((instance: any) => any)[]
public readonly properties: { [K: string]: IType<any, any> }
public readonly properties: { [K: string]: IType<any, any> } = {}
private preProcessor: (snapshot: any) => any | undefined
private readonly propertiesNames: string[]

Expand Down Expand Up @@ -190,7 +190,8 @@ export class ModelType<S, T> extends ComplexType<S, T> implements IModelType<S,
}

props<SP, TP>(
properties: { [K in keyof TP]: IType<any, TP[K]> } & { [K in keyof SP]: IType<SP[K], any> }
properties: { [K in keyof TP]: IType<any, TP[K]> | TP[K] } &
{ [K in keyof SP]: IType<SP[K], any> | SP[K] }
): IModelType<S & SP, T & TP> {
return this.cloneAndEnhance({ properties } as any)
}
Expand Down Expand Up @@ -309,15 +310,16 @@ export class ModelType<S, T> extends ComplexType<S, T> implements IModelType<S,
return instance as Object
}

finalizeNewInstance = (node: ObjectNode, snapshot: any) => {
const instance = node.storedValue as IStateTreeNode
finalizeNewInstance = (node: INode, snapshot: any) => {
const objNode = node as ObjectNode
const instance = objNode.storedValue as IStateTreeNode
this.forAllProps((name, type) => {
extendShallowObservable(instance, {
[name]: observable.ref(
type.instantiate(node, name, node._environment, snapshot[name])
type.instantiate(objNode, name, objNode._environment, snapshot[name])
)
})
extras.interceptReads(instance, name, node.unbox)
extras.interceptReads(instance, name, objNode.unbox)
})

this.initializers.reduce((self, fn) => fn(self), instance)
Expand Down Expand Up @@ -493,10 +495,10 @@ export function model<T = {}>(properties?: IModelProperties<T>): IModelType<Snap
* @export
* @alias types.model
*/
export function model(...args: any[]) {
export function model<T = {}>(...args: any[]): IModelType<Snapshot<T>, T> {
const name = typeof args[0] === "string" ? args.shift() : "AnonymousModel"
const properties = args.shift() || {}
return new ModelType({ name, properties })
return new ModelType({ name, properties }) as IModelType<Snapshot<T>, T>
}

export function compose<T1, S1, T2, S2, T3, S3>(
Expand Down
2 changes: 1 addition & 1 deletion packages/mobx-state-tree/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function extendKeepGetter(a: any, ...b: any[]) {
for (let i = 0; i < b.length; i++) {
const current = b[i]
for (let key in current) {
const descriptor = Object.getOwnPropertyDescriptor(current, key)
const descriptor = Object.getOwnPropertyDescriptor(current, key)!
if ("get" in descriptor) {
Object.defineProperty(a, key, { ...descriptor, configurable: true })
continue
Expand Down
2 changes: 1 addition & 1 deletion packages/mobx-state-tree/test/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ test("snapshot should be available and updated during an action", t => {
const a = Model.create({ x: 2 })
t.is(a.inc(), 3)
t.is(a.x, 4)
t.is(getSnapshot(a).x, 4)
t.is(getSnapshot<typeof Model.SnapshotType>(a).x, 4)
})

test("indirectly called private functions should be able to modify state", t => {
Expand Down
2 changes: 1 addition & 1 deletion packages/mobx-state-tree/test/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,5 +242,5 @@ test("snapshot processors can be composed", t => {
}))
const x = X.create({ x: 25 })
t.is(x.x, 2)
t.is(getSnapshot(x).x, 25)
t.is(getSnapshot<typeof X.SnapshotType>(x).x, 25)
})
2 changes: 1 addition & 1 deletion packages/mobx-state-tree/test/optimizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test("it should avoid processing patch if is exactly the current one in reconcil
})
const store = RootModel.create({ a: { a: 1, b: "hello" } })
unprotect(store)
const snapshot = getSnapshot(store)
const snapshot = getSnapshot<typeof Model.SnapshotType>(store)
store.a = snapshot.a
t.is(getSnapshot(store.a), snapshot.a)
t.deepEqual(getSnapshot(store), snapshot)
Expand Down
2 changes: 1 addition & 1 deletion packages/mobx-state-tree/test/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ test("Date can be rehydrated using unix timestamp", t => {
t.is(store.date.getTime(), time.getTime())
applySnapshot(store, { date: newTime })
t.is(store.date.getTime(), newTime)
t.is(getSnapshot(store).date, newTime)
t.is(getSnapshot<typeof Factory.SnapshotType>(store).date, newTime)
})
2 changes: 2 additions & 0 deletions packages/mobx-state-tree/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"noImplicitAny": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"strict": true,
"strictPropertyInitialization": false,
"strictNullChecks": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10503,6 +10503,10 @@ typescript@^2.4.2:
version "2.5.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.5.3.tgz#df3dcdc38f3beb800d4bc322646b04a3f6ca7f0d"

typescript@^2.7.0:
version "2.7.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.7.2.tgz#2d615a1ef4aee4f574425cdff7026edf81919836"

ua-parser-js@^0.7.9:
version "0.7.17"
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.17.tgz#e9ec5f9498b9ec910e7ae3ac626a805c4d09ecac"
Expand Down