deepkit / deepkit-framework

A new full-featured and high-performance TypeScript framework
https://deepkit.io/
MIT License
3.2k stars 123 forks source link

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

Open wielski opened 2 years ago

wielski commented 2 years ago

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.

marcj commented 2 years ago

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.

wielski commented 2 years ago

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
marcj commented 2 years ago

Yeah that's not possible due to technical restrictions

wielski commented 2 years ago

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

marcus-sa commented 9 months ago

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