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

Additive inverse of number literals #30914

Open
5 tasks done
cshaa opened this issue Apr 13, 2019 · 1 comment
Open
5 tasks done

Additive inverse of number literals #30914

cshaa opened this issue Apr 13, 2019 · 1 comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@cshaa
Copy link

cshaa commented Apr 13, 2019

Search Terms

Using unary minus on variable that is of number-literal type. Negative number literals.

Suggestion

This is a specific case of #26382. My proposal is simply that if we apply unary minus on an expression whose type is a union of number literal types, the result should be a union of their additive inverses:

declare var a: 1 | 2 | 3;
const b = -a; // should be -1 | -2 | -3

Furthermore we could add a unary minus type operator:

-(42) === -42
-(A | B) === -A | -B
-number === number
-(not number) // error

Both should be very easy and uncontroversial to implement.

Use Cases

function compare(a: C, b: C): -1 | 0 | 1;

function reverseCompare(a: C, b: C): -1 | 0 | 1
{
  return -compare(a, b); // shouldn't throw an error
}

The type operator would be just for sake of completeness

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • 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.
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Apr 15, 2019
@keitzer
Copy link

keitzer commented Oct 18, 2024

Our example code:

export const SpacingUnit = {
	/** xxs - 4 unit spacing */
	xxs_4: 4,
	/** xs - 8 unit spacing */
	xs_8: 8,
	/** sm - 12 unit spacing */
	sm_12: 12,
	// ... others
} as const;

export type BoxSpacingUnit =
	/* others */
	| -12
	| -8
	| -4
	| 0
	| 4
	| 8
	| 12
	/* others */
	| `-${keyof typeof SpacingUnit}`
	| keyof typeof SpacingUnit;

interface BoxStyleProps {
	// Margin
	marginTop?: BoxSpacingUnit;
	marginBottom?: BoxSpacingUnit;
	marginLeft?: BoxSpacingUnit;
	marginRight?: BoxSpacingUnit;
	marginHorizontal?: BoxSpacingUnit;
	marginVertical?: BoxSpacingUnit;
	margin?: BoxSpacingUnit;

	// ... others...
}

Image

Adding support for this will be helpful for Design Systems that constrain possible styling values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants