question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to have typescript correctly infer the type for a custom type schema?

See original GitHub issue

I have the following:

import * as yup from 'yup'
import Daet from 'daet'

class daetSchema extends yup.date {
	constructor() {
		super()
		this.withMutation(() => {
			this.transform(function(value, originalvalue) {
				if (this.isType(value)) return value
				return new Daet(value)
			})
		})
	}

	_typeCheck(value: any) {
		// @ts-ignore
		return super._typeCheck(value) || value instanceof Daet
	}
}

export const mySchema = yup.object({
	start: new daetSchema()
		.label('Start Time')
		.max(yup.ref('finish'))
		.required()
})
export type MySchema = yup.InferType<typeof mySchema>

However the types are coming back with Date:

const mySchema: yup.ObjectSchema<{
    start: Date;
}>
type MySchema = {
    start: Date;
}

How do I correct their types so they come back with Daet:

const mySchema: yup.ObjectSchema<{
    start: Daet;
}>
type MySchema = {
    start: Daet;
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:9

github_iconTop GitHub Comments

1reaction
rizkimcitracommented, Jul 16, 2022

same issue here, is there any update?

1reaction
baluptoncommented, Apr 7, 2020

does that help?

Not sure what foo, bar, baz, subschema, and o are meant to represent here…

Essentially, all I need is for Yup to work with https://github.com/bevry/daet instances

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Type Inference - TypeScript
This kind of inference takes place when initializing variables and members, setting parameter default values, and determining function return types. In most ...
Read more >
How to Infer Types from Schema : r/typescript - Reddit
So yes, you can start from schema and infer a type for objects that meet this schema, as well as generate a reasonably-typed...
Read more >
Methods for TypeScript runtime type checking - LogRocket Blog
Explore five methods of performing TypeScript type checks at runtime in this post and learn each of their advantages and disadvantages.
Read more >
Designing the perfect Typescript schema validation library
It's a Typescript-first schema declaration library with rigorous (and correct!) inferred types, incredible developer experience, and a few ...
Read more >
TypeScript Simple Types - W3Schools
TypeScript may not always properly infer what the type of a variable may be. In such cases, it will set the type to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found