Skip to content
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

Consider making unknown a union of the object and all primitive types #39498

Closed
4 of 5 tasks
ExE-Boss opened this issue Jul 8, 2020 · 2 comments
Closed
4 of 5 tasks
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@ExE-Boss
Copy link
Contributor

ExE-Boss commented Jul 8, 2020

Search Terms

  • unknown type
  • unknown type union
  • make unknown type union

Suggestion

unknown would be a union of all lower‑case built‑in types, i.e.:

type unknown = string | number | bigint | boolean | symbol | object | null | undefined;

Use Cases

When describing an API that takes all‑but‑one primitive type, it’s useful to be able to do Exclude<unknown, symbol> or Exclude<unknown, number | bigint> without needing #29317.

Examples

From DefinitelyTyped/DefinitelyTyped#44805:
/**
 * @see https://tc39.es/ecma262/#sec-createhtml
 */
export function CreateHTML(
	string: Exclude<unknown, string>, // unknown & not string,
	tag: string,
	attribute: string,
	value?: Exclude<unknown, string>, // unknown & not string,
): string;

/**
 * @throws {TypeError} If `x` or `y` is a `number` or `bigint` or they're different types.
 * @see https://tc39.es/ecma262/#sec-samevaluenonnumeric
 */
export function SameValueNonNumeric(
	x: Exclude<unknown, number | bigint>, // unknown & not (number | bigint),
	y: Exclude<unknown, number | bigint>, // unknown & not (number | bigint),
): boolean;

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
    • There’s the possibility that some code somewhere depends on Exclude<unknown, something> returning unknown.
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Related

@MartinJohns
Copy link
Contributor

MartinJohns commented Jul 8, 2020

Wouldn't that completely go against the entire point of unknown? #24439

Anything is assignable to unknown, but unknown isn't assignable to anything but itself and any without a type assertion or a control flow based narrowing. Likewise, no operations are permitted on an unknown without first asserting or narrowing to a more specific type.

@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript labels Jul 8, 2020
@RyanCavanaugh
Copy link
Member

You're underestimating the breaking changeness of this, because changing unknown to a union means it will distribute in conditional types, which is very different behavior in many many cases. Changing boolean to be an alias of true | false had a lot of downstream problems that still confuse people; doing this for unknown would be even worse. It's not feasible to change this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants