diff --git a/src/Internal/Arg.elm b/src/Internal/Arg.elm index 8b2f574..eb8b901 100644 --- a/src/Internal/Arg.elm +++ b/src/Internal/Arg.elm @@ -182,8 +182,20 @@ aliasAs aliasName (Arg toArgDetails) = innerArgDetails = toArgDetails index - ( aliasVal, _, _ ) = - val innerArgDetails.index aliasName + -- The alias refers to the same value as the underlying + -- pattern, so it should have the same type. Use the + -- inner pattern's annotation (the record type, tuple + -- type, etc.) rather than creating a fresh type variable. + aliasVal : Compiler.Expression + aliasVal = + Compiler.Expression <| + \_ -> + { expression = + Exp.FunctionOrValue [] + (Format.sanitize aliasName) + , annotation = innerArgDetails.details.annotation + , imports = [] + } in { details = { imports = innerArgDetails.details.imports diff --git a/tests/TypeChecking.elm b/tests/TypeChecking.elm index efca18c..8106bca 100644 --- a/tests/TypeChecking.elm +++ b/tests/TypeChecking.elm @@ -272,6 +272,24 @@ generatedCode = ( 1 + 2, x ) """ ] + , describe "aliasAs pattern type" + [ test "aliasAs on record pattern uses the underlying record type" <| + \_ -> + Elm.Declare.fn "describe" + (Arg.record (\a b -> ( a, b )) + |> Arg.field "name" + |> Arg.field "age" + |> Arg.aliasAs "person" + ) + (\( ( name, _ ), person ) -> + Elm.tuple name person + ) + |> .declaration + |> Elm.Expect.declarationAs + """ + describe : { name : name, age : age } -> ( name, { name : name, age : age } ) + describe ({ name, age } as person) = + ( name, person ) , test "Elm.Let.fn declaration has a type annotation" <| \_ -> Elm.declaration "useLetFn"