Skip to content

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

Closed
@ExE-Boss

Description

@ExE-Boss

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions