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 — Improved checking of nullable operands in expressions by ahejlsberg · Pull Request #13483 · microsoft/TypeScript

Conversation

@ahejlsberg

@ahejlsberg Anders Hejlsberg (ahejlsberg) commented Jan 14, 2017 •
edited by DanielRosenwasser
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

This PR improves our checking of nullable operands in expressions. Specifically, we now error in the following cases:

  • If either operand of a + operator is nullable, and neither operand is of type any or string.
  • If either operand of a -, *, **, /, %, <<, >>, >>>, &, |, or ^ operator is nullable.
  • If either operand of a <, >, <=, >=, or in operator is nullable.
  • If the right operand of an instanceof operator is nullable.
  • If the operand of a +, -, ~, ++, or -- unary operator is nullable.

An operand is considered nullable if the type of the operand is null or undefined or a union type that includes null or undefined. Note that the union type case only only occurs in --strictNullChecks mode because null and undefined disappear from unions in classic type checking mode.

Fixes #12795.

Sorry, something went wrong.

Uh oh!

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

normalser, HerringtonDarkholme, zpdDG4gta8XKpMCd, pleerock, pyrocat101, alexdresko, ulrichb, matkoch, janaagaard75, saeedtabrizi, and marvinhagemeister reacted with thumbs up emoji SlurpTheo and saeedtabrizi reacted with hooray emoji
All reactions
  • 👍 11 reactions
  • 🎉 2 reactions
Anders Hejlsberg (ahejlsberg) added 6 commits January 13, 2017 15:05
@ahejlsberg
Improved undefined/null handling for arithmetic operators
bc7f86c
@ahejlsberg
Accept new baselines
a1e16d2
@ahejlsberg
Improved undefined/null handling for relational operators
8ce193c
@ahejlsberg
Accept new baselines
6dcaac6
@ahejlsberg
Improved undefined/null handling for unary operators
894ba85
@ahejlsberg
Accept new baselines
221c0d7
@msftclas Microsoft Contribution License Agreements (msftclas) added the cla-not-required label Jan 14, 2017
DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser) reviewed Jan 14, 2017
View reviewed changes
Comment thread tests/baselines/reference/additionOperatorWithNullValueAndInvalidOperator.errors.txt
~~~~~~~~
!!! error TS2365: Operator '+' cannot be applied to types 'boolean' and 'boolean'.
~~~~
!!! error TS2531: Object is possibly 'null'.

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Jan 14, 2017

Copy link
Copy Markdown
Member

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.

Object is definitely null, and typically we don't think of objects as being operands to arithmetic operations (whereas we do when it comes to primitives). Would be nice if we could be more precise on the error message.

Sorry, something went wrong.

Uh oh!

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

All reactions

@ahejlsberg Anders Hejlsberg (ahejlsberg) Jan 14, 2017

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.

Two orthogonal issues:

First, we could do extra work to omit the word possibly when the type is exactly null or undefined. However, it is very rare for someone to explicitly use null or undefined as an operand, so it's not clear that it really matters all that much. This is the type of error that only surfaces in a test.

Second, we could consider using the word value instead of object when reporting for expression operands--but it's really the same error that we use for obj.foo where obj may be null or undefined. The downside would be error message searchability, but since the error is pretty obvious that may not be an issue.

Sorry, something went wrong.

Uh oh!

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

All reactions
DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser) approved these changes Jan 14, 2017
View reviewed changes
mhegazy
Mohamed Hegazy (mhegazy) approved these changes Jan 16, 2017
View reviewed changes
@ahejlsberg
Anders Hejlsberg (ahejlsberg) merged commit d6fde0c into master Jan 17, 2017
@ahejlsberg
Anders Hejlsberg (ahejlsberg) deleted the operatorsAndNullableTypes branch January 17, 2017 01:03
@ahejlsberg Anders Hejlsberg (ahejlsberg) mentioned this pull request Feb 2, 2017
Failure to warn of possible numeric comparison with an optional function parameter #13829
Closed
@TimvdLippe Tim van der Lippe (TimvdLippe) mentioned this pull request Feb 6, 2017
Fix several compilation errors with Typescript 2.2 Polymer/polymer-analyzer#444
Merged
1 task done
@ahejlsberg Anders Hejlsberg (ahejlsberg) mentioned this pull request Feb 10, 2017
Number type narrowing from number|undefined within arithmetic conditions #13974
Closed
@TimvdLippe Tim van der Lippe (TimvdLippe) mentioned this pull request Feb 13, 2017
Suggestion: stricter operators #7989
Open
@gcnew Marin Marinov (gcnew) mentioned this pull request Mar 2, 2017
error TS2531: Object is possibly 'null' when using | operator (strictNullChecks are off) #14380
Closed
@microsoft Microsoft (microsoft) locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) DanielRosenwasser approved these changes
+1 more reviewer
@mhegazy Mohamed Hegazy (mhegazy) mhegazy approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

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

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

4 participants

@ahejlsberg @DanielRosenwasser @mhegazy @msftclas
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.