-
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
Proposal: A way to infer unique symbol and enum member #22118
Comments
Took me a minute to figure out what all you were asking for - it seems like at least a couple things:
|
#10195 seems good but might not work well with code formatter @s-ve 's answer seems definitely resolve my problem, I will verify it later. I'm felling strange that why there is no article about typed redux I've been seen noted or use this technique. As @sylvanaar said, some of concept I've asked is already rejected, that's okay, the core concept here is a way(if there is none) to explicitly let complier infer a literal as-is. That fullfil the most of use cases. Later today try to build a ActionCreator on @s-ve 's way, if success, I think this issue can be closed |
So if some of the concepts are out of scope, only preserve 1, 2, 3, 7, 8 in the above example. |
I do not think 8 is a thing we can do. any is the wildcard, it matches any constraint in the system. think of it as a union of all possible type. |
Umm, I know that any is a union type of all possible type, but I want to reject an infinity union since who write "literal" is actually want a certain type. This is useful in type narrowing and it mostly likes a mistake if an any is provided. So if reject any is also impossible, let's just ignore it. And for #10195, let's extend it to another case // 11. Not in generics
const n: literal string = 'okay' // has type 'okay' |
Sorry, I have verified the comments and updated my proposal. |
By the way, have you ever looked at the action creator library I use: |
For 3: The use of what is happening here is that since // 3.
function fn<T>(x: T): T { return x }
enum _C { A, B, C }
const c:C = fn(_C.A); For 7: the type of function fn<T extends Symbol>(x: T): T { return x }
const Symb = Symbol()
const g: typeof Symb = fn(Symb); In general, the issue here is the compiler deciphering the intent of the user. The intent can be ambiguous, for instance |
5 months passed, I've reconsidered this proposal. For 7: Oh... Widened before assignment ... Does it become impossible? |
|
Okay, so what about the |
as i mention earlier in #22118 (comment), the issue is not the inference, the issue is widening on the const. so you want the constant to have an explicit type annotation. |
Thank's for your replies |
Thanks to @s-ve ,I've resolved my questions. But there is still something we can discuss.
Let's focus on Example 3 and 7.
How to write type if you want a literal generics
Content below is useless now, I've learnt the correct way to write types I want(See above).
Search Terms:
type string literal
In some scenarios, we need to get type inference by a sure string, but not a
string
type.(Most famous one is ActionCreator in Redux, but not the only one.)
Now, typescript can infer the type of(function <T>(x: T): T {return x})('hello')
is the string, but if we want to get a more precise infer, it seems no way to do this.I'm sorry for my ignorance, typescript actually can do this.
I'll show how in my following examples.
This is not a formal language feature proposal. But a demo one is enough to explain what I mean.This how
ActionCreator
works now:Now TodoAddOne has type
(payload: any) => { type: "todo.add"; payload: any; }
With no duplication oftodo.add
, we get the same type as above.Though this is a small reduction, it goes useful when actions get greater.
How do I think this should work? (NO, Skip this section)
the string I want
, juststring
type), Typescript should emit an Error.The text was updated successfully, but these errors were encountered: