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.

with infer in template string, suggestion works in 4.6, but not work in 4.7

See original GitHub issue

Bug Report

🔎 Search Terms

suggestion pathof

🕗 Version & Regression Information

4.7

⏯ Playground Link

Playground link with relevant code

💻 Code


// get from https://github.com/microsoft/TypeScript/issues/20423#issuecomment-812564842
type PathOf<T, K extends string, P extends string = ""> =
  K extends `${infer U}.${infer V}`
    ? U extends keyof T ? PathOf<T[U], V, `${P}${U}.`> : `${P}${keyof T & (string | number)}`
    : K extends keyof T ? `${P}${K}` : `${P}${keyof T & (string | number)}`;


declare function consumer<K extends string>(path: PathOf<{a: string, b: {c: string}}, K>) : number;

// suggestion works well in 4.6, and not work in 4.7
consumer('b.')

🙁 Actual behavior

image

🙂 Expected behavior

image

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
wwwoutercommented, Oct 11, 2022

I’m experiencing the same thing. It worked with 4.6.x, and it still works with 4.9.0-dev.20221007 and 4.8.1-rc

4.6.4

image

4.7.4:

image

It compiles fine with "id" as second argument with both versions, it seems to be the language server that is acting up.

1reaction
dmitri-gbcommented, Sep 16, 2022

We have also come across a somewhat similar case where the list of suggestions in TS 4.7 (and 4.8) is no longer the same as it was in TS 4.6.

declare function pick<T extends object, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K>;

const obj = { aaa: 1, bbb: '2', ccc: true };

pick(obj, 'aaa', ''); // suggestions inside the empty string
                      // TS 4.6: aaa, bbb, ccc
                      // TS 4.7: aaa

Playground link

It is worth noting that if I rewrite pick and make keys a proper array instead of a rest parameter, then the list of suggestions will include all the properties just as in TS 4.6.

Another way to get around the issue is to use a slightly different definition for pick:

declare function pick<T extends object, K extends (keyof T)[]>(obj: T, ...keys: K): Pick<T, K[number]>;

It’s unfortunate that the (seemingly) more natural definition of pick looses its ability to provide useful suggestions. I’m curious if this is something that the TS team would consider a regression and try to revert, or is this how we should expect things to work from now on?

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript - infer template string return unexpected infer result
I was thinking the following Y will return test , however, it returns never , anyone has any idea the reason why and...
Read more >
Documentation - TypeScript 4.6
TypeScript now provides suggestions for when parameter names don't match between your function and its JSDoc comment. Suggestion diagnostics being shown in the ......
Read more >
New Typescript features - The Software House
But first things first. When working with JavaScript files, there is a feature called Template Literals. It enables you to interpolate dynamic ...
Read more >
RHSA-2021:2438 - Security Advisory - Red Hat 客户门户网站
MkdirAll are not checked for permissions (CVE-2020-15113) ... baremetal cluster] multicast traffic is not working on ovn-kubernetes ...
Read more >
XSL Transformations (XSLT) Version 3.0 - W3C
It was developed by the W3C XSLT Working Group. ... The use of the term tree in this document does not imply the...
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