Microsoft/TypeScript

Type loosening regression with union types in TS 4.2

Open

#43,185 opened on 2021幎3月10日

GitHub で芋る
 (5 comments) (1 reaction) (0 assignees)TypeScript (6,726 forks)batch import
BugDomain: check: Type InferenceHelp Wanted

Repository metrics

Stars
 (48,455 stars)
PR merge metrics
 (平均マヌゞ 6d 17h) (30d で 9 merged PRs)

説明

Bug Report

🔎 Search Terms

  • union type

🕗 Version & Regression Information

  • bug exists in 4.2
  • works in 4.1

⏯ Playground Link

https://www.typescriptlang.org/play?target=5&jsx=0&ts=4.2.3#code/FAFwngDgpgBAogSxACygJwPIGsA8AlKAZwFcAbEAPhgF4YBvGcaALhgCI4BJAFQAk48AfQwBpNgG4YaImRCsCJcjBgBfUJFiIU6OGjQB7NDl0G0VWgyZRWHHvyEC8GPBKkzyrE4bVX4SVEYKsgA08HqG5n7amLhB5FQAPlEBXkapFMDAAMb6AHaEIDBQ-jrhaDQwONwUABTopqzcAJSeJWipVeZUNXTAAJBWNlx8AoKOzmzB-dKKckVlUypNmTn5hQBmxLlZAIwVNS3J6Di5xAC2AEbooRw7bF30-QD0TzDEhAi5AOYwqKTQ5U22xACDyMAu0gAhlhCIxUIwNDBPut0FBtlBQkgkbDkaiACYwSGwgpoT5faZQEDENC5IptVI1W5sZZqbJ5AowIFZABM+0OWgCJ3OVzQNzgdwevT6LxgeIQ0iyIFIYDcVJpZLhsH0FwAVlBFTAAO6GGEUtW0qUDDRDOyjcYuKZ9PozWRDO6OtSs1YcrkAZj5rWiQsu13Y4vuNCoUpl7w1AAMiTBvSA40aTYQzdTacVogymYTYcmWSt2RstlkACwBo7tMo4JmS56vQ2oWmx76amsdOBURPSc0I6CYtZQSEE-TrGv4UI9zGFSGkQj6NNoU3OylZum5sqM8MsmDAIA

When using union types with helper function constructing one of the type, the type is incorrectly loosened from string value type to string (func1). Directly returning the shape (func2) or using as const (func3) fixes it. Also when simplifying and removing the union (func4), it starts working.

This was not problem in 4.1, errors only in 4.2

コントリビュヌタヌガむド