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

Number key not work with ObjectValue #752

Closed
Emiyaaaaa opened this issue Nov 15, 2023 · 1 comment · Fixed by #753
Closed

Number key not work with ObjectValue #752

Emiyaaaaa opened this issue Nov 15, 2023 · 1 comment · Fixed by #753

Comments

@Emiyaaaaa
Copy link
Collaborator

Emiyaaaaa commented Nov 15, 2023

ObjectValue sounds like be GivenObject[GivenKey], but it doesn't seem to be exactly the same.

Playground Link

import { ObjectValue, ToString } from "type-fest/source/internal";

type ObjectT = {
  0: number
  '1': number
}

declare const a: ObjectT['0'] //=> number
declare const b: ObjectT[0] //=> number

declare const c: ObjectT[1] //=> number
declare const d: ObjectT['1'] //=> number

// ObjectValue's behavior sounds like ObjectT[Key], but 👇🏻:
declare const e: ObjectValue<ObjectT, 0> //=> number
declare const f: ObjectValue<ObjectT, '0'> //=> never ❌ 

declare const g: ObjectValue<ObjectT, 1> //=> never ❌
declare const h: ObjectValue<ObjectT, '1'> //=> number

// new type
type ObjectValue1<T, K> =
	K extends keyof T
		? T[K]
		: ToString<K> extends keyof T
			? T[ToString<K>]
			: K extends `${infer NumberK extends number}`
				? NumberK extends keyof T
					? T[NumberK]
					: never
				: never;
// worked!
declare const i: ObjectValue1<ObjectT, '0'> //=> number ✅
declare const j: ObjectValue1<ObjectT, 1> //=> 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
@Emiyaaaaa
Copy link
Collaborator Author

@sindresorhus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant