You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"comment": "Added new keyword `valueof` designed to request for a value type in a decorator parameter.",
6
+
"type": "none"
7
+
},
8
+
{
9
+
"packageName": "@typespec/compiler",
10
+
"comment": "**BREAKING** Decorator API will not be marshalling values unless the parameter type is using `valueof`. `extern dec foo(target, value: string)` should be changed to `extern dec foo(target, value: valueof string)`.",
11
+
"type": "none"
12
+
},
13
+
{
14
+
"packageName": "@typespec/compiler",
15
+
"comment": "**DEPRECATION** To make transition to valueof smoother if using a template parameter inside a decorator that is now using valueof the existing parmater constraint will still be compatible but emit a warning.",
Copy file name to clipboardexpand all lines: docs/extending-typespec/create-decorators.md
+31-9
Original file line number
Diff line number
Diff line change
@@ -40,15 +40,37 @@ extern dec track(target: Model | Enum);
40
40
A decorator parameter can be marked optional using `?`
41
41
42
42
```typespec
43
-
extern dec track(target: Model | Enum, name?: StringLiteral);
43
+
extern dec track(target: Model | Enum, name?: valueof string);
44
44
```
45
45
46
46
### Rest parameters
47
47
48
48
A decorator's last parameter can be prefixed with `...` to collect all the remaining arguments. The type of that parameter must be an `array expression`
49
49
50
50
```typespec
51
-
extern dec track(target: Model | Enum, ...names: StringLiteral[]);
51
+
extern dec track(target: Model | Enum, ...names: valueof string[]);
52
+
```
53
+
54
+
## Ask for a value type
55
+
56
+
It is common that decorators parameter will expect a value(e.g. a string or a number). However just using `: string` as the type will also allow a user of the decorator to pass `string` itself or a custom scalar extending string as well as union of strings.
57
+
Instead the decorator can use `valueof <T>` to specify that it is expecting a value of that kind.
58
+
59
+
| Example | Description |
60
+
| ----------------- | ---------------- |
61
+
|`valueof string`| Expect a string |
62
+
|`valueof float64`| Expect a float |
63
+
|`valueof int32`| Expect a number |
64
+
|`valueof boolean`| Expect a boolean |
65
+
66
+
```tsp
67
+
extern dec tag(target: unknown, value: valueof string);
68
+
69
+
// bad
70
+
@tag(string)
71
+
72
+
// good
73
+
@tag("This is the tag name")
52
74
```
53
75
54
76
## Implement the decorator in JS
@@ -63,7 +85,7 @@ Decorators can be implemented in JavaScript by prefixing the function name with
63
85
// model.ts
64
86
importtype { DecoratorContext, Type } from"@typespec/compiler";
For certain TypeSpec types(Literal types) the decorator do not receive the actual type but a marshalled value. This is to simplify the most common cases.
117
+
For certain TypeSpec types(Literal types) the decorator do not receive the actual type but a marshalled value if the decorator parmaeter type is a `valueof`. This is to simplify the most common cases.
0 commit comments