-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Inferred constructable types #47559
Comments
You realize all of this can be solved by just… using the current type system how it's supposed to be used |
@xiBread are you developing a competitor tool? Link it! |
? |
I'm still learning the irks & quirks of typescript - and btw, I just figured out that type construction for objects is already implemented to some degree - but only within import { YesIAmAType } from "./types"
/** @type {YesIAmAType} */
var myVariable The import is dropped by the bundler. JSdocs offer additional ways to define types inside comments. I don't know about you guys but I'd rather have useful type annotations in comments than |
??? are you trolling |
If you just want helpful hints instead of validation, then JSDoc is the right fit for you! |
@xiBread what do you mean trolling? Most of my issues deal with reported errors that are none. |
See also #12416 For top-level types, this is just not writing a type annotation: var something1;
something1 = 1
something1 = "a" For adding properties onto existing objects, this is an intentional opt-out for TS; the complexity of doing this in all possible cases would be too much of a performance/complexity trade-off for what it allows you to do. Anyway it does sound like JS Doc is a much better fit for what you want out of a tool. |
@RyanCavanaugh That is equivalent to saying |
Btw, I just stumbled on ((you can even type function statements in .js files)) which delivers on ALL points in #46941. The more I look at it... the number of features available in |
No, it isn't. You can assign into it any type, but it behaves as a non-any on any read operation, which is the behavior you're asking for. var something1;
something1 = 1
something1 = "a"
// Error
something1.foo; |
@RyanCavanaugh You probably should have noted that this is a |
This issue has been marked as "Declined" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@RyanCavanaugh Maybe you can clarify something from this thread. Philosophically, does the TypeScript team consider |
They're not intended to be identical. There are probably a dozen or so behaviors that are different between TS and JS, and this is on purpose to try to suit the general different needs of people who are and aren't using type annotations to indicate their intent. Making those individually-controllable means expanding our configuration matrix by a factor of ~4,000, and opens the door to many more "bad" configuration spaces that people can find themselves in, plus the cost of documenting, reasoning about, and supporting all those possible configurations. It's not something we're particularly interested in doing. |
Suggestion
This issue proposes constructable inferred types. It solves this:
Proposed syntax which says "This is not a finished type. This is a type under construction.";
Then:
Basically, all object types are combined "&", all primitives are added as alternatives "|".
An inferred constructable type also means, and the "unfinished" typehilt also says: "The code defines the schema of the type".
So you don't need an interface or need to keep an interface that you have laying around somewhere in sync with the code that defines it. You can reference the type via
type t = typeof theVariableThatHoldsIt
.Current Workaround:
Typescript already has the ability to infer a constructed type, but only with the identifier of a function statement and without narrowing. This can be expanded to all variables.
🔍 Search Terms
inferred types, constructable type, extendable object
✅ Viability Checklist
My suggestion meets these guidelines:
-->
The text was updated successfully, but these errors were encountered: