- Notifications
You must be signed in to change notification settings - Fork12.9k
const
modifier on type parameters#51865
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
@typescript-bot test this |
typescript-bot commentedDec 12, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@ahejlsberg, I've started to run the diff-based top-repos suite on this PR atccf252f. You can monitor the buildhere. Update:The results are in! |
typescript-bot commentedDec 12, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@ahejlsberg, I've started to run the abridged perf test suite on this PR atccf252f. You can monitor the buildhere. Update:The results are in! |
typescript-bot commentedDec 12, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@ahejlsberg, I've started to run the extended test suite on this PR atccf252f. You can monitor the buildhere. |
typescript-bot commentedDec 12, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@ahejlsberg, I've started to run the diff-based user code test suite on this PR atccf252f. You can monitor the buildhere. Update:The results are in! |
typescript-bot commentedDec 12, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR atccf252f. You can monitor the buildhere. |
@ahejlsberg Here are the results of running the user test suite comparing Everything looks good! |
@ahejlsberg Here they are:Comparison Report - main..51865
System
Hosts
Scenarios
Developer Information: |
@typescript-bot perf test this |
typescript-bot commentedDec 12, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@ahejlsberg, I've started to run the perf test suite on this PR atccf252f. You can monitor the buildhere. Update:The results are in! |
@ahejlsberg Here are the results of running the top-repos suite comparing Everything looks good! |
@ahejlsberg Here they are:CompilerComparison Report - main..51865
System
Hosts
Scenarios
TSServerComparison Report - main..51865
System
Hosts
Scenarios
StartupComparison Report - main..51865
System
Hosts
Scenarios
Developer Information: |
@typescript-bot pack this |
typescript-bot commentedDec 12, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@RyanCavanaugh, I've started to run the tarball bundle task on this PR atccf252f. You can monitor the buildhere. |
typescript-bot commentedDec 12, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Hey@RyanCavanaugh, I've packed this intoan installable tgz. You can install it for testing by referencing it in your
and then running There is also a playgroundfor this build and annpm module you can use via |
@typescript-bot perf test faster |
typescript-bot commentedDec 13, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@ahejlsberg, I've started to run the abridged perf test suite on this PR atf8fd1fd. You can monitor the buildhere. Update:The results are in! |
@ahejlsberg Here they are:Comparison Report - main..51865
System
Hosts
Scenarios
Developer Information: |
mon-jai commentedDec 13, 2022
Could this fix#13923 too? |
JavaScriptBach commentedNov 9, 2023
This is a great feature. Would you consider allowing it for type aliases as well? Example use case:Automattic/mongoose#12782 |
@JavaScriptBach huh? There's no straightforward correspondence from this feature to something that would apply to type aliases. |
JavaScriptBach commentedNov 9, 2023
@RyanCavanaugh My point is that I have generic type aliases that expect to take in const type parameters, e.g. something like typeFoo<T>= ... It would be great to be able to type |
Again, not understanding. You can't write this, it's syntactically illegal: typeX=Foo<{x:3}asconst>; |
Summary:Adds support for [`const` modifier on type parameters](microsoft/TypeScript#51865). E.g.```function foo<const T>(x: T): [T] {...}```We parse an optional `const` keyword right before parsing the variance of a type parameter.```cd ~/fbsource/xplat/hermes/unsupported/tools/rustgenbuck run //xplat/hermes/unsupported/tools/rustgen:rustgen -- ffi > ../../juno/crates/hermes/src/parser/generated_ffi.rsbuck run //xplat/hermes/unsupported/tools/rustgen:rustgen -- cvt > ../../juno/crates/juno/src/hparser/generated_cvt.rs```Reviewed By: avpDifferential Revision: D66739150fbshipit-source-id: 66411ee424cea3ab19a1f8c2ab09d80abdf2737a
Summary:JS changes for support for [`const` modifier on type parameters](microsoft/TypeScript#51865). E.g.```function foo<const T>(x: T): [T] {...}```Reviewed By: SamChou19815Differential Revision: D66856337fbshipit-source-id: 109911abd8400b97a35d20d611b376dd12c46bed
Summary:Original Author: pvekris@meta.comOriginal Git:3a24531Original Reviewed By: avpOriginal Revision: D66739150Adds support for [`const` modifier on type parameters](microsoft/TypeScript#51865). E.g.```function foo<const T>(x: T): [T] {...}```We parse an optional `const` keyword right before parsing the variance of a type parameter.```cd ~/fbsource/xplat/hermes/unsupported/tools/rustgenbuck run //xplat/hermes/unsupported/tools/rustgen:rustgen -- ffi > ../../juno/crates/hermes/src/parser/generated_ffi.rsbuck run //xplat/hermes/unsupported/tools/rustgen:rustgen -- cvt > ../../juno/crates/juno/src/hparser/generated_cvt.rs```Reviewed By: fbmal7Differential Revision: D67867901fbshipit-source-id: 4683aee20adfed18e0a6343b4fdacf8e016015df
Summary:Original Author: pvekris@meta.comOriginal Git:d9b1826Original Reviewed By: SamChou19815Original Revision: D66856337JS changes for support for [`const` modifier on type parameters](microsoft/TypeScript#51865). E.g.```function foo<const T>(x: T): [T] {...}```Reviewed By: fbmal7Differential Revision: D67867911fbshipit-source-id: a711201cad644a6659c8b443a349f9f70f251511
Summary:Adds support for [`const` modifier on type parameters](microsoft/TypeScript#51865). E.g.```function foo<const T>(x: T): [T] {...}```We parse an optional `const` keyword right before parsing the variance of a type parameter.```cd ~/fbsource/xplat/hermes/unsupported/tools/rustgenbuck run //xplat/hermes/unsupported/tools/rustgen:rustgen -- ffi > ../../juno/crates/hermes/src/parser/generated_ffi.rsbuck run //xplat/hermes/unsupported/tools/rustgen:rustgen -- cvt > ../../juno/crates/juno/src/hparser/generated_cvt.rs```Reviewed By: avpDifferential Revision: D66739150fbshipit-source-id: 66411ee424cea3ab19a1f8c2ab09d80abdf2737a
Summary:JS changes for support for [`const` modifier on type parameters](microsoft/TypeScript#51865). E.g.```function foo<const T>(x: T): [T] {...}```Reviewed By: SamChou19815Differential Revision: D66856337fbshipit-source-id: 109911abd8400b97a35d20d611b376dd12c46bed
Uh oh!
There was an error while loading.Please reload this page.
With this PR we implement a new
const
modifier for type parameters. In a function, method, or constructor invocation, when a literal expression in an argument is contextually typed by aconst
type parameter, the literal expression is given the most precise type possible, similar to having applied anas const
assertion (see#29510). The kinds of literal expressions affected by aconst
contextual type are string, numeric, and boolean literal values, array literals, and object literals.The
const
modifier is permitted on type parameters of functions, methods, and classes. It is an error to apply theconst
modifier to type parameters of interfaces and type aliases.Given the function declarations
the invocations
infer the types
A
const
type parameter has no effect on variables or other kinds of expressions used as function arguments. For example:The example above still requires writing
[1, 2, 3] as const
to infer typereadonly [1, 2, 3]
fora
.When a
const
type parameter is constrained to an array type, that array type should include areadonly
modifier; otherwise, inferences for the type parameter will fail to meet the constraint. For example:Without the
readonly
modifier in the constraint, inference defaults tounknown[]
because an inferred readonly tuple type wouldn't be assignable tounknown[]
.Fixes#30680.
Fixes#41114.