-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Throw types #40468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Throw types #40468
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
1befac3
feat: introduce throw type
Jack-Works 2a9f067
feat: handle throw type in call expression
Jack-Works f1286b6
feat: add typeof modifier in template string
Jack-Works 1c1c653
chore: accept baseline
Jack-Works 22c76cf
fix: un-instantiate throw type in return type
Jack-Works 241cc97
fix: throw type parse error at true branch of conditional type
Jack-Works 530c395
feat: add throw type check for identifier
Jack-Works d69f2b0
fix: lint error
Jack-Works b5f4fce
feat: able to reference translated diagnostic message
Jack-Works b39ff3d
feat: support category in throw type
Jack-Works 1950b0f
fix: base case of throw types
Jack-Works 9faf8c4
test: add test case for prevent call expr usage
Jack-Works daac7e7
test: add test for identifier use case
Jack-Works 7d785eb
feat: improve throw type on return type
Jack-Works c22687f
feat: improve throw type on argument position
Jack-Works c21c1b7
feat: add property access check for throw type
Jack-Works 94aa731
fix: infinite loop in dropThrowTypeInConditionalType
Jack-Works 56bfcd7
fix: throw type in fn param, co-assign in cond type
Jack-Works bd0f330
feat: reduce throw type in intersection
Jack-Works cdd5cd0
Merge branch 'master' into throwTypes
Jack-Works cfd61cb
Merge branch 'master' into throwTypes
Jack-Works 800fb0c
chore: accept new baseline
Jack-Works e391bc4
feat: add intrinsic type
Jack-Works e5e4c0b
Merge branch 'main' into throwTypes
Jack-Works 14c00cc
chore: update baseline
Jack-Works 2303958
fix: linter
Jack-Works e138279
Merge branch 'main' into throwTypes
Jack-Works c2ae402
test: update tests
Jack-Works 27df052
Merge branch 'main' into throwTypes
Jack-Works 4f01135
Merge branch 'main' into throwTypes
Jack-Works e7684dc
fix: test failed
Jack-Works 361bb7b
Merge branch 'main' into throwTypes
Jack-Works ea2c58d
fix: lint
Jack-Works a995c42
fix: test failed
Jack-Works File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
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
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
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
33 changes: 33 additions & 0 deletions
33
tests/baselines/reference/throwType_function_parameter.errors.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
tests/cases/compiler/throwType_function_parameter.ts(4,24): error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'. | ||
tests/cases/compiler/throwType_function_parameter.ts(4,24): error TS2899: Type instantiated results in a throw type saying: | ||
No zero | ||
tests/cases/compiler/throwType_function_parameter.ts(13,4): error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'. | ||
tests/cases/compiler/throwType_function_parameter.ts(13,4): error TS2899: Type instantiated results in a throw type saying: | ||
"found ""str"" | ||
|
||
|
||
==== tests/cases/compiler/throwType_function_parameter.ts (4 errors) ==== | ||
function checkParameterPosition<T extends number>(y: T extends 1234 ? throw 'No zero' : T) { | ||
y.toExponential() | ||
} | ||
checkParameterPosition(1234) | ||
~~~~ | ||
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'. | ||
~~~~ | ||
!!! error TS2899: Type instantiated results in a throw type saying: | ||
!!! error TS2899: No zero | ||
checkParameterPosition(12345678) | ||
|
||
type MustNumber<T> = T extends number ? T : throw `"found "${TypeToString<T>}"` | ||
type MustNumber2<T> = T extends number ? T : throw `"found "${TypeToString<T>}"` | ||
function f2<T>(a: MustNumber<T>, b: MustNumber2<T>) { | ||
a = b | ||
} | ||
|
||
f2('str', {}) | ||
~~~~~ | ||
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'. | ||
~~~~~ | ||
!!! error TS2899: Type instantiated results in a throw type saying: | ||
!!! error TS2899: "found ""str"" | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//// [throwType_function_parameter.ts] | ||
function checkParameterPosition<T extends number>(y: T extends 1234 ? throw 'No zero' : T) { | ||
y.toExponential() | ||
} | ||
checkParameterPosition(1234) | ||
checkParameterPosition(12345678) | ||
|
||
type MustNumber<T> = T extends number ? T : throw `"found "${TypeToString<T>}"` | ||
type MustNumber2<T> = T extends number ? T : throw `"found "${TypeToString<T>}"` | ||
function f2<T>(a: MustNumber<T>, b: MustNumber2<T>) { | ||
a = b | ||
} | ||
|
||
f2('str', {}) | ||
|
||
|
||
//// [throwType_function_parameter.js] | ||
function checkParameterPosition(y) { | ||
y.toExponential(); | ||
} | ||
checkParameterPosition(1234); | ||
checkParameterPosition(12345678); | ||
function f2(a, b) { | ||
a = b; | ||
} | ||
f2('str', {}); |
53 changes: 53 additions & 0 deletions
53
tests/baselines/reference/throwType_function_parameter.symbols
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
=== tests/cases/compiler/throwType_function_parameter.ts === | ||
function checkParameterPosition<T extends number>(y: T extends 1234 ? throw 'No zero' : T) { | ||
>checkParameterPosition : Symbol(checkParameterPosition, Decl(throwType_function_parameter.ts, 0, 0)) | ||
>T : Symbol(T, Decl(throwType_function_parameter.ts, 0, 32)) | ||
>y : Symbol(y, Decl(throwType_function_parameter.ts, 0, 50)) | ||
>T : Symbol(T, Decl(throwType_function_parameter.ts, 0, 32)) | ||
>T : Symbol(T, Decl(throwType_function_parameter.ts, 0, 32)) | ||
|
||
y.toExponential() | ||
>y.toExponential : Symbol(Number.toExponential, Decl(lib.es5.d.ts, --, --)) | ||
>y : Symbol(y, Decl(throwType_function_parameter.ts, 0, 50)) | ||
>toExponential : Symbol(Number.toExponential, Decl(lib.es5.d.ts, --, --)) | ||
} | ||
checkParameterPosition(1234) | ||
>checkParameterPosition : Symbol(checkParameterPosition, Decl(throwType_function_parameter.ts, 0, 0)) | ||
|
||
checkParameterPosition(12345678) | ||
>checkParameterPosition : Symbol(checkParameterPosition, Decl(throwType_function_parameter.ts, 0, 0)) | ||
|
||
type MustNumber<T> = T extends number ? T : throw `"found "${TypeToString<T>}"` | ||
>MustNumber : Symbol(MustNumber, Decl(throwType_function_parameter.ts, 4, 32)) | ||
>T : Symbol(T, Decl(throwType_function_parameter.ts, 6, 16)) | ||
>T : Symbol(T, Decl(throwType_function_parameter.ts, 6, 16)) | ||
>T : Symbol(T, Decl(throwType_function_parameter.ts, 6, 16)) | ||
>TypeToString : Symbol(TypeToString, Decl(lib.es5.d.ts, --, --)) | ||
>T : Symbol(T, Decl(throwType_function_parameter.ts, 6, 16)) | ||
|
||
type MustNumber2<T> = T extends number ? T : throw `"found "${TypeToString<T>}"` | ||
>MustNumber2 : Symbol(MustNumber2, Decl(throwType_function_parameter.ts, 6, 79)) | ||
>T : Symbol(T, Decl(throwType_function_parameter.ts, 7, 17)) | ||
>T : Symbol(T, Decl(throwType_function_parameter.ts, 7, 17)) | ||
>T : Symbol(T, Decl(throwType_function_parameter.ts, 7, 17)) | ||
>TypeToString : Symbol(TypeToString, Decl(lib.es5.d.ts, --, --)) | ||
>T : Symbol(T, Decl(throwType_function_parameter.ts, 7, 17)) | ||
|
||
function f2<T>(a: MustNumber<T>, b: MustNumber2<T>) { | ||
>f2 : Symbol(f2, Decl(throwType_function_parameter.ts, 7, 80)) | ||
>T : Symbol(T, Decl(throwType_function_parameter.ts, 8, 12)) | ||
>a : Symbol(a, Decl(throwType_function_parameter.ts, 8, 15)) | ||
>MustNumber : Symbol(MustNumber, Decl(throwType_function_parameter.ts, 4, 32)) | ||
>T : Symbol(T, Decl(throwType_function_parameter.ts, 8, 12)) | ||
>b : Symbol(b, Decl(throwType_function_parameter.ts, 8, 32)) | ||
>MustNumber2 : Symbol(MustNumber2, Decl(throwType_function_parameter.ts, 6, 79)) | ||
>T : Symbol(T, Decl(throwType_function_parameter.ts, 8, 12)) | ||
|
||
a = b | ||
>a : Symbol(a, Decl(throwType_function_parameter.ts, 8, 15)) | ||
>b : Symbol(b, Decl(throwType_function_parameter.ts, 8, 32)) | ||
} | ||
|
||
f2('str', {}) | ||
>f2 : Symbol(f2, Decl(throwType_function_parameter.ts, 7, 80)) | ||
|
44 changes: 44 additions & 0 deletions
44
tests/baselines/reference/throwType_function_parameter.types
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
=== tests/cases/compiler/throwType_function_parameter.ts === | ||
function checkParameterPosition<T extends number>(y: T extends 1234 ? throw 'No zero' : T) { | ||
>checkParameterPosition : <T extends number>(y: T extends 1234 ? throw 'No zero' : T) => void | ||
>y : T extends 1234 ? never : T | ||
|
||
y.toExponential() | ||
>y.toExponential() : string | ||
>y.toExponential : (fractionDigits?: number) => string | ||
>y : T extends 1234 ? never : T | ||
>toExponential : (fractionDigits?: number) => string | ||
} | ||
checkParameterPosition(1234) | ||
>checkParameterPosition(1234) : void | ||
>checkParameterPosition : <T extends number>(y: T extends 1234 ? never : T) => void | ||
>1234 : 1234 | ||
|
||
checkParameterPosition(12345678) | ||
>checkParameterPosition(12345678) : void | ||
>checkParameterPosition : <T extends number>(y: T extends 1234 ? never : T) => void | ||
>12345678 : 12345678 | ||
|
||
type MustNumber<T> = T extends number ? T : throw `"found "${TypeToString<T>}"` | ||
>MustNumber : MustNumber<T> | ||
|
||
type MustNumber2<T> = T extends number ? T : throw `"found "${TypeToString<T>}"` | ||
>MustNumber2 : MustNumber2<T> | ||
|
||
function f2<T>(a: MustNumber<T>, b: MustNumber2<T>) { | ||
>f2 : <T>(a: MustNumber<T>, b: MustNumber2<T>) => void | ||
>a : MustNumber<T> | ||
>b : MustNumber2<T> | ||
|
||
a = b | ||
>a = b : MustNumber2<T> | ||
>a : MustNumber<T> | ||
>b : MustNumber2<T> | ||
} | ||
|
||
f2('str', {}) | ||
>f2('str', {}) : void | ||
>f2 : <T>(a: MustNumber<T>, b: MustNumber2<T>) => void | ||
>'str' : "str" | ||
>{} : {} | ||
|
24 changes: 24 additions & 0 deletions
24
tests/baselines/reference/throwType_function_return.errors.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
tests/cases/compiler/throwType_function_return.ts(5,1): error TS2899: Type instantiated results in a throw type saying: | ||
Cannot divided by 0 | ||
tests/cases/compiler/throwType_function_return.ts(10,1): error TS2899: Type instantiated results in a throw type saying: | ||
Wrong | ||
|
||
|
||
==== tests/cases/compiler/throwType_function_return.ts (2 errors) ==== | ||
function checkedDivide<T extends number>(x: T): T extends 0 ? throw 'Cannot divided by 0' : number { | ||
if (x === 0) throw new Error('') | ||
return 5 / x | ||
} | ||
checkedDivide(0) | ||
~~~~~~~~~~~~~~~~ | ||
!!! error TS2899: Type instantiated results in a throw type saying: | ||
!!! error TS2899: Cannot divided by 0 | ||
checkedDivide(1) | ||
|
||
const theAnswerToEverything = <T>(x: T): T extends 42 ? T : throw "Wrong" => x | ||
theAnswerToEverything(42 as const) | ||
theAnswerToEverything('') | ||
~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2899: Type instantiated results in a throw type saying: | ||
!!! error TS2899: Wrong | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//// [throwType_function_return.ts] | ||
function checkedDivide<T extends number>(x: T): T extends 0 ? throw 'Cannot divided by 0' : number { | ||
if (x === 0) throw new Error('') | ||
return 5 / x | ||
} | ||
checkedDivide(0) | ||
checkedDivide(1) | ||
|
||
const theAnswerToEverything = <T>(x: T): T extends 42 ? T : throw "Wrong" => x | ||
theAnswerToEverything(42 as const) | ||
theAnswerToEverything('') | ||
|
||
|
||
//// [throwType_function_return.js] | ||
function checkedDivide(x) { | ||
if (x === 0) | ||
throw new Error(''); | ||
return 5 / x; | ||
} | ||
checkedDivide(0); | ||
checkedDivide(1); | ||
var theAnswerToEverything = function (x) { return x; }; | ||
theAnswerToEverything(42); | ||
theAnswerToEverything(''); |
Oops, something went wrong.
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.
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.
IMO,
NameOf<T>
might be a better name.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.
That issue requires a runtime behavior but this one is only type-level. I don't know if it is a good idea to use
NameOf