io-ts: Broken build with typescript@2.9.0-rc

Here’s the error

export type mixed = object | number | string | boolean | symbol | undefined | null
export type Is<A> = (m: mixed) => m is A // <= new error
/*
A type predicate's type must be assignable to its parameter's type.
  Type 'A' is not assignable to type 'mixed'.
    Type 'A' is not assignable to type 'object'.
*/    

Related: section “Unconstrained type parameters are no longer assignable to object in strictNullChecks” in https://blogs.msdn.microsoft.com/typescript/2018/05/16/announcing-typescript-2-9-rc/

What’s the better fix? Changing mixed to

export type mixed = {} | undefined | null

?

EDIT: best bet so far is

export type mixed = { [key: string]: any } | object | number | string | boolean | symbol | undefined | null

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 15 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I must admit I don’t even use this library

Well, then thank you for taking the time to chime in, more opinions/suggestions are always helpful.

that definition of mixed leaves a dangerous loophole

You are right, my candidate is not a perfect fit for a general purpose definition of mixed. However looks good enough for io-ts, let me explain what I mean. I chose { [key: string]: any } because it plays well with t.Dictionary. In fact, in “idiomatic” io-ts you would define foo as

function foo(m: t.mixed) {
  if (t.Dictionary.is(m) && 'bar' in m) {
    const bar = m['bar'] // bar has type mixed
    bar.baz // and the compiler DOES complain here
  }
}

Finally, as @sledorze pointed out, in the TypeScript repo there’s some work related to unknown (https://github.com/Microsoft/TypeScript/pull/24439) and any (https://github.com/Microsoft/TypeScript/pull/24423) so maybe we should just wait the next release and then try to change mixed to

type mixed = unknown

@gcanti works also for me withtypescript@2.9.1.

wait for the next stable release (I guess typescript@2.9.1)

My guess was wrong, unknown is coming in TypeScript 3, see Announcing TypeScript 2.9.1

@sledorze so in the meanwhile I would go for

export type mixed = { [key: string]: any } | object | number | string | boolean | symbol | undefined | null

WDYT?

No errors at compilation time. Looks good to me 👍

maybe we should just wait…

FYI just tried typescript@3.0.0-dev.20180531 (which already contains unknown) and type mixed = unknown works like a charm