joiful-ts / joiful

TypeScript Declarative Validation for Joi
239 stars 18 forks source link

Types of values of object returned by validateAsClass not as expected #190

Closed cubabit closed 3 years ago

cubabit commented 3 years ago

When I use validateAsClass and cast a value from a string to a date, and my schema property is typed as a Date, I expect the type of the property to be a date after validating.

Example

import * as jf from "joiful";

class Schema {
  @jf.date().required()
  date: Date;
}

const input = {
  date: "2021-01-01",
};

const { value } = jf.validateAsClass(input, Schema);

console.log(value.date.constructor);

The value logged to the console is [Function: Date] as expected.

Expected behaviour

When hovering over date (in value.date.contructor) the type should be Date.

Actual behaviour

When hovering over date (in value.date.contructor) the type is string.

Environment

tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "lib": [
      "es2019",
      "es2020.bigint",
      "es2020.string",
      "es2020.symbol.wellknown"
    ],
    "module": "commonjs",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "outDir": "dist/",
    "removeComments": true,
    "resolveJsonModule": true,
    "rootDir": "src/",
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "strictNullChecks": true,
    "strictPropertyInitialization": false,
    "suppressImplicitAnyIndexErrors": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "target": "es2019"
  },
  "exclude": [
    "**/__mocks__/*"
  ],
  "include": [
    "src/",
    "types/"
  ]
}
laurence-myers commented 3 years ago

Sounds like this is a quirk of whatever editor or IDE you are using.

I set up a unit test for this, and stepped through the debugger in IntelliJ IDEA. The date object's prototype constructor is Date. Hovering over value.date.constructor give me Function with some nice JSDoc comments.

I don't think there's much we can do in joiful, maybe raise it with the developers of your source code editor?

image

image

image