fix the function overloading claim in the type-narrowing theory - #356
Merged
Conversation
Function overloading does not narrow types: the compiler does not transfer types from the overload signatures into the implementation body. The old example type-checked only because of `any`, and broke as soon as `any` was replaced with `unknown`. Reframe the claim and align the example with the 85-function-overloads lesson, which already uses `unknown` plus a number-first type guard. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #355
Репорт от студента: в теории урока «Сужение типа (Narrowing)» утверждалось, что перегрузка функций — пример работы сужения типов. Это неверно.
Что не так
Перегрузка сама по себе типы не сужает: компилятор не переносит типы из сигнатур перегрузок в тело реализации. Старый пример проходил проверку только за счёт
anyи ломался, как толькоanyзаменяли наunknown.Проверено на
tsc 5.8.3 --strict:any)any→unknownTS18046: 'a' is of type 'unknown'в веткеelse(a: string | number, …), безtypeofTS2339: Property 'toFixed' does not exist on type 'string | number'Третья проверка решающая: даже когда все сигнатуры перегрузок описывают одинаковые типы параметров, тело реализации видит полный union без какого-либо сужения. Всё сужение в примере давал
typeof, а не перегрузка.Что сделано
Тезис переформулирован на противоположный: перегрузка не сужает типы, и именно поэтому внутри реализации нужны защитники типа. Пример выровнен с предыдущим уроком
85-function-overloads, где то же самое уже написано корректно — сunknownи проверкойnumber-first.Правки в
ruиen(локалиesу курса нет). Код упражнения не менялся.Проверка
ru/README.mdиen/README.md, компилируется:tsc --strict --noEmit→exit=0rumdl check(конфиг изbase-image/common/.rumdl.toml, цельmarkdown-lint) →No issues found in 2 filesКстати, ошибка прошла CI потому, что проверка типов в CI не включена — #344.
🤖 Generated with Claude Code