microsoft / microsoft/TypeScript

Object with all context-sensitive properties requires at least one non-context-sensitive property for inference to work

Open
#64,251 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

🔎 Search Terms

object with all context-sensitive properties, self referencial types, xstate

🕗 Version & Regression Information

Tested with TypeScript 7.0.2, the issue exists even in versions as old as 4.4.4

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.3#code/PTAEAcCcHsCMBsCmBbAXKAJtRBnAdgOQAuoAlngGaKSgAioAxtJJIg0fAJ4A0j0eRRAA8SAQzwZQOIqMGhRreXk4AoBq1mIAsqIYALcogAUAbxWgyeUkVKj46AESiH3c3wHCi6E6ArRo6ACMoAC+rhbSmjjebhaiMRaJoIgCkJzoRkweIryRggCUoAC8AHygZklJWYIisZUgoAB6APx1SXmIbYkNLV0hdf0W-SH5KioNONDwAK42-OiiGJKioNM4iEg4OKB4-AC01Z5763g41qQAbogQMODURJygAAYA+uh40-DwT6BqGoI6fSGUxucjnOyOZzhdw1LzlXz+IKhaEdaLlOrxdGVZKpdKgTL8WG5GQFYplCrYmGeLoWHqtSlSEmdBl0vrQpJvHafeADNzDUYqDBseAKa5ZaSMf7aXQGPCIVBuAA89E8KQw2wpFjBNghoAA1ohONAKHQANoOVEOAC67KpInQcAAVmwiLbUQlKqaAMqWfWG41mi1MnDWq0e7EpIhpZoZQ72wNxojW4madBewqlUAXaCkDC2jkxrlfPq8oYlOpGIUUbWkeagACSQoE1k4AHlYM72MqShny9jaGMHncG02bA9FQAVMpFNwT5IiNXbcSqJLNfGgOeqiTbIwAOn3mLlV0gvdWeD1uwA7nhQGu5wqGZuF9vQE6XbeR5GW+3O0RJ2UH0pCculGJJ3kQY9B04YdGy-B4fxdf9ijcHxTQAaV9A0jRNCcw0-ZtxwndCrTKfogAOfEUiAA

💻 Code
// problem: doesn't infer D correctly, context and state are any
createMachine({
  initial: "a",
  context: { foo: 1 },
  states: {
    a: {
      entry: (context, state) => {
        context
        // ^? any
        state
        // ^? any
      }
    }
  }
})

// solution: add a useless non-context-sensitive property `_: null` 
createMachine({
  initial: "a",
  context: { foo: 1 },
  states: {
    a: {
      entry: (context, state) => {
        context
        // ^? { foo: number }
        state
        // ^? "a"
      },
      _: null
    }
  }
})

declare const createMachine:
  <D extends {
    initial: keyof D["states"],
    context: object,
    states: {
      [S in keyof D["states"]]: {
        entry?: (context: D["context"], state: S) => void,
        _?: null
      }
    }
  }>
    (definition: IdentityObject<D>) =>
      D

type Identity<T> =
  T extends any
    ? ( T extends (...a: never) => unknown ? T :
        T extends object ? IdentityObject<T> :
        T
      )
    : never

type IdentityObject<T> =
  { [K in keyof T]: Identity<T[K]> }
🙁 Actual behavior

The first createMachine invocation doesn't infer the type parameter correctly (it requires at least one non-context-sensitive property) and hence context and state parameters of entry are not inferred.

🙂 Expected behavior

The first createMachine invocation should infer the type parameter correctly without having to pass in a non-context-sensitive property leading to inference of context and state parameters of entry

Additional information about the issue

The premise is adding (or rather not adding) an extra useless property shouldn't make a difference to the inference

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the inference difference in the linked TypeScript Playground using the two createMachine examples. Read the generic declaration and IdentityObject type in the issue, then trace the compiler's type-parameter inference for the context-sensitive entry property. Done means the version without the extra _ property infers context as { foo: number } and state as "a".

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.