10000 Fix to typedefs for Typescript 2.7 by runningskull · Pull Request #637 · mobxjs/mobx-state-tree · GitHub
[go: up one dir, main page]

Skip to content

Fix to typedefs for Typescript 2.7 #637

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

Closed
wants to merge 1 commit into from

Conversation

runningskull
Copy link
@runningskull runningskull commented Feb 2, 2018

Related to #629 and #635.

These changes compile using the latest typescript (usingnpm run quick-build) but I don't know enough to know if they're correct or if a there's a better alternative.

const properties = args.shift() || {}
return new ModelType({ name, properties })
const config = {name: (name as string), properties} as ModelTypeConfig
return new ModelType(config)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and above fixes the error: src/types/complex-types/model.ts(483,17): error TS2394: Overload signature is not compatible with function implementation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may simply change
export function model(...args: any[]) {
to
export function model(...args: any[]): IModelType<Snapshot<any>, any> {

@@ -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) || {}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the error src/utils.ts(55,26): error TS2532: Object is possibly 'undefined'.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may also do:
if (descriptor && "get" in descriptor) {

@@ -191,7 +191,7 @@ 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> }
): IModelType<S & SP, T & TP> {
): IModelType<S & SP & Snapshot<SP>, T & TP> {
Copy link 10000
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solution from #635

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(done in latest commit)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mweststrate wdys?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the impression that property types are broken completely with TS 2.7, are you guys experiencing the same? I mean even x: types.number get's infered to x: any on the IModelType. Maybe we should just wait for TS 2.8 where we can fix this properly? cc @mattiamanzati

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the impression that property types are broken completely with TS 2.7, are you guys experiencing the same? I mean even x: types.number get's infered to x: any on the IModelType. Maybe we should just wait for TS 2.8 where we can fix this properly? cc @mattiamanzati

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more. 10000

@mweststrate
check this out
http://www.typescriptlang.org/play/#src=type%20IModelProperties%3CT%3E%20%3D%20%7B%20%5BK%20in%20keyof%20T%5D%3A%20T%5BK%5D%20%7D%0D%0Afunction%20inferer%3CT%3E(properties%3A%20IModelProperties%3CT%3E)%3A%20IModelProperties%3CT%3E%20%7B%0D%0A%20%20%20%20return%20properties%0D%0A%7D%0D%0A%0D%0A%2F%2F%20if%20you%20hover%20a%20you%20will%20see%20that%20prop1%20and%20prop2%20are%20inferred%20as%20any%0D%0Aconst%20a%20%3D%20inferer(%7B%0D%0A%20%20%20%20prop1%3A%20%22asd%22%2C%0D%0A%20%20%20%20prop2%3A%201%0D%0A%7D)%3B%0D%0A%0D%0Atype%20IMGOODTYPE%20%3D%20IModelProperties%3C%7B%20a%3A%20string%20%7D%3E%3B%0D%0A%2F%2F%20but%20here%2C%20the%20original%20type%20of%20prop1%20and%20prop2%20are%20restored%20%0D%0Atype%20imgoodTYPEEevenahasbadone%20%3D%20typeof%20a%3B

type IModelProperties<T> = { [K in keyof T]: T[K] };
function inferer<T>(properties: IModelProperties<T>): IModelProperties<T> {
  return properties;
}

// if you hover a you will see that prop1 and prop2 are:
// inferred as any on typescript 2.7.1
// inferred as string and number on typescript 2.6.2
const a = inferer({
  prop1: "asd",
  prop2: 1,
});

type IMGOODTYPE = IModelProperties<{ a: string }>;
// but here, the original type of prop1 and prop2 are restored,
// even in typescript 2.7.1
type imgoodTYPEEevenahasbadone = typeof a;

@@ -55,7 +55,7 @@
"tape": "^4.6.0",
"tslib": "^1.7.1",
"tslint": "^3.15.1",
"typescript": "^2.4.2"
"typescript": "^2.7.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its a yarn project,
You'll need to upgrade via yarn for the lock file to be updated

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(done in latest commit)

const properties = args.shift() || {}
return new ModelType({ name, properties })
const config = {name: (name as string), properties} as ModelTypeConfig
return new ModelType(config)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may simply change
export function model(...args: any[]) {
to
export function model(...args: any[]): IModelType<Snapshot<any>, any> {

@@ -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) || {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may also do:
if (descriptor && "get" in descriptor) {

@@ -191,7 +191,7 @@ 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> }
): IModelType<S & SP, T & TP> {
): IModelType<S & SP & Snapshot<SP>, T & TP> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mweststrate
Copy link
Member

Hey @runningskull, what is the status of this PR?

@runningskull
Copy link
Author
runningskull commented Feb 7, 2018

Hey @mweststrate sorry for the delay. I just pushed up the solutions that @Bnaya recommended.

Building works great, but it looks like the tests are still failing due to some problem w/ type definitions, but I'm not exactly sure why. I can investigate some more later, but perhaps you'll know straight away: I put some details about it at runningskull#1

@abelkbil
Copy link
abelkbil commented Feb 8, 2018

@runningskull @Bnaya How this issue is fixed. I use version 1.3.1. I get same issue. What should I do

@mweststrate
Copy link
Member

Closing this, in favor of #667 which seemed to behave more consistently when trying out. Thanks for the investigation though! And let's hope 2.8 lands soon :)

@mweststrate mweststrate closed this Mar 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0