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

Value world, object, tuple literals, const #3022

Merged
merged 210 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 169 commits
Commits
Show all changes
210 commits
Select commit Hold shift + click to select a range
49f51ca
Initial implementation of Object
timotheeguerin Mar 14, 2024
a517a06
Add support for tuple literals
timotheeguerin Mar 14, 2024
9a6d548
fix formatter
timotheeguerin Mar 14, 2024
97effa8
Simplify
timotheeguerin Mar 14, 2024
6a263cf
Fixes
timotheeguerin Mar 14, 2024
320797c
Add tests for basic checking
timotheeguerin Mar 15, 2024
6948455
Tm language and spec
timotheeguerin Mar 15, 2024
3560171
Add valueof assignment
timotheeguerin Mar 15, 2024
898beb6
Marshall types
timotheeguerin Mar 15, 2024
91b4d45
project
timotheeguerin Mar 15, 2024
12ed54d
Merge branch 'main' into feature/object-literals
timotheeguerin Mar 15, 2024
8376eee
Handle defaults
timotheeguerin Mar 15, 2024
e23685f
Fix
timotheeguerin Mar 15, 2024
80518d2
Create feature-object-literals-2024-2-15-18-36-3.md
timotheeguerin Mar 15, 2024
29b49f8
Create feature-object-literals-32024-2-15-18-36-3.md
timotheeguerin Mar 15, 2024
a5283b2
.
timotheeguerin Mar 15, 2024
ce94cd8
Add test to make sure value not allowed in certain usages
timotheeguerin Mar 15, 2024
8f32964
Fix
timotheeguerin Mar 15, 2024
a1cc33f
.
timotheeguerin Mar 15, 2024
759d09c
Fix openapi3 emitter
timotheeguerin Mar 15, 2024
9611adc
Create feature-object-literals-2024-2-15-21-56-46.md
timotheeguerin Mar 15, 2024
7cb98d5
Add quickfix
timotheeguerin Mar 15, 2024
89a7811
Fix filename
timotheeguerin Mar 15, 2024
377f8b6
Add codefix to convert model expression to object literal
timotheeguerin Mar 15, 2024
f387e1e
Fix
timotheeguerin Mar 15, 2024
7bec3ab
.
timotheeguerin Mar 16, 2024
1738240
Unify Values and allow enum member as values
timotheeguerin Mar 16, 2024
deced7a
.
timotheeguerin Mar 16, 2024
8129fe4
.
timotheeguerin Mar 16, 2024
fd87dc3
progress
timotheeguerin Mar 16, 2024
6a326cd
Merge branch 'main' into feature/object-literals
timotheeguerin Mar 18, 2024
c5004e3
fix
timotheeguerin Mar 18, 2024
d131045
Add new ParamConstraintUnion
timotheeguerin Mar 18, 2024
fd019aa
.
timotheeguerin Mar 18, 2024
df8dcaf
Fix
timotheeguerin Mar 18, 2024
b402ce7
Split
timotheeguerin Mar 18, 2024
34adfbe
Create feature-object-literals-2024-2-18-22-23-26.md
timotheeguerin Mar 18, 2024
14efb01
fix
timotheeguerin Mar 18, 2024
bc24b0e
Merge branch 'feature/object-literals' of https://github.com/timothee…
timotheeguerin Mar 18, 2024
b6979cf
Merge branch 'main' into feature/object-literals
timotheeguerin Mar 19, 2024
d253264
Test
timotheeguerin Mar 19, 2024
f7eb99a
Fix up issues
timotheeguerin Mar 19, 2024
b4f84d7
Get tuple
timotheeguerin Mar 19, 2024
07f461e
Additional properties
timotheeguerin Mar 19, 2024
1d9bd2b
.
timotheeguerin Mar 20, 2024
adf8efc
Merge branch 'main' of https://github.com/Microsoft/typespec into fea…
timotheeguerin Apr 4, 2024
856699c
Add support for const in parser
timotheeguerin Apr 4, 2024
262f670
Merge branch 'main' into feature/object-literals
timotheeguerin Apr 9, 2024
6dead0d
Introduce Value entities
timotheeguerin Apr 10, 2024
3da5cff
call expression
timotheeguerin Apr 10, 2024
0126ffa
add tests for call expression parsing
timotheeguerin Apr 10, 2024
134d3b6
Add scalar constructors parsing and formatter
timotheeguerin Apr 10, 2024
dfb2874
Add colorization for scalar consutructors
timotheeguerin Apr 10, 2024
10e69f9
update grammar
timotheeguerin Apr 10, 2024
4b29c93
Delete .chronus/changes/feature-object-literals-2024-2-15-21-56-46.md
timotheeguerin Apr 10, 2024
cf59227
Fix
timotheeguerin Apr 10, 2024
225fb07
Merge branch 'feature/object-literals' of https://github.com/timothee…
timotheeguerin Apr 10, 2024
63f46cb
no decorator on scalar members
timotheeguerin Apr 10, 2024
258f1f4
Merge branch 'main' into feature/object-literals
timotheeguerin Apr 10, 2024
26cda8a
named constructor checking
timotheeguerin Apr 11, 2024
e2a4787
Compare using Numeric
timotheeguerin Apr 11, 2024
08fb196
min
timotheeguerin Apr 11, 2024
56bbb6e
Progress with tests
timotheeguerin Apr 11, 2024
95c96af
Merge branch 'main' of https://github.com/Microsoft/typespec into fea…
timotheeguerin Apr 11, 2024
218e454
Tests
timotheeguerin Apr 11, 2024
0503b61
Fix up tests
timotheeguerin Apr 11, 2024
2bc3ee4
infer type from const value
timotheeguerin Apr 11, 2024
9f2898c
add test for ambiguous numeric
timotheeguerin Apr 11, 2024
a964e9d
cache constant values
timotheeguerin Apr 11, 2024
5701b7b
Add string and boolean values tests
timotheeguerin Apr 11, 2024
be288fd
Scalar values
timotheeguerin Apr 11, 2024
bb357b7
Better way to resolve values vs types
timotheeguerin Apr 11, 2024
de6f139
Progress
timotheeguerin Apr 11, 2024
0455316
Type
timotheeguerin Apr 11, 2024
7953580
more tests
timotheeguerin Apr 12, 2024
4662df3
Js marshaller conditional Numeric
timotheeguerin Apr 12, 2024
1117fba
More marshalling tests
timotheeguerin Apr 12, 2024
f68a677
nit fixes
timotheeguerin Apr 12, 2024
e785441
special projection marshal
timotheeguerin Apr 12, 2024
16d6ef5
Merge branch 'main' of https://github.com/Microsoft/typespec into fea…
timotheeguerin Apr 12, 2024
1032772
small test fix
timotheeguerin Apr 12, 2024
aef40ad
Convert numeric decorators
timotheeguerin Apr 12, 2024
bde6748
Show contextual errors for arguments
timotheeguerin Apr 12, 2024
6a30fd7
Fix template parameters
timotheeguerin Apr 12, 2024
a759d23
.
timotheeguerin Apr 12, 2024
a6aeb71
progress
timotheeguerin Apr 12, 2024
edd8ad0
progress
timotheeguerin Apr 12, 2024
5c0e117
Merge branch 'main' of https://github.com/Microsoft/typespec into fea…
timotheeguerin Apr 12, 2024
a57ff40
Improve diag messages and fix tests
timotheeguerin Apr 12, 2024
1a4f18d
Validate excess properties for value declarations
timotheeguerin Apr 12, 2024
deed566
cleanup
timotheeguerin Apr 12, 2024
603dd2c
cleanup and fix last tests
timotheeguerin Apr 12, 2024
9804e8d
Fix json schema
timotheeguerin Apr 12, 2024
a583297
more fixes
timotheeguerin Apr 12, 2024
68e87ca
Fix up build
timotheeguerin Apr 12, 2024
6feee8e
regen some docs
timotheeguerin Apr 12, 2024
ccdfeb6
some fixes
timotheeguerin Apr 12, 2024
b814cbd
Support union variants
timotheeguerin Apr 12, 2024
7c04ff6
fix protobuf tests
timotheeguerin Apr 12, 2024
3059031
http and rest test fix
timotheeguerin Apr 12, 2024
560330c
actually cast tuple as value
timotheeguerin Apr 12, 2024
f53a83a
Better conversion of legacy model and tuple to values
timotheeguerin Apr 13, 2024
82601cb
default can also cast tuple
timotheeguerin Apr 13, 2024
56deca2
format
timotheeguerin Apr 13, 2024
5892af7
Merge branch 'main' of https://github.com/Microsoft/typespec into fea…
timotheeguerin Apr 13, 2024
b6f4f61
merge with main
timotheeguerin Apr 13, 2024
bbf9022
remove unnecessary marshalling
timotheeguerin Apr 13, 2024
609481e
Merge branch 'main' into feature/object-literals
timotheeguerin Apr 15, 2024
64c371b
Better string template handling
timotheeguerin Apr 15, 2024
008ba34
.
timotheeguerin Apr 15, 2024
dfe0bf2
missing return
timotheeguerin Apr 16, 2024
1884559
string value test
timotheeguerin Apr 16, 2024
f1ae081
Opt-in new marshalling
timotheeguerin Apr 16, 2024
b5a80a1
fix null test
timotheeguerin Apr 16, 2024
3b52b6e
add defaultValue tests
timotheeguerin Apr 16, 2024
0d1d605
Add validation for min, max value and length
timotheeguerin Apr 16, 2024
327e4fb
rename and fix
timotheeguerin Apr 16, 2024
483f27c
Create feature-object-literals-2024-3-16-17-54-23.md
timotheeguerin Apr 16, 2024
55a1488
fix lint
timotheeguerin Apr 16, 2024
d24d622
Merge branch 'feature/object-literals' of https://github.com/timothee…
timotheeguerin Apr 16, 2024
da8e037
Add string template tests
timotheeguerin Apr 16, 2024
212533c
Add valueof casting tests
timotheeguerin Apr 16, 2024
894d08c
Changelog
timotheeguerin Apr 16, 2024
f5dadec
Merge branch 'main' into feature/object-literals
timotheeguerin Apr 16, 2024
265634e
Update .chronus/changes/feature-object-literals-32024-2-15-18-36-3.md
timotheeguerin Apr 16, 2024
184175a
Merge branch 'main' into feature/object-literals
timotheeguerin Apr 18, 2024
ac79e22
add notes
bterlson Apr 18, 2024
dd89960
Update docs/language-basics/values.md
bterlson Apr 18, 2024
c4f0c7f
Update docs/language-basics/values.md
bterlson Apr 18, 2024
c5a2564
Update docs/language-basics/values.md
bterlson Apr 18, 2024
b751e6e
Merge pull request #7 from bterlson/object-literals-docs
timotheeguerin Apr 18, 2024
fadfa7a
Merge branch 'main' of https://github.com/Microsoft/typespec into fea…
timotheeguerin Apr 18, 2024
1dd06c2
abc
timotheeguerin Apr 18, 2024
fd01267
Validate min/max items for tuples to array
timotheeguerin Apr 19, 2024
8a1f95c
fix
timotheeguerin Apr 19, 2024
8438d6c
remove outdated doc
timotheeguerin Apr 19, 2024
25b6b9e
Add parse support for typeof
timotheeguerin Apr 19, 2024
7afcec1
Limit typeof syntax and organize valueof
timotheeguerin Apr 20, 2024
53c8a74
Rename
timotheeguerin Apr 20, 2024
d4d778b
Structured mixed constraint
timotheeguerin Apr 20, 2024
02de1e7
Mixed function parameter
timotheeguerin Apr 20, 2024
e8e4024
fix tspd
timotheeguerin Apr 20, 2024
67f2eaf
fix
timotheeguerin Apr 20, 2024
fc2618c
abc
timotheeguerin Apr 20, 2024
bae371d
simplify
timotheeguerin Apr 20, 2024
1d2f418
Don't need valueconstraint anymore
timotheeguerin Apr 20, 2024
2678b38
missing
timotheeguerin Apr 20, 2024
efb29df
better way of dealing with templates
timotheeguerin Apr 20, 2024
1b5ea8e
string template that serialize to string is assignable to the string …
timotheeguerin Apr 20, 2024
6e5fb91
Move mixedconstraint to not be a type
timotheeguerin Apr 21, 2024
5b5f3bc
Fix doc generation
timotheeguerin Apr 21, 2024
6ad1cd4
Fix docs
timotheeguerin Apr 22, 2024
3fa5a92
format
timotheeguerin Apr 22, 2024
73e1c6c
Treat templates as syntax template (#8)
timotheeguerin Apr 25, 2024
7d8441d
Add validation of usage of template with valueof vs not
timotheeguerin Apr 25, 2024
096e2e2
Fix some assignment issues
timotheeguerin Apr 25, 2024
c84051d
Merge branch 'main' of https://github.com/Microsoft/typespec into fea…
timotheeguerin Apr 25, 2024
ad161a1
fix
timotheeguerin Apr 25, 2024
ee65807
Fix test
timotheeguerin Apr 25, 2024
6c1b42c
fix up tests
timotheeguerin Apr 25, 2024
5575e5f
Fix up deprecations
timotheeguerin Apr 25, 2024
86e9f6c
fix
timotheeguerin Apr 25, 2024
c038969
Fix
timotheeguerin Apr 25, 2024
4fc4900
Fix declare const in a namespace
timotheeguerin Apr 25, 2024
206fb0f
Push
timotheeguerin Apr 25, 2024
88c848d
Fix ide reporting diagnostics on hover
timotheeguerin Apr 25, 2024
80f5021
update spec
timotheeguerin Apr 26, 2024
2d729a2
Fix
timotheeguerin Apr 26, 2024
745d8b9
fix invalid
timotheeguerin Apr 26, 2024
533e4ba
Update docs/language-basics/values.md
timotheeguerin Apr 29, 2024
6c55bdf
Merge branch 'main' of https://github.com/Microsoft/typespec into fea…
timotheeguerin Apr 30, 2024
03ac2d4
Merge branch 'feature/object-literals' of https://github.com/timothee…
timotheeguerin Apr 30, 2024
56e88cb
Add const hovering
timotheeguerin May 2, 2024
68bdfef
add hover tests
timotheeguerin May 2, 2024
64b31c2
String literal assignable to string template
timotheeguerin May 2, 2024
eed204b
add validation for interpolating invalid template parmater
timotheeguerin May 2, 2024
da4ab8d
More value docs (#9)
bterlson May 4, 2024
e0d7bf5
More
timotheeguerin May 6, 2024
e8c4b7c
Fix bug
timotheeguerin May 6, 2024
520d681
Rename
timotheeguerin May 6, 2024
f4de434
Cleanup
timotheeguerin May 6, 2024
c300350
rename tuple literal to array literal
timotheeguerin May 6, 2024
4050eda
Merge branch 'main' into feature/object-literals
timotheeguerin May 6, 2024
f5582f3
Todo
timotheeguerin May 6, 2024
a110a5d
Apply suggestions from code review
timotheeguerin May 6, 2024
540dd96
Rename object literals -> object values where it make sense
timotheeguerin May 6, 2024
674fe27
Merge branch 'feature/object-literals' of https://github.com/timothee…
timotheeguerin May 6, 2024
4086daf
Migrate array literal to value nameing
timotheeguerin May 6, 2024
ef3b656
Update .chronus/changes/feature-object-literals-2024-3-16-17-54-23.md
timotheeguerin May 6, 2024
158e991
Update .chronus/changes/feature-object-literals-2024-3-16-10-38-3.md
timotheeguerin May 6, 2024
ed754c7
Update .chronus/changes/feature-object-literals-2024-3-16-10-38-3.md
timotheeguerin May 6, 2024
d1a0052
Update packages/compiler/lib/intrinsics.tsp
timotheeguerin May 6, 2024
66dc669
Update packages/compiler/lib/intrinsics.tsp
timotheeguerin May 6, 2024
741e03d
Some CR fixes
timotheeguerin May 6, 2024
fbab31e
Update packages/compiler/src/core/messages.ts
timotheeguerin May 6, 2024
4d53ef4
More CR comments
timotheeguerin May 6, 2024
1e01b42
Merge branch 'feature/object-literals' of https://github.com/timothee…
timotheeguerin May 6, 2024
9c1bca5
Merge branch 'main' of https://github.com/Microsoft/typespec into fea…
timotheeguerin May 6, 2024
94ce40a
ADd more scalar constructor and docs
timotheeguerin May 7, 2024
9a02980
Add tests
timotheeguerin May 7, 2024
5eca49f
Fix marshalling of enum members
timotheeguerin May 7, 2024
1b59484
Fix another issue with template default
timotheeguerin May 7, 2024
6030a9c
Update packages/compiler/src/core/checker.ts
timotheeguerin May 7, 2024
feecb80
Update packages/compiler/src/core/checker.ts
timotheeguerin May 7, 2024
83f613d
Add new `entityKind` property to
timotheeguerin May 8, 2024
3031a77
remove todo
timotheeguerin May 8, 2024
2d068c7
Better errors when trying to get a value of model/tuple
timotheeguerin May 8, 2024
98e9d05
Merge branch 'main' into feature/object-literals
timotheeguerin May 8, 2024
87bdb56
Fix tm grammar for scalar body
timotheeguerin May 8, 2024
2425dc6
Fix
timotheeguerin May 8, 2024
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
17 changes: 17 additions & 0 deletions .chronus/changes/feature-object-literals-2024-2-15-18-36-3.md
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/compiler"
---

New Language Feature: Object and Tuple Literals.
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved

```tsp
@dummy(#{
name: "John",
age: 48,
address: #{ city: "London" }
aliases: #["Bob", "Frank"]
})
```
10 changes: 10 additions & 0 deletions .chronus/changes/feature-object-literals-2024-2-18-22-23-26.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/json-schema"
- "@typespec/protobuf"
- "@typespec/versioning"
---

Update to support new value types
24 changes: 24 additions & 0 deletions .chronus/changes/feature-object-literals-2024-3-16-10-38-3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
changeKind: deprecation
packages:
- "@typespec/compiler"
---

Decorator API: Legacy marshalling logic

If a library had a decorator with `valueof` one of those types `numeric`, `int64`, `uint64`, `integer`, `float`, `decimal`, `decimal128`, `null` it used to marshall those as JS `number` and `NullType` for `null`. With the introduction of values we have a new marshalling logic which will marshall those numeric types as `Numeric` and the others will remain numbers. `null` will also get marshalled as `null`.
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved

For now this is an opt-in behavior with a warning on decorators not opt-in having a parameter with a constraint from the list above.
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved

Example:
```tsp
extern dec multipleOf(target: numeric | Reflection.ModelProperty, value: valueof numeric);
```
Will now emit a deprecated warning because `value` is of type `valueof string` which would marshall to `Numeric` under the new logic but as `number` previously.

To opt-in you can add the following to your library js/ts files.
```ts
export const $flags = defineModuleFlags({
decoratorArgMarshalling: "lossless",
});
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/openapi3"
---

Add support for new object and tuple literals as default values (e.g. `decimals: decimal[] = #[123, 456.7];`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/rest"
---

Update types to support new values in TypeSpec
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@typespec/html-program-viewer"
---

Add support for new fields added with the value world
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/http"
---

Update Flow Template to make use of the new tuple literals
125 changes: 125 additions & 0 deletions docs/language-basics/values.md
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
id: values
title: Values
---

# Values

TypeSpec can define values in addition to types. Values are useful in an API description to define default values for types or provide example values. They are also useful when passing data to decorators, and for template parameters that are ultimately passed to decorators.

There are three kinds of values: objects, arrays, and scalars. These values can be created with object literals, array literals, and scalar literals and initializers. Additionally, values can result from referencing enum members and union variants.

## Object values

Object values use the syntax `#{}` and can define any number of properties. For example:

```typespec
const point = #{ x: 0, y: 0 };
```

The object value's properties must refer to other values. It is an error to reference a type.

```typespec
const example = #{
prop1: #{ nested: true }; // ok
prop2: { nested: true }; // error: values can't reference a type
prop3: string; // error: values can't reference a type
}
```

## Array values

Array values use the syntax `#[]` and can define any number of items. For example:

```typespec
const points = #[#{ x: 0, y: 0 }, #{ x: 1, y: 1 }];
```

As with object values, array values cannot contain types.

If an array type defines a minimum and maximum size using the `@minValue` and `@maxValue` decorators, the compiler will validate that the array value has the appropriate number of items. For example:

```typespec
/** Can have at most 3 tags */
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved
@maxItems(2)
model Tags is Array<string>;

const exampleTags1: Tags = #["TypeSpec", "JSON"]; // ok
const exampleTags2: Tags = #["TypeSpec", "JSON", "OpenAPI"]; // error
```

## Scalar values

There are two ways to create scalar values: with a literal syntax like `"string value"`, and with a scalar initializer like `utcDateTime.fromISO("2020-12-01T12:00:00Z")`.

### Scalar literals

The literal syntax for strings, numerics, booleans and null can evaluate to either a type or a value depending on the surrounding context of the literal. When the literal is in _type context_ (a model property type, operation return type, alias definition, etc.) the literal becomes a literal type. When the literal is in _value context_ (a default value, property of an object value, const definition, etc.), the literal becomes a value. When the literal is in an _ambiguous context_ (e.g. an argument to a template or decorator that can accept either a type or a value) the literal becomes a value. The `typeof` operator can be used to convert the literal to a type in such ambiguous contexts.

```typespec
extern dec setNumberValue(target: unknown, color: valueof numeric);
extern dec setNumberType(target: unknown, color: numeric);
extern dec setNumberTypeOrValue(target: unknown, color: numeric | (valueof numeric));

@setNumberValue(123) // Passes the scalar value `numeric(123)`.
@setNumberType(123) // Passes the numeric literal type 123.
@setNumberTypeOrValue(123) // passes the scalar value `numeric(123)`.
model A {}
```

### Scalar initializers

Scalar initializers create scalar values by calling an initializer with one or more values. Scalar initializers for types extended from `numeric`, `string`, and `boolean` are called by adding parenthesis after the scalar reference:

```typespec
const n = int8(100);
const s = string("hello");
```

Any scalar can additionally be declared with named initializers that take one or more value parameters. For example, `utcDateTime` provides a `fromISO` initializer that takes an ISO string. Named scalars can be declared like so:

```typespec
scalar ipv4 extends string {
init fromInt(value: uint32);
}

const ip = ipv4.fromInt(2341230);
```

## Enum member &amp; union variant references

References to enum members and union variants can be either types or values and follow the same rules as scalar literals. When an enum member reference is in _type context_, the reference becomes an enum member type. When in _value context_ or _ambiguous context_ the reference becomes the enum member's value.

```typespec
extern dec setColorValue(target: unknown, color: valueof string);
extern dec setColorMember(target: unknown, color: Reflection.EnumMember);

enum Color {
red,
green,
blue,
}

@setColorValue(Color.red) // same as passing the literal "red"
@setColorMember(Color.red) // passes the enum member Color.red
model A {}
```

When a union variant reference is in _type context_, the reference becomes the type of the union variant. When in _value context_ or _ambiguous context_ the reference becomes the value of the union variant. It is an error to refer to a union variant whose type is not a literal type.

```typespec
extern dec setColorValue(target: unknown, color: valueof string);
extern dec setColorType(target: unknown, color: string);

union Color {
red: "red",
green: "green",
blue: "blue",
other: string,
}

@setColorValue(Color.red) // passes the scalar value `string("red")`
@setColorValue(Color.other) // error, trying to pass a type as a value.
@setColorType(Color.red) // passes the string literal type `"red"`
model A {}
```
2 changes: 1 addition & 1 deletion docs/libraries/http/reference/decorators.md
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ it will be used as a prefix to the route URI of the operation.
`@route` can only be applied to operations, namespaces, and interfaces.

```typespec
@TypeSpec.Http.route(path: valueof string, options?: (anonymous model))
@TypeSpec.Http.route(path: valueof string, options?: { shared: boolean })
```

#### Target
Expand Down
Loading
Loading