Fix to typedefs for Typescript 2.7#637
Conversation
| const properties = args.shift() || {} | ||
| return new ModelType({ name, properties }) | ||
| const config = {name: (name as string), properties} as ModelTypeConfig | ||
| return new ModelType(config) |
There was a problem hiding this comment.
This and above fixes the error: src/types/complex-types/model.ts(483,17): error TS2394: Overload signature is not compatible with function implementation.
There was a problem hiding this comment.
You may simply change
export function model(...args: any[]) {
to
export function model(...args: any[]): IModelType<Snapshot<any>, any> {
| const current = b[i] | ||
| for (let key in current) { | ||
| const descriptor = Object.getOwnPropertyDescriptor(current, key) | ||
| const descriptor = Object.getOwnPropertyDescriptor(current, key) || {} |
There was a problem hiding this comment.
This fixes the error src/utils.ts(55,26): error TS2532: Object is possibly 'undefined'.
There was a problem hiding this comment.
you may also do:
if (descriptor && "get" in descriptor) {
| 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> { |
There was a problem hiding this comment.
I have no idea why,
But uncommenting the additional props declaration also fix this issue
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
@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;| "tslib": "^1.7.1", | ||
| "tslint": "^3.15.1", | ||
| "typescript": "^2.4.2" | ||
| "typescript": "^2.7.1" |
There was a problem hiding this comment.
Its a yarn project,
You'll need to upgrade via yarn for the lock file to be updated
| const properties = args.shift() || {} | ||
| return new ModelType({ name, properties }) | ||
| const config = {name: (name as string), properties} as ModelTypeConfig | ||
| return new ModelType(config) |
There was a problem hiding this comment.
You may simply change
export function model(...args: any[]) {
to
export function model(...args: any[]): IModelType<Snapshot<any>, any> {
| const current = b[i] | ||
| for (let key in current) { | ||
| const descriptor = Object.getOwnPropertyDescriptor(current, key) | ||
| const descriptor = Object.getOwnPropertyDescriptor(current, key) || {} |
There was a problem hiding this comment.
you may also do:
if (descriptor && "get" in descriptor) {
| 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> { |
There was a problem hiding this comment.
I have no idea why,
But uncommenting the additional props declaration also fix this issue
|
Hey @runningskull, what is the status of this PR? |
0812fa2 to
49f5915
Compare
|
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 |
|
@runningskull @Bnaya How this issue is fixed. I use version 1.3.1. I get same issue. What should I do |
|
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 :) |
Related to #629 and #635.
These changes compile using the latest typescript (using
npm run quick-build) but I don't know enough to know if they're correct or if a there's a better alternative.