did some digging and found: https://github.com/microsoft/TypeScript/pull/11432 … tl;dr empty array literals internally create an "evolving array type". when performing an operation like `x.push(v)`, `x.unshift(v)` or `x[n] = v`, it tracks types that have been seen until finalized https://www.typescriptlang.org/play/#code/PTAEDsHtwWgUwG6QDYIJbgOagIYCc8cBPAGlwGdQ0AXUAdx0qloGM84dq4ATemgC1zhQcALYAHakVwFioZDTiFkAKGRxaADQCMoALygA2trIByfmlMBdANwqdAOnEBXcvwAU1PM7gBKG6AgIgSQeCpS4nCgACq6BhFwkABmoDoBQe7kXhjYAD4QzqIARkq+hlYqKkGIKOhYMoREahqpAEz6Rrb2rYYADFYd2naaPdoDBubapsOtTq4eXj7+4USRMe3xq4kpI+lgmdn1+eCFJXig+UWQKBzgZRVAA …
-
-
Prikaži ovu nitHvala. Twitter će to iskoristiti za poboljšanje vaše vremenske crte. PoništiPoništi
-
-
-
This nerd sniped me. I think TS array uses any, which can then be narrowed. Does this do what you want? https://www.typescriptlang.org/play/#code/MYGwhgzhAEDKAuZgGsA8AVaBTAHvLAdgCYxgECeAfNAN4BQ0j0ADgK4BGIAlsNBIigBc0dAG0AutAC80CQyZsIACwAUANzAhWWYegCUtaPKZN4SrhAB0-JMkuLVGrVj3HoAXzqe6ILPGg40rLiANzQAPThAcJk5HI49qzKKgCMemERUTgxFPEZJiaR0dAErAC27FgATnLAAPYE-NDswqUV1RJBOCF0Pn7Q5EEEWADucALIKumFUeTCCLaorATIBHUjBJR05InJafkFh0Vz44vLq+ubdPWN-sDzE6htlVXUMuQHh18Fxw9nK2sNpQgA …
-
haha sorry :p I want to be able to have TS infer it for me, so not quite
- Još 1 odgovor
Novi razgovor -
-
-
I _think_ arrays are special cased. So not possible to get same behaviour for user-defined types. Will try and dig up an official reference
-
Here are the release notes for TypeScript 2.1 when the feature was added. It mentions empty arrays specifically, rather than working on all generics https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html#improved-any-inference …
- Još 2 druga odgovora
Novi razgovor -
-
-
the array literal is special, but i believe you can use assert for narrowing down the type in arbitrary operations these days: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions …
-
but yeah, this is still limited to functions you control. TS doesn't expose anything that lets you control how the type evolves based on later usage (im also not sure how that would work if it did have that feature)
Kraj razgovora
Novi razgovor -
-
-
If you’re willing to change it to `y = x.push(1)` then it gets a lot easier: `push(T) -> []<T>`
Hvala. Twitter će to iskoristiti za poboljšanje vaše vremenske crte. PoništiPoništi
-
-
-
Another situation that would work is to type it out manually, like `new Stack<number>()`. Not ideal.
Hvala. Twitter će to iskoristiti za poboljšanje vaše vremenske crte. PoništiPoništi
-
-
-
I think you can hack it with assert in 3.7
Hvala. Twitter će to iskoristiti za poboljšanje vaše vremenske crte. PoništiPoništi
-
Čini se da učitavanje traje već neko vrijeme.
Twitter je možda preopterećen ili ima kratkotrajnih poteškoća u radu. Pokušajte ponovno ili potražite dodatne informacije u odjeljku Status Twittera.
i'm guessing this is either really simple or really difficult to do
Q: how would I type a generic class/constructor that is unknown/any on instantiation, but narrowed after an operation on that instance? ie how does this work?