GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||
GeezerVison is showing github.com — Const contexts for literal expressions by ahejlsberg · Pull Request #29510 · microsoft/TypeScript Conversation
Member
With this PR we introduce let x = 10 as const; // Type 10 let y = <const> [10, 20]; // Type readonly [10, 20] let z = { text: "hello" } as const; // Type { readonly text: "hello" } A
Const contexts do not otherwise affect types of expressions, and expressions in const contexts never have a contextual type. An expression
Note in particular that const contexts extend into nested array and object literals. For example, the declaration let obj = { x: 10, y: [20, 30], z: { a: { b: 42 } } } as const; corresponds to let obj: { readonly x: 10; readonly y: readonly [20, 30]; readonly z: { readonly a: { readonly b: 42; }; }; }; A
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Collaborator
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Contributor
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Member
Author
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Collaborator
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Sheetal Nandi (sheetalkamat)
added a commit
to microsoft/TypeScript-TmLanguage
that referenced
this pull request
|
There was an error while loading. Please reload this page.
|
Can it be also assumed in certain situations? Like: const a: 12; // type is 12 let x = { // Can this be `const`? // Either always, or only when it's `readonly x1: a`. It's fine either way. x1: a }; The situation here being assignment to a constant, and fine to require Also, one important situation (as mentioned with more details): function createAction<TType>(type: TType) { // Not concerned with return types here, but inferred argument type return { type: type }; } // Today this gets inferred as string, but can string literal be inferred as itself? const doStuff = createAction("DO_STUFF"); The situation here being sending a literal (string or number) to a function with argument type being generic and inferred from usage. It'll be nice to avoid |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Next step: allowing |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Is the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
That would require adding a new keyword for dubious benefit. Personally I think |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
But it was already a literal, before you added |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
be it a |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Question: if the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
to those who believes in |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Anders Hejlsberg (@ahejlsberg) Would there be any way to dynamically concatenate two literal types to produce a new literal type using this For example, in a function like... const concatLiterals = <A extends string, B extends string>(a: A, b: B) => `${a}{b}` as const // or (a + b) as const I'm assuming the dynamic nature of the above here would prevent this from working, and it would still widen the type to It would be great to have some sort of type-level concat helper for literals like this.... so that you could do something like: (a + b) as (const: A + B) or `${a}{b}` as (const: ConcatLit<A, B>) or something along those lines. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Apparently |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
I agree with I suspect that we'll end up with a quick fix like |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
oh let's not forget what readonly really is #13002 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Somehow I knew exactly what you linked to before I clicked on it. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This is probably out of the scope, but: |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
+1 for |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
A question regarding this.... interface foo { bar: string } function foobar(): ReadonlyArray<foo> { } Is it possible to return something like so function foobar(): foo as const { } Would really like to have a use case like the above included as well, as it would help a lot with immutability. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Is there a way to allow constant string to represent string literals? For example: In the case above, "mod" is of type "any" because the compiler doesn't recognize the string literal in the constant I may also add, it seems very difficult to import namespaces using dynamic imports, which I think is another terrible oversight. That doesn't even make sense. A namespace, while a type, is still a reference under the hood, and thus should still be importable dynamically somehow; perhaps like: I think all this would aim to better support dynamic imports "on demand" instead of modules forcibly loading every single module when some may not be needed at all, It could also help promote faster initial page loads in many cases. ;) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
James (@rjamesnw) since this PR is already merged I don't expect that you can get traction for anything new, but this is a good one actually. Can you please open a new issue for it? Thanks. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
You’re right, sorry, it also occurred to me and I was going to get to doing so asap. ;) Update: Issue created: #32401 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
When we want to use other TypeScript version and we do: `npm install [email protected] -D` But when we use new features like const casting microsoft/TypeScript#29510 it does not compile. Instead of that if we use: `yarn add [email protected]` Everything works
This commit refactors the SignatureType enum into an array of strings declared [as const](microsoft/TypeScript#29510). This allows the SignatureType type to be expressed as a union type, which works a bit better when parsing a users provided string. This is some simple refactoring in preparation for declarative function signatures.
This commit refactors the SignatureType enum into an array of strings declared [as const](microsoft/TypeScript#29510). This allows the SignatureType type to be expressed as a union type, which works a bit better when parsing a users provided string. This is some simple refactoring in preparation for declarative function signatures. BREAKING CHANGE: exported SignatureType type is converted from an enum to a union type
This commit refactors the SignatureType enum into an array of strings declared [as const](microsoft/TypeScript#29510). This allows the SignatureType type to be expressed as a union type, which works a bit better when parsing a users provided string. This is some simple refactoring in preparation for declarative function signatures. BREAKING CHANGE: exported SignatureType type is converted from an enum to a union type
There was an error while loading. Please reload this page.
|
Why would someone use let numbers = [1, 2, 3] as const; |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Excuse me,why is the error reported? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Because not every element of the array has an |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
I found out that let x = 12; x = 2 as const; x = 5; console.log(x); // 5 Also you'll get no error if the reassigned value is like the previous value: let x = [1, 2] as const; x = [1, 2]; // works |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Is there a way to type an array of objects declared using |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Type literal assertionas const to prevent type widening
Suggestion: a way to disable type widening in object literals
Syntax for hinting literal type inference
There was an error while loading. Please reload this page.