From 7addca63aed56153397d41ad93db272a2ded0f54 Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Tue, 15 Mar 2022 14:43:46 -0700 Subject: [PATCH] Update issue templates (#48261) --- .github/ISSUE_TEMPLATE/Bug_report.md | 2 + .github/ISSUE_TEMPLATE/Feature_request.md | 2 + .github/ISSUE_TEMPLATE/lib_change.md | 113 +++++++++--------- .../types-not-correct-in-with-callback.md | 47 ++++++++ 4 files changed, 109 insertions(+), 55 deletions(-) create mode 100644 .github/ISSUE_TEMPLATE/types-not-correct-in-with-callback.md diff --git a/.github/ISSUE_TEMPLATE/Bug_report.md b/.github/ISSUE_TEMPLATE/Bug_report.md index 6cc9e4cd7c87a..d920d7ec3ddff 100644 --- a/.github/ISSUE_TEMPLATE/Bug_report.md +++ b/.github/ISSUE_TEMPLATE/Bug_report.md @@ -4,7 +4,9 @@ about: Create a report to help us improve TypeScript title: '' labels: '' assignees: '' + --- + # Bug Report - -## Configuration Check - - -My compilation *target* is `ES2015` and my *lib* is `the default`. - -## Missing / Incorrect Definition - - - -## Sample Code - - - -## Documentation Link - - +--- +name: Library change +about: Fix or improve issues with built-in type definitions like `lib.dom.d.ts`, `lib.es6.d.ts`, + etc. +title: '' +labels: '' +assignees: '' + +--- + +# lib Update Request + + + +## Configuration Check + + +My compilation *target* is `ES2015` and my *lib* is `the default`. + +## Missing / Incorrect Definition + + + +## Sample Code + + + +## Documentation Link + + diff --git a/.github/ISSUE_TEMPLATE/types-not-correct-in-with-callback.md b/.github/ISSUE_TEMPLATE/types-not-correct-in-with-callback.md new file mode 100644 index 0000000000000..46b34ccb3fcb0 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/types-not-correct-in-with-callback.md @@ -0,0 +1,47 @@ +--- +name: Types Not Correct in/with Callback +about: TypeScript assuming the wrong type either after a callback runs, or within + a callback +title: '' +labels: Duplicate +assignees: '' + +--- + +TypeScript has two narrowing-related behaviors that are both intentional. Please do not log additional bugs on this; see #9998 for more discussion. + +The first is that *narrowings are not respected in callbacks*. In other words: +```ts +function fn(obj: { name: string | number }) { + if (typeof obj.name === "string") { + // Errors + window.setTimeout(() => console.log(obj.name.toLowerCase()); + } +} +``` +This is intentional since the value of `obj.name` "could" change types between when the narrowing occurred and when the callback was invoke. See also #11498 + +The second is that *function calls do not reset narrowings*. In other words: +```ts +function fn(obj: { name: string | number }) { + if (typeof obj.name === "string") { + console.log("Here"); + // Does not error + console.log(obj.name.toLowerCase()); + } +} +``` +This is intentional behavior, *even though `console.log` could have mutated obj*. This rule is consistently applied, even with the function is in-principle inspectable to actually have side effects +```ts +function fn(obj: { name: string | number }) { + if (typeof obj.name === "string") { + mut(); + // Does not error + console.log(obj.name.toLowerCase()); + } + + function mut() { + obj.name = 42; + } +} +```