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

Add Array.prototype.findLast and Array.prototype.findLastIndex #48829

Closed
yume-chan opened this issue Apr 25, 2022 · 29 comments · Fixed by #49636
Closed

Add Array.prototype.findLast and Array.prototype.findLastIndex #48829

yume-chan opened this issue Apr 25, 2022 · 29 comments · Fixed by #49636
Assignees
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Effort: Casual Good issue if you're already used to contributing to the codebase. Harder than "good first issue". Fix Available A PR has been opened for this issue Help Wanted You can do this

Comments

@yume-chan
Copy link
Contributor

yume-chan commented Apr 25, 2022

lib Update Request

Configuration Check

My compilation target is ES5 and my lib is ESNext.

Missing / Incorrect Definition

Missing Array.prototype.findLast and Array.prototype.findLastIndex

Sample Code

[3, 2, 1].findLast(x => x > 2);

Documentation Link

Proposal: https://tc39.es/proposal-array-find-from-last/ (currently stage 3 4)
Can I Use: https://caniuse.com/mdn-javascript_builtins_array_findlast (Chrome and Safari already shipped it without flags)

@andrewbranch andrewbranch added the Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript label Apr 25, 2022
@andrewbranch andrewbranch added this to the TypeScript 4.8.0 milestone Apr 25, 2022
@andrewbranch
Copy link
Member

[Double checking with @DanielRosenwasser that stage 3 is the only criteria for adding to lib.esnext]

@DanielRosenwasser
Copy link
Member

It is, but if I ask for stage 4 at an upcoming meeting we might as well add it to a stable version.

@fatcerberus
Copy link

Seems weird that Firefox hasn’t implemented it yet, that’s a bit unfortunate.

@Kingwl
Copy link
Contributor

Kingwl commented Apr 28, 2022

Seems weird that Firefox hasn’t implemented it yet, that’s a bit unfortunate.

Sorry....My fault.

@fatcerberus
Copy link

@Kingwl It’s your fault that Firefox doesn’t support this API? What?

@DanielRosenwasser
Copy link
Member

@fatcerberus, the context is that @Kingwl is contributing the changeset to Firefox.

By the way, we reached Stage 4 today! 🎉🎉🎉

I believe that means we should add this to lib.es2023.array.d.ts.

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Jun 6, 2022
@DanielRosenwasser DanielRosenwasser changed the title [lib] missing Array.prototype.findLast and Array.prototype.findLastIndex Add Array.prototype.findLast and Array.prototype.findLastIndex Jun 6, 2022
@DanielRosenwasser DanielRosenwasser added Help Wanted You can do this Effort: Casual Good issue if you're already used to contributing to the codebase. Harder than "good first issue". labels Jun 6, 2022
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Jun 22, 2022
@monikams
Copy link

With which version of typescript I could use the method findLast?

@juniorbotelho
Copy link

@monikams I found a temporally solution for this problem, just create a global.d.ts file in anywhere of your project and type this code:

export {}

declare global {
  interface Array<T> {
    findLastIndex(
      predicate: (value: T, index: number, obj: T[]) => unknown,
      thisArg?: any
    ): number
  }
}

Ready! You can use this approach without wrongs from your typescript linter.

@cobaltt7
Copy link

cobaltt7 commented Nov 6, 2022

findLastIndex is in Node LTS v18.0.0

@RamonBalthazar
Copy link

Is adding these methods a breaking change? Why has this been moved to the 5.0 milestone? Thanks
@DanielRosenwasser

@andrewbranch
Copy link
Member

5.0 is just the next release.

G-bot987 added a commit to G-bot987/NextDnDCharacterManager that referenced this issue Dec 26, 2022
…s will be fixed in typescript version 5, short term fix is to add to globals microsoft/TypeScript#48829
@jez9999
Copy link

jez9999 commented Jan 12, 2023

Has this been added for 5.0? I'm really wanting to use findLastIndex and according to this, all modern browsers support it.

@DutchKevv
Copy link

Any updates?

Its a bit funky that we still have to include a custom typing for this...

@DanielRosenwasser
Copy link
Member

It's in TypeScript 5.0 beta and the nightlies. The target release dates are available here.

@jez9999
Copy link

jez9999 commented Mar 16, 2023

Well I upgraded my project to Typescript 5.0.2 today and hoped the errors would go away but nope, I still get:

Property 'findLastIndex' does not exist on type 'MoveDelta[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later.

If I try setting that in my tsconfig.json:

	"compilerOptions": {
		"lib":["es2023"],
	}

I get the build error:

Parsing error: Invalid value for lib provided: es2023

Can anyone help me out? What am I doing wrong?

@DanielRosenwasser
Copy link
Member

Given the error message, it sounds like you need to upgrade your version of typescript-eslint.

@jez9999
Copy link

jez9999 commented Mar 16, 2023

Given the error message, it sounds like you need to upgrade your version of typescript-eslint.

It seems to be TS itself giving the error. In VS code, the error is suffixed with "ts(2550)".

@DanielRosenwasser
Copy link
Member

I'm not sure - if you can validate that your editor is using the latest version of TS (you can select the workspace version)

image

and that you see the same errors when all other extensions/plugins are disabled, then I would file a separate bug.

@jez9999
Copy link

jez9999 commented Mar 16, 2023

Well it's not using that dev version but it is using the 5.0.2 workspace version. Should that have it?

@cristan
Copy link

cristan commented Apr 13, 2023

It works on TypeScript 5 at least at the playground typescriptlang.org: example. It doesn't say much though: it also works just fine if you select Typescript 4.

Also note that you'll need at least Node.JS 18, as that's the lowest version that supports findLast (source).

@BR0kEN-
Copy link

BR0kEN- commented Apr 18, 2023

It works only in TS 5 and only when the target is ESNext.

@juneate
Copy link

juneate commented Apr 25, 2023

@cristan It works on TypeScript 5 at least at the playground typescriptlang.org

Incorrect, as you can see here:
image

As @BR0kEN- said, if you change the target in that environment, the issue disappears. But if you try to "Export" the example to CodeSandbox with the same settings, it triggers the issue again.

image

Tried also updating to the latest Typyscript 5.1 build, no change.

DaviDevMod added a commit to DaviDevMod/focus-trap that referenced this issue May 7, 2023
DaviDevMod added a commit to DaviDevMod/focus-trap that referenced this issue May 7, 2023
DaviDevMod added a commit to DaviDevMod/focus-trap that referenced this issue May 7, 2023
@cristan
Copy link

cristan commented Jun 26, 2023

findLast was introduced in EcmaScript 2023. However, es2023 isn't supported in Typescript 5. Maybe that's it?

es2023 not supported

@shakedhagag
Copy link

Is there any progress with this issue?

@jacobbogers
Copy link

Is there any progress on this issue?

@RyanCavanaugh
Copy link
Member

There's not progress because it was fixed in 2022. If you're not seeing findLast / etc, you have a configuration issue. Seek support at StackOverflow, Discord, etc.

@cristan
Copy link

cristan commented Dec 19, 2023

Ah, it indeed does work, but the problem is indeed that findLast was introduced in EcmaScript 2023. You'll need Node.JS 18 or higher for that, but that that isn't much of a problem. TypeScript is slightly more problematic:

Setting the target to es2023 even in TypeScript 5.3.3 yields the same error I had before. However, findLast does work when setting the target to esnext ^_^

In the handbook, you'll see that es2023 is indeed not an option. It also states: "esnext is a moving target that may include support for Stage 3 proposals to ECMAScript modules". So I would much rather set the target explicitly to es2023 instead to this vague concept, but hey, it works.

@GoRRRRchik
Copy link

GoRRRRchik commented Feb 20, 2024

here is the solution

make the declaration under your code with findLastIndex

Array.prototype.findLastIndex = function(predicate) { for (let i = this.length - 1; i >= 0; i--) { if (predicate(this[i], i, this)) { return i; } } return -1; };

and it will fix your issue

@traez
Copy link

traez commented Sep 18, 2024

Compile Error Line 31: Char 32: error TS2550: Property 'findLastIndex' does not exist on type 'string[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later.

Got above on my LeetCode solution https://leetcode.com/problems/increasing-decreasing-string

MDN says

"Baseline 2022 Newly available Since August 2022, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers."

So I guess that's why. Leetcode yet to update their transpiler/compiler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Effort: Casual Good issue if you're already used to contributing to the codebase. Harder than "good first issue". Fix Available A PR has been opened for this issue Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.