Skip to content

Unable to infer function input type from previous element in nested tupleย #53650

Closed
@ssalbdivad

Description

@ssalbdivad

Bug Report

๐Ÿ”Ž Search Terms

tuples, inference, functions, parameters, parsing, arktype, mapped types

๐Ÿ•— Version & Regression Information

  • This is the behavior in every version I tried (tested up to nightly)

โฏ Playground Link

Playground link with relevant code

๐Ÿ’ป Code

type MorphTuple = [string, "|>", any]

type validateMorph<def extends MorphTuple> = def[1] extends "|>"
    ? [validateDefinition<def[0]>, "|>", (In: def[0]) => unknown]
    : def

type validateDefinition<def> = def extends MorphTuple
    ? validateMorph<def>
    : {
          [k in keyof def]: validateDefinition<def[k]>
      }

declare function type<def>(def: validateDefinition<def>): def

// Function input (x) inferred as "ark"
const shallow = type(["ark", "|>", (x) => x.length])
const objectLiteral = type({ a: ["ark", "|>", (x) => x.length] })

// Function input inferred as any (should be "ark")
const nestedTuple = type([["ark", "|>", (x) => x.length]])

๐Ÿ–Š๏ธ Description

I encountered this bug a few months ago and originally logged an issue within ArkType, but suspected it was a TS bug given the inconsistency in the behavior particularly between the object literal and tuple cases.

The goal of this minimal repro is to narrow the function's input definition and use the type of the first element to infer the input parameter for the third element's function type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions