-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdk): add
TuiLooseUnion
(#9068)
- Loading branch information
Showing
9 changed files
with
69 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* @example | ||
* type Color = 'primary' | 'secondary' | string; | ||
* | ||
* Your brand has some known colors, like `primary` and `secondary`. | ||
* But you also want to make sure that users can specify any color they want. | ||
* | ||
* @Input() color: Color = ''; | ||
* | ||
* <my-icon color="red" /> | ||
* | ||
* But there's an issue. We aren't getting `color` | ||
* suggestions when we use the `MyIcon` component. | ||
* If we try to autocomplete the `color` prop, we get no suggestions. | ||
* Ideally, we want `primary` and `secondary` to be part of that list. | ||
* How do we manage that? | ||
* | ||
* This works because of a quirk of the TypeScript compiler. | ||
* When you create a union between a string literal type and `string`, | ||
* TypeScript will eagerly widen the type to `string`. | ||
* | ||
* But if we change type to | ||
* type Color = 'primary' | 'secondary' | (string & {}); | ||
* | ||
* TypeScript has already forgotten that `"primary"` and `"secondary"` | ||
* were ever part of the type. But by intersecting `string` with | ||
* an empty object, we trick TypeScript into retaining the string | ||
* literal types for a bit longer. | ||
* | ||
* This might feel pretty fragile to you. This doesn't seem like | ||
* intended behavior from TypeScript. Well, the team actually | ||
* know about this trick. They test against it. And someday, they may make | ||
* it so that a plain `string` type will just work. | ||
* But until then, keep this in mind. | ||
*/ | ||
export type TuiLooseUnion<U> = | ||
| U | ||
| (U extends string | ||
? Record<never, never> & string | ||
: U extends number | ||
? Record<never, never> & number | ||
: U extends symbol | ||
? Record<never, never> & symbol | ||
: U extends bigint | ||
? Record<never, never> & bigint | ||
: never); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
projects/kit/components/badged-content/badged-content.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import {Directive, Input} from '@angular/core'; | ||
import type {TuiLooseUnion} from '@taiga-ui/cdk/types'; | ||
|
||
@Directive({ | ||
standalone: true, | ||
selector: '[tuiSlot]', | ||
}) | ||
export class TuiBadgedContentDirective { | ||
@Input() | ||
public tuiSlot: 'bottom' | 'top' | (Record<never, never> & string) = 'top'; | ||
public tuiSlot: TuiLooseUnion<'bottom' | 'top'> = 'top'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import {Directive, Input} from '@angular/core'; | ||
import type {TuiLooseUnion} from '@taiga-ui/cdk/types'; | ||
|
||
@Directive({ | ||
standalone: true, | ||
selector: '[tuiSlot]', | ||
}) | ||
export class TuiAppBarDirective { | ||
@Input() | ||
public tuiSlot: 'left' | 'right' | (Record<never, never> & string) = 'left'; | ||
public tuiSlot: TuiLooseUnion<'left' | 'right'> = 'left'; | ||
} |
3 changes: 2 additions & 1 deletion
3
projects/layout/components/block-status/block-status.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import {Directive, Input} from '@angular/core'; | ||
import type {TuiLooseUnion} from '@taiga-ui/cdk/types'; | ||
|
||
@Directive({ | ||
standalone: true, | ||
selector: '[tuiSlot]', | ||
}) | ||
export class TuiBlockStatusDirective { | ||
@Input() | ||
public tuiSlot: 'action' | 'top' | (Record<never, never> & string) = 'top'; | ||
public tuiSlot: TuiLooseUnion<'action' | 'top'> = 'top'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters