deepkit-framework icon indicating copy to clipboard operation
deepkit-framework copied to clipboard

type-compiler produces empty ops for some types and broken output

Open wielski opened this issue 2 years ago • 5 comments

type-compiler produces empty ops for type Result from https://github.com/traverse1984/oxide.ts

import { Result } from 'oxide.ts';
export type MyResult = Result<number, Error>;

causes broken output:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__ΩMyResult = void 0;
const __ΩMyResult; <--- empty
exports.__ΩMyResult = __ΩMyResult;
//# sourceMappingURL=index.js.map

with error 'Const declarations' require an initialization value.

Result is both type and a class constructor.

It happens because of extractPackStructOfTypeReference calls for extractPackStructOfType with node FunctionDeclaration, that stops working because of config.mode is never. I suspect it happens because of findReflectionFromPath reading tsconfig.json from oxide.ts inside node_modules folder, and returns never cause there is no reflection field.

wielski avatar Aug 16 '22 15:08 wielski

Yeah oxide.ts was not built using the type-compiler, so all their types will be excluded. In your case this results in an error since excluded types are not handled as any or unknown. So, the fix is to emit __ΩMyResult as any (or unknown) bytecode.

marcj avatar Aug 16 '22 19:08 marcj

Taking a types from third party npm modules would be helpful. In my case I want to deserialize json with optionable fields, using interface like this:

import { Option } from 'oxide.ts';

interface MyOptionableStruct {
  myOptionableField: Option<number>;
}

maybe it possible to build type declaration reflection for external modules by adding an extra type for this purpose? like this:

import { Result } from 'oxide.ts';
import { TypeReference } from '@deepkit/type';

export type MyResult = Result<number, Error>; // reflects as any
export type MyResultRef = TypeReference<Result<number, Error>>; // reflects as real result type
export type MyResultRef = Result<number, Error> && TypeReference; // or this way

wielski avatar Aug 17 '22 09:08 wielski

Yeah that's not possible due to technical restrictions

marcj avatar Aug 17 '22 09:08 marcj

@marcj thanks for your help :) I'll try to rebuild desired types with type-compiler

wielski avatar Aug 17 '22 10:08 wielski

@wielski this will be possible when https://github.com/deepkit/deepkit-framework/pull/517 lands

marcus-sa avatar Dec 12 '23 13:12 marcus-sa