microsoft / microsoft/TypeScript

`as const` breaks intra expression inference

Open
#62,220 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: check: Type Inference Help Wanted Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### 🔎 Search Terms

const context expression assertion inference type parameter

### 🕗 Version & Regression Information

- This is the behavior in every version I tried

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.0-dev.20250806#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwxAGcMAeAFQBp4ARAPgApYBzALngG8AoeeLVAA7IM7GgG4e8ATBzBkYEOyYw2tAJTwAvHXjkJvMHiLIAtovjLV5DdvipTAIxAwJAXzWiJXQ6hLwHMgDWIPiaBMQYDNy8-EIi8ACMAAxJlJLSsvLmljY6LAB0GDgAyhgw-MwMamkGRqbZLLnwBRAhzBgAFmmuzUTwPiRqXgMY8ADuODCBFVrhJFGSscLsyanpMnIKSo1aeSqFJWUVVTX9dWbbKk0tbZ3dQ1xAA

### 💻 Code

```ts
declare function test(arg: {
input: D;
produce: (arg: D) => T;
consume: (arg: T) => number;
}): D;

const broken = test({
input: 100,
produce: (arg) => arg.toString(), // 'arg' is of type 'unknown'.(18046)
consume: (arg) => arg.length,
} as const);

const working = test({
input: 100,
produce: (arg) => arg.toString(), // `arg` inferred as `number`
consume: (arg) => arg.length,
});
```

### 🙁 Actual behavior

`as const` is the only difference between those two and its presence breaks assigned parameter type in `produce`

### 🙂 Expected behavior

I think `as const` should not impact inference this way and both should be inferred just OK. It's just that the `broken` case should infer `100` instead of `number` for the `D` type parameter

### Additional information about the issue

_No response_

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 with the linked TypeScript Playground and compare the `broken` and `working` examples, then trace how the `as const` assertion affects contextual typing and inference for `produce`. Done means the asserted case infers the callback parameter without `unknown` and preserves the expected literal `100` inference for `D`.

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
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.