question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

v4.7 and above stopped supporting a type pattern that used to work in v4.6 (Key Remapping in Mapped Type)

See original GitHub issue

Bug Report

In version 4.7 and above a patter ([P in TName as P]:) causes types to break. It’s really strange, since it does work if used in some ways, but if used to pass down to a method argument, it will result in type unknown.

See playground links for examples.

🕗 Version & Regression Information

A friendly person in the comments of my original stackoverflow question comments narrowed it down to this: between 2022-03-15 and 2022-03-16

⏯ Playground Link

TS 4.7 TS 4.6

💻 Code

The issue was caused by the following types:

[P in TName as P]:

which does not work anymore in v4.7 and needs to be

[P in TName]:

(I had it like this, because I used to exclude a type like this: [P in TName as P extends UnacceptedPaths ? never : P]:)

🙁 Actual behavior

One of the nested types receives type unknown to it’s params, when it should be properly inferred from the code.

🙂 Expected behavior

Should work like in v4.6 - type should be inferred. If I remember correctly, I came up with the problematic type from this.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanCavanaughcommented, Oct 13, 2022

@ahejlsberg here’s a simplified version that presents as an implicit any regression:

type UnacceptedPaths = "really_bad" | "notgood";

type FieldsProp<TFieldValues, TName extends keyof TFieldValues> = {
    [P in TName as P extends UnacceptedPaths ? never : P]: (x: TFieldValues[P]) => void;
};

type RegisterFieldProps<TFieldValues> = {
    control: TFieldValues;
    fields: FieldsProp<TFieldValues, keyof TFieldValues>;
};

declare function RegisterFields<TFieldValues>(fields: RegisterFieldProps<TFieldValues>): unknown;
const TEST = RegisterFields({
    control: {
        test: 99,
    },
    fields: {
        test: (newVal1) => {
            // newVal1 should be type number (4.6)
            // actual: implicit any (4.8+)
            const a: number = newVal1;
        }
    },
});

Can we tweak back the originating PR to still allow inferences from mapped types where the as clause is of the form [K in T as expr ? K : never] / [K in T as expr ? never : K] ?

1reaction
MartinJohnscommented, Oct 11, 2022

Clarification, because I got confused: The issue only appears when you remove this line from the 4.7 playground:

simpleValue: "type_string_not_allowed", // funnily enough, this works properly
Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Mapped Types - TypeScript
Generating types by re-using an existing type. ... Mapped types build on the syntax for index signatures, which are used to declare ......
Read more >
Mastering TypeScript mapped types - LogRocket Blog
In this post, we'll cover mapped types in TypeScript, a real-world example of them, and utility types including Partial, Readonly, and Pick.
Read more >
Removal of mapping types | Elasticsearch Guide [8.5] | Elastic
Elasticsearch 8.0.0 no longer supports mapping types. For details on how to migrate your clusters away from mapping types, see the removal of...
Read more >
keyof and Mapped Types In TypeScript 2.1 - Chris Krycho
Now we can make this generic, so it works for types besides just this one set of states — so that it'll work...
Read more >
What's new in .NET Framework - Microsoft Learn
.NET Framework 4.8 improves support for hosted HWNDs and Windows Forms interoperation in High-DPI WPF applications on platforms that support ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found