Skip to content

Unsoundness with covariant type #8718

Open
@charlag

Description

@charlag

Flow version: 0.156.0 (at least since 0.135.0)

Expected behavior

Making type generic does not introduce unsoundness.

Actual behavior

Context

Our Mithril definitions use types which looks similar to these:

export interface Vnode<Attrs> {
	attrs: Attrs,
	children: ChildArray,
	dom: HTMLElement,
}

declare interface Mithril {
	<AttrsT>(
		component: Class<MComponent<AttrsT>>,
		attributes: AttrsT,
		children?: Children
	): Vnode<any>;
}

interface MComponent<+Attrs> {
	/** Creates a view out of virtual elements. */
	view(vnode: Vnode<Attrs>): Children;
}

export type Child = Vnode<any> | string | number | boolean | null;
export type ChildArray = Array<Children>;
export type Children = Child | ChildArray;

declare var m: Mithril;

// --
// --
// --

type TestAttrs = {
	test: number,
}

class TestComponent implements MComponent<TestAttrs> {
	view(vnode: Vnode<TestAttrs>): Children {
		return String(vnode.attrs.test)
	}
}

function test(): Children {
	return m(TestComponent, {
		test: "c" // this should not be allowed
	})
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions