-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generated types allow assigning wrong message type if it is a superset of the target type #551
Comments
Merged
We've looked into this, also in the context of connectrpc/connect-es#694. Unfortunately, the mitigations we investigated came with major drawbacks. We are fixing this with the upcoming v2. Here's how v2 behaves with the example above: import { create } from "@bufbuild/protobuf";
import {
FooSchema,
FooAndBarSchema,
NotFooSchema,
type Foo,
type NotFoo,
type FooAndBar
} from "./gen/example_pb.js";
const a0: Foo = create(FooAndBarSchema); // TS2322: Type FooAndBar is not assignable to type Foo
const a1: Foo = create(NotFooSchema); // TS2322: Type NotFoo is not assignable to type Foo
const a2: NotFoo = create(FooSchema); // TS2322: Type Foo is not assignable to type NotFoo
const a3: NotFoo = create(FooAndBarSchema); // TS2322: Type FooAndBar is not assignable to type NotFoo
const a4: FooAndBar = create(FooSchema); // TS2322: Type Foo is not assignable to type FooAndBar
const a5: FooAndBar = create(NotFooSchema); // TS2322: Type NotFoo is not assignable to type FooAndBar
function example(foo: Foo) { }
example(create(NotFooSchema)); // TS2345: Argument of type NotFoo is not assignable to parameter of type Foo The
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is no compile error in
const a: ProtoA = new ProtoB()
if the fields inProtoA
are a subset of the fields inProtoB
.example.proto
example.ts
An example fix would be adding a dummy private variable to to every generated proto. TypeScript doesn't output any code when compiling this, so there should be no runtime implications (Playground Link).
Relevant TypeScript docs:
The text was updated successfully, but these errors were encountered: