GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Use `missingType` in `--noUncheckedIndexedAccess` mode by ahejlsberg · Pull Request #51653 · microsoft/TypeScript

Conversation

@ahejlsberg

@ahejlsberg Anders Hejlsberg (ahejlsberg) commented Nov 27, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

This implements what I discuss here. I borrwed tests from that PR. Thanks Mateusz Burzyński (@Andarist).

Fixes #43614.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

andreialecu and eklenin reacted with heart emoji andreialecu and JakobBruenker reacted with rocket emoji
All reactions
  • ❤️ 2 reactions
  • 🚀 2 reactions
Anders Hejlsberg (ahejlsberg) added 3 commits November 27, 2022 10:11
@ahejlsberg
Use missingType in noUncheckedIndexedAccess mode
9e67c17
@ahejlsberg
Accept new baselines
54b981f
@ahejlsberg
Add tests
61104d0
@typescript-bot TypeScript Bot (typescript-bot) assigned Anders Hejlsberg (ahejlsberg) Nov 27, 2022
@typescript-bot TypeScript Bot (typescript-bot) added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Nov 27, 2022
@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Nov 27, 2022

Copy link
Copy Markdown
Member Author

TypeScript Bot (@typescript-bot) test this
TypeScript Bot (@typescript-bot) user test this inline
TypeScript Bot (@typescript-bot) run dt
TypeScript Bot (@typescript-bot) perf test faster
TypeScript Bot (@typescript-bot) test top100

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 27, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Heya Anders Hejlsberg (@ahejlsberg), I've started to run the diff-based top-repos suite on this PR at 61104d0. You can monitor the build here.

Update: The results are in!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 27, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Heya Anders Hejlsberg (@ahejlsberg), I've started to run the diff-based user code test suite on this PR at 61104d0. You can monitor the build here.

Update: The results are in!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 27, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Heya Anders Hejlsberg (@ahejlsberg), I've started to run the extended test suite on this PR at 61104d0. You can monitor the build here.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 27, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Heya Anders Hejlsberg (@ahejlsberg), I've started to run the abridged perf test suite on this PR at 61104d0. You can monitor the build here.

Update: The results are in!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 27, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Heya Anders Hejlsberg (@ahejlsberg), I've started to run the parallelized Definitely Typed test suite on this PR at 61104d0. You can monitor the build here.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 27, 2022

Copy link
Copy Markdown
Contributor

Anders Hejlsberg (@ahejlsberg) Here are the results of running the user test suite comparing main and refs/pull/51653/merge:

Everything looks good!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 27, 2022

Copy link
Copy Markdown
Contributor

Anders Hejlsberg (@ahejlsberg)
The results of the perf run you requested are in!

Here they are:

Comparison Report - main..51653

Metric main 51653 Delta Best Worst
Angular - node (v16.17.1, x64)
Memory used 340,642k (± 0.01%) 340,607k (± 0.02%) -35k (- 0.01%) 340,403k 340,686k
Parse Time 1.90s (± 0.62%) 1.89s (± 0.73%) -0.02s (- 0.84%) 1.85s 1.92s
Bind Time 0.65s (± 0.51%) 0.65s (± 0.86%) -0.00s (- 0.31%) 0.63s 0.66s
Check Time 5.18s (± 0.51%) 5.17s (± 0.35%) -0.01s (- 0.10%) 5.13s 5.20s
Emit Time 5.13s (± 1.03%) 5.10s (± 0.84%) -0.03s (- 0.60%) 5.00s 5.20s
Total Time 12.87s (± 0.61%) 12.81s (± 0.40%) -0.06s (- 0.44%) 12.69s 12.93s
Compiler-Unions - node (v16.17.1, x64)
Memory used 187,674k (± 0.59%) 187,348k (± 0.51%) -326k (- 0.17%) 186,605k 189,975k
Parse Time 0.79s (± 0.42%) 0.79s (± 0.75%) +0.00s (+ 0.51%) 0.78s 0.81s
Bind Time 0.42s (± 0.53%) 0.42s (± 0.53%) -0.00s (- 0.48%) 0.41s 0.42s
Check Time 6.10s (± 0.89%) 6.10s (± 0.53%) -0.01s (- 0.10%) 6.03s 6.17s
Emit Time 1.94s (± 0.49%) 1.94s (± 0.71%) -0.00s (- 0.05%) 1.90s 1.97s
Total Time 9.25s (± 0.63%) 9.25s (± 0.38%) -0.00s (- 0.02%) 9.17s 9.31s
Monaco - node (v16.17.1, x64)
Memory used 319,814k (± 0.01%) 319,791k (± 0.01%) -23k (- 0.01%) 319,741k 319,867k
Parse Time 1.43s (± 0.35%) 1.42s (± 0.69%) -0.00s (- 0.28%) 1.40s 1.44s
Bind Time 0.59s (± 0.84%) 0.59s (± 0.75%) -0.00s (- 0.50%) 0.58s 0.60s
Check Time 4.90s (± 0.42%) 4.88s (± 0.31%) -0.03s (- 0.53%) 4.84s 4.92s
Emit Time 2.74s (± 0.66%) 2.73s (± 0.59%) -0.01s (- 0.29%) 2.69s 2.77s
Total Time 9.67s (± 0.36%) 9.62s (± 0.25%) -0.04s (- 0.43%) 9.58s 9.68s
TFS - node (v16.17.1, x64)
Memory used 282,278k (± 0.01%) 282,282k (± 0.01%) +5k (+ 0.00%) 282,226k 282,338k
Parse Time 1.17s (± 1.06%) 1.16s (± 1.01%) -0.01s (- 1.02%) 1.14s 1.19s
Bind Time 0.66s (± 3.65%) 0.66s (± 2.84%) +0.00s (+ 0.76%) 0.61s 0.69s
Check Time 4.75s (± 0.51%) 4.76s (± 0.38%) +0.01s (+ 0.27%) 4.70s 4.78s
Emit Time 2.73s (± 2.00%) 2.79s (± 1.99%) +0.07s (+ 2.50%) 2.67s 2.89s
Total Time 9.30s (± 0.88%) 9.38s (± 0.58%) +0.08s (+ 0.81%) 9.26s 9.50s
material-ui - node (v16.17.1, x64)
Memory used 435,275k (± 0.00%) 435,266k (± 0.00%) -9k (- 0.00%) 435,235k 435,297k
Parse Time 1.65s (± 0.41%) 1.65s (± 0.61%) -0.00s (- 0.06%) 1.61s 1.66s
Bind Time 0.50s (± 1.18%) 0.51s (± 0.74%) +0.00s (+ 0.20%) 0.50s 0.51s
Check Time 11.93s (± 0.97%) 11.89s (± 0.74%) -0.04s (- 0.29%) 11.67s 12.04s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 14.08s (± 0.86%) 14.05s (± 0.67%) -0.03s (- 0.23%) 13.83s 14.21s
xstate - node (v16.17.1, x64)
Memory used 515,987k (± 0.01%) 516,086k (± 0.01%) +99k (+ 0.02%) 515,963k 516,236k
Parse Time 2.31s (± 0.53%) 2.31s (± 0.44%) -0.00s (- 0.22%) 2.29s 2.34s
Bind Time 0.83s (± 1.07%) 0.83s (± 1.06%) -0.00s (- 0.48%) 0.82s 0.86s
Check Time 1.37s (± 0.35%) 1.36s (± 0.69%) -0.01s (- 0.51%) 1.34s 1.38s
Emit Time 0.06s (± 0.00%) 0.06s (± 0.00%) 0.00s ( 0.00%) 0.06s 0.06s
Total Time 4.58s (± 0.35%) 4.56s (± 0.42%) -0.01s (- 0.33%) 4.52s 4.62s
System
Machine Namets-ci-ubuntu
Platformlinux 5.4.0-131-generic
Architecturex64
Available Memory16 GB
Available Memory15 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v16.17.1, x64)
Scenarios
  • Angular - node (v16.17.1, x64)
  • Compiler-Unions - node (v16.17.1, x64)
  • Monaco - node (v16.17.1, x64)
  • TFS - node (v16.17.1, x64)
  • material-ui - node (v16.17.1, x64)
  • xstate - node (v16.17.1, x64)
Benchmark Name Iterations
Current 51653 10
Baseline main 10

Developer Information:

Download Benchmark

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 27, 2022

Copy link
Copy Markdown
Contributor

Heya Anders Hejlsberg (@ahejlsberg), I've run the RWC suite on this PR - assuming you're on the TS core team, you can view the resulting diff here.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Andarist

Mateusz Burzyński (Andarist) commented Nov 27, 2022

Copy link
Copy Markdown
Contributor

I love it when „in the next day or two” means „in the next half an hour” 😉

ahejlsberg, andreialecu, JakobBruenker, fatcerberus, and tonivj5 reacted with laugh emoji
All reactions
  • 😄 5 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Andarist
Mateusz Burzyński (Andarist) reviewed Nov 27, 2022
View reviewed changes
Comment thread tests/cases/compiler/inKeywordNarrowingWithNoUncheckedIndexedAccess.ts
else {
obj.test; // undefined
}
}

@Andarist Mateusz Burzyński (Andarist) Nov 27, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've hoped that this solution would make it work (somehow through CFA) for at least the in variant:

function f5(obj: Record<string, string>, prop: string) {
    if (obj[prop]) {
        obj[prop]; // actual: string | undefined; expected: string
    }
    if (prop in obj) {
        obj[prop]; // actual: string | undefined; expected: string
    }
}

Is this kind of analysis supported for any kind of patterns today?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@fatcerberus Bruce Pascoe (fatcerberus) Nov 27, 2022

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obj[prop] where prop isn’t a hardcoded literal can’t be narrowed in any way to my knowledge. There are many, many issues attesting to that if you go looking, and it’s considered to be by design.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Andarist Mateusz Burzyński (Andarist) Nov 27, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ye, I've figured out as much - although I wonder how hard a limitation that is.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@fatcerberus Bruce Pascoe (fatcerberus) Nov 27, 2022

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything gets duped to #10530 which is kind of misleading since that issue was originally about bracketed access with a literal key (which has since been fixed), but the picture that develops after enough hunting is that it’s a performance sinkhole. See e.g. #25109 (comment)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ahejlsberg Anders Hejlsberg (ahejlsberg) Nov 27, 2022

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mateusz Burzyński (@Andarist) It works when prop is declared as a const with a singleton literal type, e.g. const prop = "foo", but no support beyond that. We could conceivably support non-singleton-literal-type const variables, but it isn't trivial.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tskj Tarjei Skjærset (tskj) Nov 28, 2022

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many, many issues attesting to that if you go looking, and it’s considered to be by design.

I understand that this seems difficult to implement and we might never get this feature, but I'm having a hard time understanding how this could be "by design". What I mean is, this seems like fairly straight forward JS and as far as I understand TypeScript's design goals, we really should be able to support (type correctly) this kind of code. I really am sympathetic to the fact that it might not be feasible to do in the current implementation, I'm just challenging the idea that we want it to be this way.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

JakobBruenker, ehoogeveen-medweb, and Bjorn-Strom reacted with thumbs up emoji
All reactions
  • 👍 3 reactions

@fatcerberus Bruce Pascoe (fatcerberus) Nov 28, 2022

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“By design” = ”intentional decision made during the design process”. Some such decisions are known compromises. I don’t use the phrase to imply it’s what the user wants, because nobody can even define that except for the individual users themselves, and the team can’t go around asking every single user before they’re allowed to label things “working as intended”. 😉

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tskj Tarjei Skjærset (tskj) Nov 28, 2022

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure there always needs to be compromises made in the design of the language, but this isn't that. This seems to be a limitation of the current implementation (an implementation detail if you will), not some inherent limitation in the design of the language itself.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@fatcerberus Bruce Pascoe (fatcerberus) Nov 28, 2022

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, the implementation is still part of the design of the TypeScript compiler in particular. Obviously a language wouldn't be intentionally designed that way in isolation, but that's a moot point since there's no TypeScript language specification. There used to be a formal spec a long time ago, but nowadays the implementation is the specification, and that's an entirely pragmatic design process. If the implementation is designed a certain way, in practice that means the language is too.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@fatcerberus Bruce Pascoe (fatcerberus) Nov 28, 2022

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...at least until someone gets fed up with the limitations and makes an alternative implementation. 😅

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Andarist
Mateusz Burzyński (Andarist) reviewed Nov 27, 2022
View reviewed changes
Comment thread tests/cases/compiler/inKeywordNarrowingWithNoUncheckedIndexedAccess.ts

function f4(obj: Record<string, string>) {
obj.test; // string | undefined
if (obj.hasOwnProperty("test")) {

@Andarist Mateusz Burzyński (Andarist) Nov 27, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, how does this one work if hasOwnProperty's signature is "just":

hasOwnProperty(v: PropertyKey): boolean;

what kind of sorcery is going on here? :D does this method have some special support in the compiler itself?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ahejlsberg Anders Hejlsberg (ahejlsberg) Nov 27, 2022

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this method have some special support in the compiler itself?

It does indeed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@fatcerberus Bruce Pascoe (fatcerberus) Nov 27, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m going to assume that feature was implemented early on since nowadays feature requests to special-case built-in methods (like Object.assign) get rejected if the special case can’t be represented in the method signature.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Andarist Mateusz Burzyński (Andarist) Nov 27, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, I found the code related to hasOwnProperty here

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 27, 2022

Copy link
Copy Markdown
Contributor

Anders Hejlsberg (@ahejlsberg) Here are the results of running the top-repos suite comparing main and refs/pull/51653/merge:

Everything looks good!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Andarist Mateusz Burzyński (Andarist) mentioned this pull request Nov 27, 2022
Improve checking of in operator combined with noUncheckedIndexedAccess #50936
Closed
gabritto
Gabriela Araujo Britto (gabritto) approved these changes Nov 28, 2022
View reviewed changes
@ahejlsberg
Anders Hejlsberg (ahejlsberg) requested review from Daniel Rosenwasser (DanielRosenwasser) and Ryan Cavanaugh (RyanCavanaugh) November 30, 2022 22:41
@ahejlsberg
Optimizing searching for undefinedType and missingType
36014d3
@ahejlsberg
Anders Hejlsberg (ahejlsberg) merged commit d43112a into main Dec 7, 2022
@ahejlsberg
Anders Hejlsberg (ahejlsberg) deleted the fix43614 branch December 7, 2022 21:38
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Dec 8, 2022
Design Meeting Notes, 11/30/2022 #51811
Closed
@ItsHarper Harper Andrews (ItsHarper) mentioned this pull request Apr 4, 2024
noUncheckedIndexedAccess does not narrow properly when iterating with for...in #58082
Closed
6 tasks done
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@gabritto Gabriela Araujo Britto (gabritto) gabritto approved these changes
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) Awaiting requested review from RyanCavanaugh
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Awaiting requested review from DanielRosenwasser
+3 more reviewers
@fatcerberus Bruce Pascoe (fatcerberus) fatcerberus left review comments
@Andarist Mateusz Burzyński (Andarist) Andarist left review comments
@tskj Tarjei Skjærset (tskj) tskj left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@ahejlsberg Anders Hejlsberg (ahejlsberg)

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

PR Backlog
Archived in project

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

noUncheckedIndexedAccess does not narrow properly with "prop" in obj

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@ahejlsberg @typescript-bot @Andarist @fatcerberus @gabritto @tskj
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.