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

rt.Static<> can't extract static type for Runtype.Record with nested Record ...? #42

Open
breathe opened this Issue Feb 27, 2018 · 10 comments

Comments

Projects
None yet
6 participants
@breathe
Copy link
Contributor

breathe commented Feb 27, 2018

I'm noticing that I can't seem to correctly extract the static type from Runtype's with nested rt.Record({}).

const Example = rt.Record({
  b: rt.Record({
    c: rt.String
  })
});

type StaticExample = rt.Static<typeof Example>;

Effective type of StaticExample (as reported by vscode):

type StaticExample = {
    b: any;
}

And this produces no type error ...

const x: StaticExample = {
  b: ""
};

Am I doing something wrong or is this a bug or a known limitation? I'm using runtypes 2.0.3 with typescript 2.7.2 and this tsconfig:

{
  "compileOnSave": true,
  "compilerOptions": {
    "rootDir": "./src",
    "outDir": "./lib",
    "lib": ["es2017", "es2017.object", "esnext.asynciterable"],
    "strict": true,
    "strictNullChecks": true,
    "noImplicitAny": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es2015",
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "inlineSourceMap": true,
    "inlineSources": true,
    "forceConsistentCasingInFileNames": true,
    "strictFunctionTypes": false,
    "noErrorTruncation": true,
    "strictPropertyInitialization": false
  },
  "include": ["./src/**/*"],
  "exclude": ["node_modules"]
}
@breathe

This comment has been minimized.

Copy link
Contributor

breathe commented Feb 27, 2018

Update to this:

Actually it appears that the typescript compiler is indeed reporting this as an error -- but the type information is not correctly exposed to vscode ...

Also the error message reported by the compiler (invoking tsc) does not appear to make sense ...

file: 'file:///<snip>'
severity: 'Error'
message: 'Type '{ b: string; }' is not assignable to type '{ b: any; }'.
  Types of property 'b' are incompatible.
    Type 'string' is not assignable to type '{ c: string; }'.'
at: '70,7'
source: 'ts'
code: '2322' 

The target type for the assignment should print as '{b: {c: {string }}' shouldn't it ...?

As in

message: 'Type '{ b: string; }' is not assignable to type '{ b: { c: { string; }; }'.

Could there be a typescript bug to report here ...?

@pelotom

This comment has been minimized.

Copy link
Owner

pelotom commented Feb 28, 2018

Hm, that's indeed very odd!

And this produces no type error ...

I'm seeing the error in vscode as well as on the command line...

@gcanti

This comment has been minimized.

Copy link

gcanti commented Feb 28, 2018

@pelotom

This comment has been minimized.

Copy link
Owner

pelotom commented Feb 28, 2018

Related Microsoft/TypeScript#14041

Interesting. As @OliverJAsh pointed out, it seems to affect more than just intellisense, maybe the title should be modified?

@pelotom

This comment has been minimized.

Copy link
Owner

pelotom commented Feb 28, 2018

@gcanti have you found this issue to affect io-ts?

Edit Looks like yes!

@gcanti

This comment has been minimized.

Copy link

gcanti commented Feb 28, 2018

@pelotom after some tests looks like my old workaround is still valid (i.e. going through a type alias)

gcanti/io-ts#144

EDIT: however the inferred types are super ugly

@darky

This comment has been minimized.

Copy link

darky commented Mar 22, 2018

@pelotom, do you want to fix problem as same way, that gcanti/io-ts#144?
Seems Microsoft/TypeScript#14041 not will be fixed soon

@pelotom

This comment has been minimized.

Copy link
Owner

pelotom commented Mar 22, 2018

@darky Do you want to submit a PR to fix it? I won't have time to get to it myself in the near term.

@Ciantic

This comment has been minimized.

Copy link

Ciantic commented Jun 10, 2018

EDIT: I read incorrectly.

Looks like Microsoft/TypeScript#14041 will not be fixed, so some workaround is needed after all.

@isaac-weisberg

This comment has been minimized.

Copy link

isaac-weisberg commented Oct 2, 2018

And yep it's still not out there to be resolved anywhere soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment