dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.31k stars 1.59k forks source link

Allow private typedefs in public APIs #59241

Open DanTup opened 1 year ago

DanTup commented 1 year ago

Sometimes I want to use a typedef as an alias within a file, because a type is large and unwieldy. I don't want to expose the typedef because it's really just there to make this file more readable. Externally I want the type to look like the aliased type.

typedef _Options
    = Either3<bool, CallHierarchyOptions, CallHierarchyRegistrationOptions>;

class Foo {
  _Options get options => _Options.t1(true);
}

This reports an error that I'm using a private type in a public API, but really I want the type alias to be invisible outside of this file - I want it to act like:

class Foo {
  Either3<bool, CallHierarchyOptions, CallHierarchyRegistrationOptions> get options => _Options.t1(true);
}

I just don't want to have to write that whole type in the API.

This might be at odds with dart-lang/sdk#58350 though. That describes exactly this situation, but I don't know if the typedef was used because it was an easy way to illustrate the problem, or if there's a reason why using the private typedef causes other issues (for example I'm not sure what dartdoc would do here.. you probably wouldn't want the private typedef showing up in places like documentation?).

bwilkerson commented 1 year ago

... I'm not sure what dartdoc would do here ...

I'm not either, but we should be consistent. I don't think it would be terrible for DartDoc to substitute the real type for a private typedef, but it might make the output harder to read.

@srawlins

srawlins commented 1 year ago

I'm also not sure, but I can add a test to dartdoc to see what it would do. Stay tuned.