Description
π Search Terms
recursive type, unused, compile error
π Version & Regression Information
- This changed between versions 3.3 (where this sample never compiles) and 3.5 (where it depends on the magic line). 5.2.2 works the same as 3.5.
β― Playground Link
π» Code
/**
* Wrapped in a function to allow referring to types before they are declared.
* This makes recursive and co-recursive types possible.
*/
type TypeReference = () => TreeSchema<TypeReference>;
/**
* Compile time assert that A is assignable to (extends) B.
* To use, simply define a type:
* `type _check = requireAssignableTo<T, Expected>;`
*/
type requireAssignableTo<_A extends B, B> = true;
class TreeSchema<T> {
public constructor(public readonly info: T) {}
}
// Captures both type and runtime information from the parameter.
// Removing the `extends TypeReference` makes this code compile, even if the magic line below is commented out.
function map<T extends TypeReference>(types: T): TreeSchema<T> {
return new TreeSchema(types);
}
const jsonRoot = () => jsonObject;
// Commenting out this line out or moving it to the bottom of the file causes this code to not compile.
// This is very odd: removing unused type definitions should not break compilation.
type _magic = requireAssignableTo<typeof jsonRoot, TypeReference>;
const jsonObject = map(jsonRoot);
π Actual behavior
This code fails to compile if the "_magic" line (which declares an unused type) is removed, or moved to the bottom of the file.
π Expected behavior
Ideally this code should compile, regardless of if the "_magic" line is present.
Additionally:
- the presence of an unused type definition should not impact type checking.
- the location of an type definition should not impact type checking.
Additional information about the issue
This was minified from https://github.com/microsoft/FluidFramework/blob/main/experimental/dds/tree2/src/domains/json/jsonDomainSchema.ts where I discovered that the type assertion I added caused the code to build without having to use the special recursive type workaround methods I added (which avoid using "extends" clauses which seem to break the recursive case). I'd love for cases like this to compile as it would make our API much nicer (the magic type assertion like to fix the build would be needed in our user's code and may require refactoring to add, so it's not a great workaround).