Skip to content

[Bug]: Allow template parameters to be used in default values #5905

Closed
@j-waters

Description

@j-waters

Describe the bug

Possibly a duplicate of #959 but that issue is very old.

Extending the example in https://typespec.io/docs/language-basics/templates/#template-parameter-value-types, it seems like you should be able to define a default value with a template parameter value:

alias TakesValue<StringValue extends valueof string> = {
    @doc(StringValue)
    property: typeof StringValue = StringValue;
};

const str: "a" | "b" = "a";
alias M1 = TakesValue<str>;

However, this isn't allowed, and we get the error:

StringValue refers to a type, but is being used as a value here. TypeSpec(expect-value)

Reproduction

https://typespec.io/playground/?c=YWxpYXMgVGFrZXNWYWx1ZTxTdHJpbmfFDCBleHRlbmRzIHbEDm9mIHPFHD4gPSB7DQogICAgQGRvYyjLMinGF3Byb3BlcnR5OiB0eXDEOcxVPcwOOw0KfTsNCg0KY29uc3TEYTogImEiIHwgImIiID3EDDsNCuYApU0xID3sAKpzdHI%2BOw%3D%3D&e=%40typespec%2Fopenapi3&options=%7B%7D

Checklist

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions