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

5153 - IndexOf #33838

Open
veralex opened this issue Jul 15, 2024 · 0 comments
Open

5153 - IndexOf #33838

veralex opened this issue Jul 15, 2024 · 0 comments
Labels
5153 answer Share answers/solutions to a question en in English

Comments

@veralex
Copy link

veralex commented Jul 15, 2024

5153 - IndexOf

by Pineapple (@pineapple0919) #medium #array

Question

Implement the type version of Array.indexOf, indexOf<T, U> takes an Array T, any U and returns the index of the first U in Array T.

type Res = IndexOf<[1, 2, 3], 2>; // expected to be 1
type Res1 = IndexOf<[2,6, 3,8,4,1,7, 3,9], 3>; // expected to be 2
type Res2 = IndexOf<[0, 0, 0], 2>; // expected to be -1

View on GitHub: https://tsch.js.org/5153

...

type IndexOf<T extends unknown[], U extends unknown, N extends number[] = []> = T extends [infer L, ...infer R]
  ? Equal<U, L> extends true
    ? N['length']
    : IndexOf<R, U, [1, ...N]>
  : -1
@veralex veralex added answer Share answers/solutions to a question en in English labels Jul 15, 2024
@github-actions github-actions bot added the 5153 label Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5153 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant