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

[HOLD for payment 2024-10-10] [$1000] Enable the noUncheckedIndexedAccess TS compiler option #43055

Closed
roryabraham opened this issue Jun 4, 2024 · 52 comments
Assignees
Labels
Awaiting Payment Auto-added when associated PR is deployed to production Daily KSv2 External Added to denote the issue can be worked on by a contributor NewFeature Something to build that is a new item.

Comments

@roryabraham
Copy link
Contributor

roryabraham commented Jun 4, 2024

Slack proposal: https://expensify.slack.com/archives/C01GTK53T8Q/p1717468504413819

Problem

We currently have crash occurring on production. The problematic code can be summarized with a minimal example:

type MyType = {
    something: string;
};

const myArr: MyType[] = [];

// myItem is inferred to have type MyType, when really it is undefined 
const myItem = myArr[42];

// No compiler error, but the app will crash
console.log(myItem.something);

As you can see, we unsafely indexed an array, and then assumed the result was defined. Then trying to access a property of undefined, we experience a crash.

Solution

Enable the noUncheckedArrayAccess TypeScript config. With that config enabled, the type of myItem is correctly inferred to MyType | undefined, and we get a compiler error when trying to access myItem.something, preventing the crash at compile time.

Issue OwnerCurrent Issue Owner: @adelekennedy
@roryabraham roryabraham added External Added to denote the issue can be worked on by a contributor Daily KSv2 NewFeature Something to build that is a new item. labels Jun 4, 2024
@roryabraham roryabraham self-assigned this Jun 4, 2024
@melvin-bot melvin-bot bot added the Help Wanted Apply this label when an issue is open to proposals by contributors label Jun 4, 2024
Copy link

melvin-bot bot commented Jun 4, 2024

Triggered auto assignment to Contributor-plus team member for initial proposal review - @eh2077 (External)

Copy link

melvin-bot bot commented Jun 4, 2024

Triggered auto assignment to @adelekennedy (NewFeature), see https://stackoverflowteams.com/c/expensify/questions/14418#:~:text=BugZero%20process%20steps%20for%20feature%20requests for more details. Please add this Feature request to a GH project, as outlined in the SO.

@melvin-bot melvin-bot bot added Weekly KSv2 Daily KSv2 and removed Daily KSv2 Weekly KSv2 labels Jun 4, 2024
@roryabraham
Copy link
Contributor Author

It's worth noting that when enabling this rule we get more than 1000 errors across more than 200 files, so in order for me to accept a proposal it would need to include a rollout plan so we can incrementally adopt this new rule across a subset of files at a time.

@ShridharGoel
Copy link
Contributor

Proposal

Please re-state the problem that we are trying to solve in this issue.

Enable the noUncheckedIndexedAccess TS compiler option.

What is the root cause of that problem?

New change.

What changes do you think we should make in order to solve the problem?

We need to enable the noUncheckedArrayAccess TypeScript config.

{
  "compilerOptions": {
    // other compiler options
    "noUncheckedArrayAccess": true
  }
}

Once noUncheckedArrayAccess is enabled, TypeScript will infer the type of an array access expression as including undefined. For example, accessing an element of an array myArr: MyType[] at an arbitrary index will be inferred as MyType | undefined.

On enabling:

type MyType = {
    something: string;
};

const myArr: MyType[] = [];

// myItem is inferred to have type MyType | undefined
const myItem = myArr[42];

// Compiler error: Object is possibly 'undefined'
console.log(myItem.something);

Add a unit test:

test('array access should handle undefined values', () => {
    const myArr: MyType[] = [];
    const myItem = myArr[42];
    expect(myItem).toBeUndefined();
});

Incremental adoption:

Add it only to some files or some modules can be done by using a new tsconfig.incremental.json.

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "noUncheckedArrayAccess": true
  },
  "include": [
    "src/module1/**/*.ts",
    "src/module2/file1.ts"
  ]
}

One by one we can keep adding modules. Once most of the files are updated, we can include the check in tsconfig.json.

@eh2077
Copy link
Contributor

eh2077 commented Jun 6, 2024

@ShridharGoel Thanks for your proposal.

How will the new tsconfig.incremental.json cooperate with npm run typecheck which uses tsconfig.json from the root directory?
Is it possible to achieve the goal by editing the tsconfig.json from project root directory?

@ShridharGoel
Copy link
Contributor

ShridharGoel commented Jun 6, 2024

We can use something like tsc -p tsconfig.uncheckedindex.json as the command (using uncheckedindex in the name now).

This comment was marked as off-topic.

@melvin-bot melvin-bot bot added Reviewing Has a PR in review Weekly KSv2 and removed Daily KSv2 labels Jun 6, 2024
@roryabraham
Copy link
Contributor Author

We can use something like tsc -p tsconfig.uncheckedindex.json as the command (using uncheckedindex in the name now).

That sounds reasonable. So the rollout plan would look something like this:

  1. Create a new tsconfig.uncheckedindex.json that only applies the rule to some reasonable subset of files.
  2. Update npm run typecheck to do this:
tsc && tsc -p tsconfig.uncheckedindex.json
  1. Incrementally update tsconfig.uncheckedindex.json, module by module, until in includes all files
  2. Add noUncheckedArrayAccess to the base tsconfig.json, remove tsconfig.uncheckedindex.json and restore npm run typecheck to the way it is now.

sounds good, the only other thing I'd like to figure out is how we want to break it up into reasonably-sized PRs. Some of these might be a bit trickier than your standard TS migration, because it actually involves changing runtime code.

Going to assign this one a bounty of $1000 because it's going to be a few PRs.

@roryabraham roryabraham changed the title Enable the noUncheckedIndexedAccess TS compiler option [$1000] Enable the noUncheckedIndexedAccess TS compiler option Jun 6, 2024
Copy link

melvin-bot bot commented Jun 6, 2024

⚠️ Could not update price automatically because there is no linked Upwork Job ID. The BZ team member will need to update the price manually in Upwork.

@melvin-bot melvin-bot bot removed the Help Wanted Apply this label when an issue is open to proposals by contributors label Jun 6, 2024
@melvin-bot melvin-bot bot added Weekly KSv2 Awaiting Payment Auto-added when associated PR is deployed to production and removed Weekly KSv2 labels Oct 2, 2024
@melvin-bot melvin-bot bot changed the title [$1000] Enable the noUncheckedIndexedAccess TS compiler option [HOLD for payment 2024-10-10] [$1000] Enable the noUncheckedIndexedAccess TS compiler option Oct 3, 2024
Copy link

melvin-bot bot commented Oct 3, 2024

Reviewing label has been removed, please complete the "BugZero Checklist".

@melvin-bot melvin-bot bot removed the Reviewing Has a PR in review label Oct 3, 2024
Copy link

melvin-bot bot commented Oct 3, 2024

The solution for this issue has been 🚀 deployed to production 🚀 in version 9.0.43-6 and is now subject to a 7-day regression period 📆. Here is the list of pull requests that resolve this issue:

If no regressions arise, payment will be issued on 2024-10-10. 🎊

For reference, here are some details about the assignees on this issue:

  • @ShridharGoel requires payment (Needs manual offer from BZ)
  • @zfurtak does not require payment (Contractor)
  • @eh2077 requires payment through NewDot Manual Requests

Copy link

melvin-bot bot commented Oct 3, 2024

BugZero Checklist: The PR adding this new feature has been merged! The following checklist (instructions) will need to be completed before the issue can be closed:

  • [@eh2077] Please propose regression test steps to ensure the new feature will work correctly on production in further releases.
  • [@adelekennedy] Link the GH issue for creating/updating the regression test once above steps have been agreed upon.

@roryabraham

This comment was marked as resolved.

@zfurtak

This comment was marked as resolved.

@roryabraham

This comment was marked as resolved.

@melvin-bot melvin-bot bot added Daily KSv2 and removed Weekly KSv2 labels Oct 9, 2024
Copy link

melvin-bot bot commented Oct 10, 2024

Payment Summary

Upwork Job

BugZero Checklist (@adelekennedy)

  • I have verified the correct assignees and roles are listed above and updated the neccesary manual offers
  • I have verified that there are no duplicate or incorrect contracts on Upwork for this job (https://www.upwork.com/ab/applicants//hired)
  • I have paid out the Upwork contracts or cancelled the ones that are incorrect
  • I have verified the payment summary above is correct

@melvin-bot melvin-bot bot added the Overdue label Oct 11, 2024
@adelekennedy
Copy link

adelekennedy commented Oct 14, 2024

Payouts due:

@ShridharGoel sent you an offer in Upwork

Job Posting

@melvin-bot melvin-bot bot removed the Overdue label Oct 14, 2024
@eh2077
Copy link
Contributor

eh2077 commented Oct 14, 2024

@adelekennedy Thanks for checking payment of this task. I'm not sure if we're eligible for the full payment of this job because the task was reassigned in the middle of the last PR, see here #43055 (comment)

I think it'll be better to wait for @roryabraham 's input before issuing the payment.

@adelekennedy
Copy link

Oh thank you @eh2077 I totally missed that (this issue has gotten pretty dense) Rory is out on parental leave so I'm making a call based on all the comments above - I think an even split here may be the most fair between you and @ShridharGoel?

@melvin-bot melvin-bot bot added the Overdue label Oct 18, 2024
@eh2077
Copy link
Contributor

eh2077 commented Oct 19, 2024

Oh thank you @eh2077 I totally missed that (this issue has gotten pretty dense) Rory is out on parental leave so I'm making a call based on all the comments above - I think an even split here may be the most fair between you and @ShridharGoel?

@adelekennedy Personally, the payment of $500 is fair. Thanks!

Just one more question, can I request payment through NewDot instead?

Copy link

melvin-bot bot commented Oct 21, 2024

@ShridharGoel, @roryabraham, @adelekennedy, @zfurtak, @eh2077 Eep! 4 days overdue now. Issues have feelings too...

@melvin-bot melvin-bot bot removed the Overdue label Oct 21, 2024
@adelekennedy
Copy link

@eh2077 yes! I just withdrew the offer on Upwork and updated the payment summary above

@eh2077
Copy link
Contributor

eh2077 commented Oct 22, 2024

Thanks! Requested in NewDot.

@adelekennedy
Copy link

closing this for now - @ShridharGoel I will pay you once you accept in NewDot

@github-project-automation github-project-automation bot moved this from In Progress to Done in Typescript migration Oct 22, 2024
@JmillsExpensify
Copy link

$500 approved for @eh2077

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting Payment Auto-added when associated PR is deployed to production Daily KSv2 External Added to denote the issue can be worked on by a contributor NewFeature Something to build that is a new item.
Projects
No open projects
Development

No branches or pull requests

8 participants