Superstruct v0.12.0 Release Notes

Release Date: 2020-11-24 // over 3 years ago
  • NEW

    🆕 New Describe utility type. This new utility lets you define a struct from an existing TypeScript type and ensure that the struct's validation matches it, otherwise TypeScript's compiler will error. For example:

    type User = {
      id: number
      name: string
    }
    
    const User: Describe<User> = object({
      id: string(), // This mistake will fail to pass type checking!
      name: string(),
    })
    
    BREAKING

    The coerce helper has changed to be more type-safe! Previously coerce functions were called with value: unknown because they ran before all validation. However, now they take a new second argument that is another struct to narrow the cases where coercions occurs. This means the value for coercion will now be type-safe.

    // Previously
    const MyNumber = coerce(number(), (value) => {
      return typeof value === 'string' ? parseFloat(value) : value
    })
    
    // Now
    const MyNumber = coerce(number(), string(), (value) => {
      return parseFloat(value)
    })