rikukissa / typehole

TypeScript development tool for Visual Studio Code that helps you automate creating the initial static typing for runtime values

Home Page:https://marketplace.visualstudio.com/items?itemName=rikurouvila.typehole

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Typehole not picking up nulls

andrew-ironforge opened this issue · comments

I've got a typehole as follows:

Screen Shot 2021-05-21 at 7 51 50 PM

row is the following in the debugger:
Screen Shot 2021-05-21 at 7 52 21 PM

Notice how totalCompensation is null even though the typehole only picks up the string. I would expect the generated type to be totalCompensation: string | null;

I did run this typehole a number of times and it should have picked up the null value before I put a breakpoint here for a screenshot.

It definitely seems like a bug. I wasn't able to reproduce it with

const list = ["hello", null];

type AutoDiscovered = any;

list.forEach((val) => {
  console.log(typehole.t3<AutoDiscovered>(val));
});

and keep getting null | string as the output type. Can you verify you have the latest typehole runtime installed (1.6.0)?
I made some fixes earlier, and it's possible that this would now be fixed.

There is a test case that checks that generating the right type from the samples, which leads me to believe that the issue might between the sample storage and the runtime.

There also seems to be something off with how json-to-ts generates the types in case of null

With this input

{ __typeholeRootWrapper__: [ { a: 2 }, { a: null } ] }

We get this output

interface RootObject {
  __typeholeRootWrapper__: TypeholeRootWrapper[];
}
interface TypeholeRootWrapper {
  a?: number;
}

I'd expect it to be

interface RootObject {
  __typeholeRootWrapper__: TypeholeRootWrapper[];
}
interface TypeholeRootWrapper {
  a: number | null;
}

There's actually an issue about the exact problem MariusAlch/json-to-ts#17

Yeah I wonder if these are related. Perhaps json-to-ts just doesn't handle nulls well

I've now forked json-to-ts and fixed the null handling. The fix is available in release https://github.com/rikukissa/typehole/releases/tag/1.6.2 . See if upgrading the extension and the runtime package would fix this. I'll close this issue for now, but please reopen if it still happens