deepkit / deepkit-framework

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

Improve error message "No valid runtime type for x given." #508

Open marcj opened 7 months ago

marcj commented 7 months ago

https://github.com/deepkit/deepkit-framework/blob/92315f91ed4295d77b8c7bb693e8cbf8d5d32b23/packages/type/src/reflection/processor.ts#L363

@SamJakob had a great idea about improving this error message. Quote from discord:

Totally - I love the way Flutter does it’s error messages, they basically explain the issue in a fair bit of detail and then provide tips on how to fix it.

So for example you could have a helper function that detects if reflection is set in tsconfig.json and explains to the user that it’s not set and what it needs to be set for (in a fairly general way in case it’s a peer dependency in another framework like yours or mine) and that the user has to set it.

And then similarly for detecting if the typescript copy is patched

it's not always possible to detect tsconfig.json options or paths (since they might be provided by a build tool, or when run in the browser), but we could describe in a general way what needs to be enabled. And if the code runs in the server maybe try to check if there is a tsconfig.json and just read that file (works for beginners well enough that have likely a single tsconfig.json)

SamJakob commented 7 months ago

Also, when reflection is not set to true in tsconfig.json, the following error is returned (in this case when testing with valuesOf):

deepkit-framework/packages/type/src/reflection/reflection.ts:136
    throw new Error('No type given');
          ^
Error: No type given
    at typeOf (deepkit-framework/packages/type/src/reflection/reflection.ts:136:11)
    at valuesOf (deepkit-framework/packages/type/src/reflection/reflection.ts:98:18)
    at Object.<anonymous> (deepkit-test/src/main.ts:41:21)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module.m._compile (deepkit-test/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Object.require.extensions.<computed> [as .ts] (deepkit-test/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Function.Module._load (node:internal/modules/cjs/loader:938:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)

Edit: this might be specific to valuesOf (and/or typeOf) in any case

marcj commented 7 months ago

good catch. we should make sure we don't have several different error messages meaning the same thing