-
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
Arranged Field Definitions #58019
Comments
As a German I strongly support this flag. |
I think this should get label Great Suggestion of something similar. It would be great to tag some great suggestions from past years with that same label so it would be easier to find them! |
Finally! 👍 This will make my life (huge regexp based code changes across tens or hundreds of files) easier as TS would solve problems with properties order. Only two points:
For later I can see how it would "fix" it with interface OrderedOverloads {
check(val: string, length?: number): boolean;
check(val: 'works'): number;
} in that order; Could you please add it in scope of this proposal?! 🙏 |
How will this interact with mapped types? I'm trying to work out how I can write some helper types. Will something like this be available? type Scrambled<T> = { [k in keyof T]: T[k] } in random order |
Thank goodness you folks finally came to your senses. Now we have a firm foundation for type UnionToTuple<T> = `[${T in that order}]` extends `${infer U extends any[]}` ? U : never; where It even lets me amend my answer to a recent question about the full set of meanings for the TypeScript My only concern is that you've introduced the interface Cloneable { clone(): that }
class A implements Cloneable {
constructor(readonly a: number) { }
clone(): A { return new A(this.a) } // okay now
} |
I forgot that (under type T = "typescript rocks";
const p: T = "tricky prospects"; // OK (anagram subtyping)
type U = "typescript is cool" in that order;
const q: U = "cryptic stool piles"; // Error, letters are not in the right order |
Wait that doesn't sound right, clearly |
LGTM |
This went from foo to oof quickly. |
This makes a difference in V8 deopt, and could be useful to have in a linter or typechecker in some form, even if the proposal here is more for laughs. |
To expand on what @nmain says, see e.g. here: https://romgrk.com/posts/optimizing-javascript/#2-avoid-different-shapes by @romgrk or the really in-depth article he links to: https://mrale.ph/blog/2015/01/11/whats-up-with-monomorphism.html |
I, too, long for the following fine day: interface Foo {
a?: string,
b: number;
} in that order;
const foo: Foo = { b: 123 }; // error, maybe?
foo.a = "abc"; // error, probably! |
I was looking for some kind of Sorted intrinsic utility for tuples and stumbled on this... I hate it soooo much 😆 |
Arranged Field Definitions
In TypeScript 5.5.555 (codename "five by five"), we're excited to introduce a new feature we're calling "arranged field definitions", or AFD. Let's look at the feature and how we got here.
Background
Ever since the first prototype of TypeScript, our goal has been to enumerate the ways that different values exist at runtime and to provide a way for programmers, on their own, to describe these key differences. If there are two values you can tell apart at runtime, TypeScript should be able to tell them apart too.
Inspiration
We were trying to find a good string padding library when we ran across this piece of code:
What was the purpose of this? What genius wrote it? Why use
null
instead ofSymbol.for("null")
? Does this count as point-free style, or just zero points? Those answers are lost to the sands of time. Regardless, the functionality of this snippet wasn't apparent until we started playing with it:It was a brilliant "Ah-ha!" moment for everyone on the team. I think Anders even spilled his coffee. Someone in JS was enforcing the arrangement of property keys in objects - in this case, that they be sorted. This was uncharted territory for us, and we immediately set to work on building a type representation for it.
TypeScript needed a way for library authors to enforce key ordering. If you accept
{ x: 1, y: 2 }
but not{ y: 2, x: 1 }
, TypeScript should have a type for that. No typechecker for JavaScript is complete unless it can represent this pattern.Syntax
Initial syntactic proposals for AFD centered around a new top-level declaration, using
tyqe
with aq (Q)
instead ofp (P)
intype
to represent that the fields had to be "in an orderly Queue":In our opinion, concerns about reabidility here are misplaced, since modern code editors can use ligatures to replace
tyqe
with something more customized based on user preference.However, much to our dismay, multiple TC39 proposals in the works are interested in using this keyword.
We then tried modifying the
interface
declaration form:This has some big advantages:
then
is a familiar keyword to Pascal programmers, a core demographic of TypeScripta
beforeb
andx
beforey
, buta x b y
would be a legal order), where only some keys would be subject to ordering constraints. People enjoy this level of complexity, and more complexity equals more job security for us. Win/win.then
, and we're not aware of any (TODO:resolve
to look into this)Unfortunately, we hit another roadblock -- Bill Gates has a patent on
then
following a semicolon back from the Microsoft BASIC days, and no one could get in touch with him to see if he'd license it to us.In the end, we decided that postfix natural-language operators like
as
andsatisfies
have been popular, and adopted that design principle for AFD:This works in both type and object literals, and extends to some other unexpected places we'll see later.
Semantics
The semantics of AFD properties are pretty obvious:
If your properties in are the wrong order, you'll get a helpful error message:
You can also use AFD in union types, solving a longstanding problem around the supposed "unorderability" of sets:
When a union is ordered, code logic must conform to the union ordering:
Compatibility
AFD will ship alongside with the compatibility flags
--noAFD
,--noImplicitAFD
,--strictAFD
,--isolatedAFD
, and the temporary transition flag--noUncheckedExperimentalInThatOrder
which we'll try to unsuccessfully try to deprecate in TypeScript 8.0.Feedback
Please check this feature out when it becomes available, evaluate it, and send us feedback (in exactly that order)!
The text was updated successfully, but these errors were encountered: