diff --git a/src/__tests__/types.tsx b/src/__tests__/types.tsx new file mode 100644 index 000000000..b558226c8 --- /dev/null +++ b/src/__tests__/types.tsx @@ -0,0 +1,284 @@ +/** @jsx createElement */ +/* eslint @typescript-eslint/no-unused-vars: "off", jest/expect-expect: "off" */ +import { + createElement, + Component, + FunctionComponent, + Context, + GeneratorComponent, +} from ".."; + +describe("types", () => { + type MyProps = { + message: string; + }; + let elem: any; + + test("Not Components", () => { + // @ts-expect-error + const MyString: Component = "Hello"; + + // @ts-expect-error + const MyNumber: Component = 10; + + // @ts-expect-error + const MyBoolean: Component = true; + + // @ts-expect-error + const MyNull: Component = null; + + // @ts-expect-error + const MyUndefined: Component = undefined; + + // Not entirely sure why this one doesn't error anymore. seems that returns + // any, while createElement('a') returns Element. + // skip @ts-expect-error + // const MyElement: Component = ; + + // @ts-expect-error + const NotComponent: Component = {}; + }); + + test("Components", () => { + const MyFunctionComponent: Component = function (this, props) { + const ctx: Context = this; + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + return
; + }; + // @ts-expect-error + elem = ; + elem = ; + + const MyAsyncFunctionComponent: Component = async function ( + this, + props, + ) { + const ctx: Context = this; + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + return
; + }; + // @ts-expect-error + elem = ; + elem = ; + + const MyGeneratorComponent: Component = function* ( + this, + initialProps, + ) { + const ctx: Context = this; + let message: string = initialProps.message; + // @ts-expect-error + let unexpected = initialProps.unexpected; + + for (const newProps of this) { + let newMessage: string = initialProps.message; + // @ts-expect-error + let newUnexpected = newProps.unexpected; + yield
; + } + + return
; + }; + // @ts-expect-error + elem = ; + elem = ; + + const MyAsyncGeneratorComponent: Component = async function* ( + this, + initialProps, + ) { + const ctx: Context = this; + let message: string = initialProps.message; + // @ts-expect-error + let unexpected = initialProps.unexpected; + + for await (const newProps of this) { + let newMessage: string = initialProps.message; + // @ts-expect-error + let newUnexpected = newProps.unexpected; + yield
; + } + + return
; + }; + // @ts-expect-error + elem = ; + elem = ; + }); + + test("Function Components", () => { + const MyFunctionComponent: FunctionComponent = function ( + this, + props, + ) { + const ctx: Context = this; + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + return
; + }; + // @ts-expect-error + elem = ; + elem = ; + + const MyAsyncFunctionComponent: FunctionComponent = async function ( + this, + props, + ) { + const ctx: Context = this; + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + return
; + }; + // @ts-expect-error + elem = ; + elem = ; + + // @ts-expect-error + const MyGeneratorComponent: FunctionComponent = function* ( + this, + props, + ) { + yield
; + }; + // @ts-expect-error + const MyAsyncGeneratorComponent: FunctionComponent = async function* ( + this, + props, + ) { + yield
; + }; + }); + + test("Generator Components", () => { + const MyGeneratorComponent: GeneratorComponent = function* ( + this, + initialProps, + ) { + const ctx: Context = this; + let message: string = initialProps.message; + // @ts-expect-error + let unexpected = initialProps.unexpected; + + for (const newProps of this) { + let newMessage: string = initialProps.message; + // @ts-expect-error + let newUnexpected = newProps.unexpected; + yield
; + } + + return
; + }; + // @ts-expect-error + elem = ; + elem = ; + + const MyAsyncGeneratorComponent: GeneratorComponent = async function* ( + this, + initialProps, + ) { + const ctx: Context = this; + let message: string = initialProps.message; + // @ts-expect-error + let unexpected = initialProps.unexpected; + + for await (const newProps of this) { + let newMessage: string = initialProps.message; + // @ts-expect-error + let newUnexpected = newProps.unexpected; + yield
; + } + + return
; + }; + // @ts-expect-error + elem = ; + elem = ; + + // TODO: add ts-expect-error at some point in the future, I guess? + // This will not pass because the function is infered as any, and any matches Iterator. + // Hopefully a later typescript version fixes this :( + const MyFunctionComponent: GeneratorComponent = function ( + this, + props, + ) { + return
; + }; + // @ts-expect-error + const MyAsyncFunctionComponent: GeneratorComponent = async function ( + this, + props, + ) { + return
; + }; + }); + + test("Loose Typings", () => { + function MyFunctionComponent(props: MyProps) { + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + + async function MyAsyncFunctionComponent(props: MyProps) { + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + + function* MyGeneratorComponent(this: Context, props: MyProps) { + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + for (props of this) { + let newMessage: string = props.message; + // @ts-expect-error + let newUnexpected = props.unexpected; + yield
; + } + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + + async function* MyAsyncGeneratorComponent(this: Context, props: MyProps) { + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + for await (props of this) { + let newMessage: string = props.message; + // @ts-expect-error + let newUnexpected = props.unexpected; + yield
; + } + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + }); +});