How to have typescript correctly infer the type for a custom type schema?
See original GitHub issueI 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:
- Created 4 years ago
- Reactions:4
- Comments:9
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
same issue here, is there any update?
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