-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
const
modifier on type parameters
#51865
Conversation
@typescript-bot test this |
Heya @ahejlsberg, I've started to run the diff-based top-repos suite on this PR at ccf252f. You can monitor the build here. Update: The results are in! |
Heya @ahejlsberg, I've started to run the abridged perf test suite on this PR at ccf252f. You can monitor the build here. Update: The results are in! |
Heya @ahejlsberg, I've started to run the extended test suite on this PR at ccf252f. You can monitor the build here. |
Heya @ahejlsberg, I've started to run the diff-based user code test suite on this PR at ccf252f. You can monitor the build here. Update: The results are in! |
Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at ccf252f. You can monitor the build here. |
@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 |
Heya @ahejlsberg, I've started to run the perf test suite on this PR at ccf252f. You can monitor the build here. 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 |
Heya @RyanCavanaugh, I've started to run the tarball bundle task on this PR at ccf252f. You can monitor the build here. |
Hey @RyanCavanaugh, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
@typescript-bot perf test faster |
Heya @ahejlsberg, I've started to run the abridged perf test suite on this PR at f8fd1fd. You can monitor the build here. Update: The results are in! |
@ahejlsberg Here they are:Comparison Report - main..51865
System
Hosts
Scenarios
Developer Information: |
Could this fix #13923 too? |
OK, I tried testing more, and it seems it works fine in CJS, but not ESM. I will try to provide repro soon. |
hello, I tested it more, and found out why I got the error... TSC correctly handled that type, but VSCode for some reason didn't picked line:
So my issue is now solved. |
Hi @ahejlsberg , I have a hard time (from your initial example) discovering the rule that makes the inferred type of Thanks for helping here, please. |
@SalathielGenese notice that both use different functions, It's important to check what is the result of the same declare function bar<T>(obj: [T, T]): T;
const x4 = bar([{ a: 1, b: 'x' }, { a: 2, b: 'y' }]);
// ?^ const x4: { a: number; b: string; } and what is the result of the same thing with a regular declare function bar<T>(obj: [T, T]): T;
const x4 = bar([{ a: 1, b: 'x' } as const, { a: 2, b: 'y' } as const]);
// ^? const x4: { readonly a: 1; readonly b: "x"; } | { readonly a: 2; readonly b: "y"; } We can see here that it's consistent with the existing behavior of |
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. |
@RyanCavanaugh My point is that I have generic type aliases that expect to take in const type parameters, e.g. something like type Foo<T> = ... It would be great to be able to type |
Again, not understanding. You can't write this, it's syntactically illegal: type X = Foo<{ x: 3 } as const>; |
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.