microsoft / microsoft/typespec

[compiler] MixedConstraint parsing does not allow some array expressions

Open
#9,689 0 comments 0 reactions 0 assignees View on GitHub
compiler:core feature triaged:core
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

### Clear and concise description of the problem

Here are some things you cannot write in mixed constraint position that you might want to do for one reason or another:

```tsp
extern dec d(target: unknown, arg: ("a" | "b")[]);
// ~~ expected-token: ',' expected

// also doesn't work with a rest param
extern dec d(target: unknown, ...args: ("a" | "b")[]);
// ~~ expected-token: ',' expected

// also doesn't work in other mixedconstraint contexts like `extends` clauses
alias A = ...;
// ~~ expected-token: ',' expected

// This also doesn't work, because the checker asserts that the constraint is syntactically an array _expression_
// before evaluating the mixed constraint in rest-constraint position. But this does work in other mixed constraint
// contexts.
extern dec d(target: unknown, ...args: Array<"a" | "b">);
// ~~~~~~~~~~~~~~~~ rest-parameter-array: A rest parameter must be of an array type.
```

This prevents writing functions/decorators that accept variadic type arguments where the valid types are those of an anonymous union.

**There is a workaround**. Declare an alias or a named union, then it can serve as the array item constraint:

```tsp
union U {
"a",
"b",
}

// or

alias U = "a" | "b";

extern dec d(target: unknown, ...rest: U[]);
```

### Checklist

- [x] Follow our [Code of Conduct](https://github.com/microsoft/typespec/blob/main/CODE_OF_CONDUCT.md)
- [x] Read the [docs](https://typespec.io/docs/).
- [x] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.