Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get<Paths<T>> fails when T has no properties that are known at compile time #991

Open
mwaibel-go opened this issue Nov 25, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@mwaibel-go
Copy link

mwaibel-go commented Nov 25, 2024

Bug description

When T has no keys that are known at compile time, the Result of Paths<T> is not assignable to the second argument of Get.

Repro

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBDAnmApnA3nA4imAaOABQEMYALAZzgF84AzKCEOAciVQFo6UKYWBYAFBDgAOxgoodYgGM0AIQCuAcwA8AFTgoAHhNEATKumoA+DELhwwjMBQBc2XBoIlyFDSZNDqQA

import type { Get, Paths } from 'type-fest'

interface Bug<T extends {}> {
  props: Get<T, Paths<T>>
}

produces the error

Type 'Paths<T>' does not satisfy the constraint 'readonly string[] | LiteralStringUnion<ToString<_Paths<T, { maxRecursionDepth: 10; bracketNotation: false; }> | _Paths<T, { maxRecursionDepth: 10; bracketNotation: true; }>>>'.
  Type 'number | `${number}`' is not assignable to type 'readonly string[] | LiteralStringUnion<ToString<_Paths<T, { maxRecursionDepth: 10; bracketNotation: false; }> | _Paths<T, { maxRecursionDepth: 10; bracketNotation: true; }>>>'.
    Type 'number' is not assignable to type 'readonly string[] | LiteralStringUnion<ToString<_Paths<T, { maxRecursionDepth: 10; bracketNotation: false; }> | _Paths<T, { maxRecursionDepth: 10; bracketNotation: true; }>>>'.

Expected behaviour

I expect the code not to produce a compilation error. Specifically, Paths<T> should be a valid argument to Get<T, ⋅> for all T that are neither null nor undefined. (If I’m not mistaken, every other type in javascript does have properties).

In instances where T has a concrete type, the Bug::props type should be inferred.

interface Foo {
  key: number
}

let bar!: Bug<Foo>
let props = bar.props // number

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@mwaibel-go mwaibel-go added the bug Something isn't working label Nov 25, 2024
@mwaibel-go mwaibel-go changed the title Get<Paths<T extends {}>> fails Get<Paths<T>> fails when T has no properties that are known at compile time Nov 25, 2024
@sindresorhus
Copy link
Owner

sindresorhus commented Nov 25, 2024

What is the expected behavior?

@mwaibel-go
Copy link
Author

I’m sorry for being unspecific. I’ve updated the issue description, @sindresorhus .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants