#Why the line 23 not `satisfies` the type?

8 messages · Page 1 of 1 (latest)

pure cragBOT
#
SettingDust#4766

Preview:ts type Primitive = | null | undefined | string | number | boolean | symbol | bigint; type BuiltIns = Primitive | Date | RegExp; type Cloneable = Record<PropertyKey, unknown> | BuiltIns | Iterable<Cloneable> | Cloneable[] type ClonedIterable<T extends Iterable<unknown>> = T extends Iterable<infer R> ...

hazy bramble
#
type Primitive =
    | null
    | undefined
    | string
    | number
    | boolean
    | symbol
    | bigint;
type BuiltIns = Primitive | Date | RegExp;
type Cloneable = Record<PropertyKey, unknown> | BuiltIns | Iterable<Cloneable> | Cloneable[]
type ClonedIterable<T extends Iterable<unknown>> = T extends Iterable<infer R>
  ? R extends Cloneable
    ? Iterable<Cloned<R>>
    : never
  : never
type Cloned<T> = T extends Iterable<unknown> ? ClonedIterable<T> : T
#
function cloneIterable<T extends Cloneable>(value: Iterable<T>) {
  const iterable = (function* () {
    for (const item of value) yield item as Cloned<T>
  })() as Iterable<Cloned<T>>

  return iterable satisfies ClonedIterable<typeof value>
}
#

The satisfies at the end of the function cloneIterable failed

#

Type 'Iterable<Cloned<T>>' does not satisfy the expected type 'T extends Cloneable ? Iterable<Cloned<T>> : never'.

#

But why? T should extends Cloneable

#

Why the line 23 not satisfies the type?

vale cipher
#

i think it's because you have a generic conditional type involved. TS can't really analyze whether values are assignable to such types. even relatively simple things like this fail to typecheck: