Skip to content

[RFC] Towards Non-Fungible Types #48513

Closed
@DanielRosenwasser

Description

@DanielRosenwasser

For years TypeScript has strived to grow and support its community. We've seen some tremendous investment, innovation, creativity, and beauty from TypeScript developers.

export type ConsistentWith<DecorationTargetProps, InjectedProps> = {
  [P in keyof DecorationTargetProps]: P extends keyof InjectedProps
    ? InjectedProps[P] extends DecorationTargetProps[P]
      ? DecorationTargetProps[P]
      : InjectedProps[P]
    : DecorationTargetProps[P];
};

export type PropInjector<InjectedProps, AdditionalProps = {}> = <
  C extends React.JSXElementConstructor<ConsistentWith<React.ComponentProps<C>, InjectedProps>>,
>(
  component: C,
) => React.JSXElementConstructor<
  DistributiveOmit<JSX.LibraryManagedAttributes<C, React.ComponentProps<C>>, keyof InjectedProps> &
    AdditionalProps
>;

But how will we ensure that creators of these innovative types are rewarded for their brilliance? How do we make sure people continue writing more beautiful types?

This question coincides with the advent of "WEB 3", a broader community shift which can be best explained by aggressively waving one's hands. Many fascinating ideas have come from the "WEB 3" world including NFTs - Non-Fungible Tokens, which are simply Tokens which caNnot be Funged.

This dovetails with another new concept which I would like to introduce today: Non-Fungible Types.

The Idea

Today we see the broader use of non-fungible tokens for breathtaking pictures of apes that look like they've just been laid off - the biggest explosion of art collection since pogs, Beanie Babies, Pokémon cards, and fidget spinners.

What a lot of people don't realize is that TypeScript code is art.

How do we figure?

Well, if you've ever heard the expression "ars longa, vita brevis" – Latin for "art is long, life is short" – then you almost immediately recalled a TypeScript type you saw in an error message. It really does seem obvious when you put it that way!

In fact, you have a wide variety, ranging from minimalist TypeScript art...

type a = {}

to surreal TypeScript art...

type UnionToIntersection<T> = 
  (T extends any ? (x: T) => any : never) extends 
  (x: infer R) => any ? R : never

So TypeScript types are great candidates for NFTs! Users should be able to take NFTs of type declarations and annotations and build a thriving ecosystem of type system artisans.

Do individual types actually have value?

I've discussed this proposal with my math friends from college experts in the field who have kinda offhandedly postulated over chat apps assured me that thanks to Cantor's Diagonal Argument, there are an uncountably infinite number of types. So can we really induce shortage to ensure value for every type?

Well growing up, I argued with my cantor all the time so I remain undeterred.

Thanks to the unfungibility of the types, the value comes from the convenience of the types! So imagine that you're writing a program that uses types like string, number, boolean, null, or undefined. As an upstanding citizen of the non-fungible-type community, you would need to get permission from the NFT owner(s) of these types. You could consult a chain of blocked accounts on Twitter to figure out who must own it, DM them, and ask them for permission to use these types, at which point they might charge a reasonable fee.

Imagine not just appropriately compensating this artist, but also the sort of community building that this will enable!

Won't this mean I can't write programs for free?

Like we mentioned, there's plenty of types to go around! Want to say you take a string without paying to use string?

Just use a structurally compatible type with an optional member of type never

type MyString = {
    toLowerCase(): MyString;
    toUpperCase(): MyString;
    slice(start?: number, end?: number): MyString;

    // ...

    myUselessMember134020105168421?: never;
}

See? It's still free! Freedom is a state of mind!

What about primitives like string, number, and boolean? Who owns those?

Currently I have the non-fungible-tokens for string, number, boolean, bigint, null, and undefined. I also have {} and the non-primitive type object. As an early pioneer in this ground-breaking space, it only feels fair to lay an early claim to these.

Someone else can use symbol. It's whatever.

Who owns never and unknown?

I am gladly donating never and unknown to @ahejlsberg so that I never have to explain them again.

Who owns any?

Technically someone on the core team does, but they seem to have lost access to their credentials and we don't want to tell you all who it was.

We recommend not using any until we can figure out how to access it again.

Timeline

I would like to see this happen soon. The fact that we have only created this proposal as recent as April 1st, 2022 means that we are already at risk of falling behind. Now is the best time to act!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Community ToolingThis might be better handled by tooling from the community instead of built into TypeScriptIn DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions