Skip to content

Ability to define the type of a custom variable inside @function #14945

Backlog

Description

@bigandy

I have a custom @function which uses a random() value to generate a different background:

Problem - custom variable defined inside @function is untyped and does not match the style query

@function --random-number() {
	result: random(1,7,1);
}

@function --random-item-bg() {
	--random-number: --random-number();
	/*
	returns an untyped number and is equivalent to 
	--random-number: random(1,7,1);
	*/
	
	result: if(
		style(--random-number: 1): var(--color1);
		style(--random-number: 2): var(--color2);
		style(--random-number: 3): var(--color3);
		style(--random-number: 4): var(--color4);
		style(--random-number: 5): var(--color5);
		style(--random-number: 6): var(--color6);
		style(--random-number: 7): var(--color7);
		else: black;
	);
}

.box {
	background-color: --random-item-bg();
}

This gives all black divs because the syntax of the --random-number is not <number> and the style query does not match.

Capture d’écran 2026-12-04 à 16 54 45

A way around - typed variable by using another @function with a returns <number>

If I use --random-number: --random-number-typed(); then it works as expected because --random-number gets a typed value back and so is of the type of <number> and the style query works as expected.

@function --random-number-typed() returns <number> {
	result: random(1,7,1);
}

@function --random-item-bg() {
	--random-number: --random-number-typed();
	/* --random-number is now typed and is of type <number> */
	
	result: if(
		style(--random-number: 1): var(--color1);
		style(--random-number: 2): var(--color2);
		style(--random-number: 3): var(--color3);
		style(--random-number: 4): var(--color4);
		style(--random-number: 5): var(--color5);
		style(--random-number: 6): var(--color6);
		style(--random-number: 7): var(--color7);
		else: black;
	);
}

.box {
	background-color: --random-item-bg();
}
Capture d’écran 2026-12-04 à 16 54 49

Expected

As a developer I would expect that --random-number has the correct type, either by setting the type when the variable was defined, or by inference from the random() function.

Notes

See the demo on Codepen

The same thing occurred when I tested it in a @mixin.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions