Syntax error when using record type in sharing constraint

Consider the following example:

module type A = sig type t end
module B : A with type t = { key : int } = struct type t = { key : int } end

This code will not compile but produce a syntax error at the opening curly brace of the sharing constraint.
A workaround is to extract the record type into a separate new type and using that instead. However I do wonder why the example above is rejected by the compiler and would be glad if anybody could explain.

This is because curly braces are only valid when declaring a record type type t = { } and not in the “type” language, you cannot write let x : { key : int } = ...

You could try writing the following instead:

module type A = sig
  type t
end

type t_record = { key : int }

module B : A with type t = t_record = struct
  type t = t_record = { key : int }
end

I’m still learning the subtleties of the module system. So can you tell us a little bit more about your use case and the interest of writing a concrete type in the signature of module B that is the same as the type of its structure? (while, in general, we should have an abstract type for a module signature and choose some relevant concrete type for the structure).