Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions types/react/experimental.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,26 @@ declare module '.' {
* @see https://reactjs.org/docs/concurrent-mode-reference.html#usetransition
*/
export function unstable_useTransition(config?: SuspenseConfig | null): [TransitionStartFunction, boolean];

/**
* @private
*/
const opaqueIdentifierBranding: unique symbol;
/**
* WARNING: Don't use this as a `string`.
*
* This is an opaque type that is not supposed to type-check structurally.
* It is only valid if returned from React methods and passed to React e.g. `<button aria-labelledby={opaqueIdentifier} />`
*/
// We can't create a type that would be rejected for string concatenation or `.toString()` calls.
// So in order to not have to add `string | OpaqueIdentifier` to every react-dom host prop we intersect it with `string`.
type OpaqueIdentifier = string & {
readonly [opaqueIdentifierBranding]: unknown;
// While this would cause `const stringified: string = opaqueIdentifier.toString()` to not type-check it also adds completions while typing.
// It would also still allow string concatenation.
// Unsure which is better. Not type-checking or not suggesting.
// toString(): void;
};

export function unstable_useOpaqueIdentifier(): OpaqueIdentifier;
}
26 changes: 25 additions & 1 deletion types/react/test/experimental.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import React = require('react');
function useExperimentalHooks() {
const [toggle, setToggle] = React.useState(false);

const [startTransition, done] = React.unstable_useTransition({ busyMinDurationMs: 100, busyDelayMs: 200, timeoutMs: 300 });
const [startTransition, done] = React.unstable_useTransition({
busyMinDurationMs: 100,
busyDelayMs: 200,
timeoutMs: 300,
});
// $ExpectType boolean
done;

Expand Down Expand Up @@ -45,3 +49,23 @@ function useExperimentalHooks() {
return '';
}
}

function Dialog() {
const nameId = React.unstable_useOpaqueIdentifier();

return (
<div role="dialog" aria-labelledby={nameId}>
<h2 id={nameId}></h2>
</div>
);
}

function InvalidOpaqueIdentifierUsage() {
const id = React.unstable_useOpaqueIdentifier();
// undesired, would warn in React should not type-check
const stringified1: string = id.toString();
// undesired, would warn in React should not type-check
const stringified2: string = id + '';

return null;
}