Skip to content

[Release] Allow spread generic type #15687

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

Merged
merged 4 commits into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13234,11 +13234,7 @@ namespace ts {
attributesArray = [];
attributesTable = createMap<Symbol>();
}
const exprType = checkExpression(attributeDecl.expression);
if (!isValidSpreadType(exprType)) {
error(attributeDecl, Diagnostics.Spread_types_may_only_be_created_from_object_types);
hasSpreadAnyType = true;
}
const exprType = getApparentType(checkExpression(attributeDecl.expression));
if (isTypeAny(exprType)) {
hasSpreadAnyType = true;
}
Expand Down
19 changes: 11 additions & 8 deletions tests/baselines/reference/tsxAttributeResolution5.errors.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
tests/cases/conformance/jsx/file.tsx(17,16): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/jsx/file.tsx(21,16): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/jsx/file.tsx(25,16): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/jsx/file.tsx(21,16): error TS2322: Type '{ x: number; }' is not assignable to type 'Attribs1'.
Types of property 'x' are incompatible.
Type 'number' is not assignable to type 'string'.
tests/cases/conformance/jsx/file.tsx(25,16): error TS2322: Type '{ y: string; }' is not assignable to type 'Attribs1'.
Property 'x' is missing in type '{ y: string; }'.
tests/cases/conformance/jsx/file.tsx(29,8): error TS2322: Type '{}' is not assignable to type 'Attribs1'.
Property 'x' is missing in type '{}'.


==== tests/cases/conformance/jsx/file.tsx (4 errors) ====
==== tests/cases/conformance/jsx/file.tsx (3 errors) ====
declare module JSX {
interface Element { }
interface IntrinsicElements {
Expand All @@ -23,20 +25,21 @@ tests/cases/conformance/jsx/file.tsx(29,8): error TS2322: Type '{}' is not assig

function make1<T extends {x: string}> (obj: T) {
return <test1 {...obj} />; // OK
~~~~~~~~
!!! error TS2698: Spread types may only be created from object types.
}

function make2<T extends {x: number}> (obj: T) {
return <test1 {...obj} />; // Error (x is number, not string)
~~~~~~~~
!!! error TS2698: Spread types may only be created from object types.
!!! error TS2322: Type '{ x: number; }' is not assignable to type 'Attribs1'.
!!! error TS2322: Types of property 'x' are incompatible.
!!! error TS2322: Type 'number' is not assignable to type 'string'.
}

function make3<T extends {y: string}> (obj: T) {
return <test1 {...obj} />; // Error, missing x
~~~~~~~~
!!! error TS2698: Spread types may only be created from object types.
!!! error TS2322: Type '{ y: string; }' is not assignable to type 'Attribs1'.
!!! error TS2322: Property 'x' is missing in type '{ y: string; }'.
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
tests/cases/conformance/jsx/file.tsx(8,34): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/jsx/file.tsx(13,34): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/jsx/file.tsx(8,34): error TS2322: Type '{ ignore-prop: 10; prop: number; }' is not assignable to type 'IntrinsicAttributes & { prop: number; "ignore-prop": string; }'.
Type '{ ignore-prop: 10; prop: number; }' is not assignable to type '{ prop: number; "ignore-prop": string; }'.
Types of property '"ignore-prop"' are incompatible.
Type '10' is not assignable to type 'string'.
tests/cases/conformance/jsx/file.tsx(13,34): error TS2322: Type '{}' is not assignable to type 'IntrinsicAttributes & { prop: {}; "ignore-prop": string; }'.
Type '{}' is not assignable to type '{ prop: {}; "ignore-prop": string; }'.
Property 'prop' is missing in type '{}'.
tests/cases/conformance/jsx/file.tsx(20,19): error TS2322: Type '{ func: (a: number, b: string) => void; }' is not assignable to type 'IntrinsicAttributes & { func: (arg: number) => void; }'.
Type '{ func: (a: number, b: string) => void; }' is not assignable to type '{ func: (arg: number) => void; }'.
Types of property 'func' are incompatible.
Expand All @@ -19,15 +24,20 @@ tests/cases/conformance/jsx/file.tsx(31,10): error TS2453: The type argument for
// Error
function Bar<T extends {prop: number}>(arg: T) {
let a1 = <ComponentSpecific1 {...arg} ignore-prop={10} />;
~~~~~~~~
!!! error TS2698: Spread types may only be created from object types.
~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2322: Type '{ ignore-prop: 10; prop: number; }' is not assignable to type 'IntrinsicAttributes & { prop: number; "ignore-prop": string; }'.
!!! error TS2322: Type '{ ignore-prop: 10; prop: number; }' is not assignable to type '{ prop: number; "ignore-prop": string; }'.
!!! error TS2322: Types of property '"ignore-prop"' are incompatible.
!!! error TS2322: Type '10' is not assignable to type 'string'.
}

// Error
function Baz<T>(arg: T) {
let a0 = <ComponentSpecific1 {...arg} />
~~~~~~~~
!!! error TS2698: Spread types may only be created from object types.
!!! error TS2322: Type '{}' is not assignable to type 'IntrinsicAttributes & { prop: {}; "ignore-prop": string; }'.
!!! error TS2322: Type '{}' is not assignable to type '{ prop: {}; "ignore-prop": string; }'.
!!! error TS2322: Property 'prop' is missing in type '{}'.
}

declare function Link<U>(l: {func: (arg: U)=>void}): JSX.Element;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,128 +1,127 @@
=== tests/cases/conformance/jsx/file.tsx ===

import React = require('react')
>React : Symbol(React, Decl(file.tsx, 0, 0))

declare function OverloadComponent<U>(): JSX.Element;
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 1, 31), Decl(file.tsx, 3, 53), Decl(file.tsx, 4, 101))
>U : Symbol(U, Decl(file.tsx, 3, 35))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 0, 31), Decl(file.tsx, 2, 53), Decl(file.tsx, 3, 101))
>U : Symbol(U, Decl(file.tsx, 2, 35))
>JSX : Symbol(JSX, Decl(react.d.ts, 2352, 1))
>Element : Symbol(JSX.Element, Decl(react.d.ts, 2355, 27))

declare function OverloadComponent<U>(attr: {b: U, a?: string, "ignore-prop": boolean}): JSX.Element;
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 1, 31), Decl(file.tsx, 3, 53), Decl(file.tsx, 4, 101))
>U : Symbol(U, Decl(file.tsx, 4, 35))
>attr : Symbol(attr, Decl(file.tsx, 4, 38))
>b : Symbol(b, Decl(file.tsx, 4, 45))
>U : Symbol(U, Decl(file.tsx, 4, 35))
>a : Symbol(a, Decl(file.tsx, 4, 50))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 0, 31), Decl(file.tsx, 2, 53), Decl(file.tsx, 3, 101))
>U : Symbol(U, Decl(file.tsx, 3, 35))
>attr : Symbol(attr, Decl(file.tsx, 3, 38))
>b : Symbol(b, Decl(file.tsx, 3, 45))
>U : Symbol(U, Decl(file.tsx, 3, 35))
>a : Symbol(a, Decl(file.tsx, 3, 50))
>JSX : Symbol(JSX, Decl(react.d.ts, 2352, 1))
>Element : Symbol(JSX.Element, Decl(react.d.ts, 2355, 27))

declare function OverloadComponent<T, U>(attr: {b: U, a: T}): JSX.Element;
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 1, 31), Decl(file.tsx, 3, 53), Decl(file.tsx, 4, 101))
>T : Symbol(T, Decl(file.tsx, 5, 35))
>U : Symbol(U, Decl(file.tsx, 5, 37))
>attr : Symbol(attr, Decl(file.tsx, 5, 41))
>b : Symbol(b, Decl(file.tsx, 5, 48))
>U : Symbol(U, Decl(file.tsx, 5, 37))
>a : Symbol(a, Decl(file.tsx, 5, 53))
>T : Symbol(T, Decl(file.tsx, 5, 35))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 0, 31), Decl(file.tsx, 2, 53), Decl(file.tsx, 3, 101))
>T : Symbol(T, Decl(file.tsx, 4, 35))
>U : Symbol(U, Decl(file.tsx, 4, 37))
>attr : Symbol(attr, Decl(file.tsx, 4, 41))
>b : Symbol(b, Decl(file.tsx, 4, 48))
>U : Symbol(U, Decl(file.tsx, 4, 37))
>a : Symbol(a, Decl(file.tsx, 4, 53))
>T : Symbol(T, Decl(file.tsx, 4, 35))
>JSX : Symbol(JSX, Decl(react.d.ts, 2352, 1))
>Element : Symbol(JSX.Element, Decl(react.d.ts, 2355, 27))

// OK
function Baz<T extends {b: number}, U extends {a: boolean, b:string}>(arg1: T, arg2: U) {
>Baz : Symbol(Baz, Decl(file.tsx, 5, 74))
>T : Symbol(T, Decl(file.tsx, 8, 13))
>b : Symbol(b, Decl(file.tsx, 8, 24))
>U : Symbol(U, Decl(file.tsx, 8, 35))
>a : Symbol(a, Decl(file.tsx, 8, 47))
>b : Symbol(b, Decl(file.tsx, 8, 58))
>arg1 : Symbol(arg1, Decl(file.tsx, 8, 70))
>T : Symbol(T, Decl(file.tsx, 8, 13))
>arg2 : Symbol(arg2, Decl(file.tsx, 8, 78))
>U : Symbol(U, Decl(file.tsx, 8, 35))
>Baz : Symbol(Baz, Decl(file.tsx, 4, 74))
>T : Symbol(T, Decl(file.tsx, 7, 13))
>b : Symbol(b, Decl(file.tsx, 7, 24))
>U : Symbol(U, Decl(file.tsx, 7, 35))
>a : Symbol(a, Decl(file.tsx, 7, 47))
>b : Symbol(b, Decl(file.tsx, 7, 58))
>arg1 : Symbol(arg1, Decl(file.tsx, 7, 70))
>T : Symbol(T, Decl(file.tsx, 7, 13))
>arg2 : Symbol(arg2, Decl(file.tsx, 7, 78))
>U : Symbol(U, Decl(file.tsx, 7, 35))

let a0 = <OverloadComponent {...arg1} a="hello" ignore-prop />;
>a0 : Symbol(a0, Decl(file.tsx, 9, 7))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 1, 31), Decl(file.tsx, 3, 53), Decl(file.tsx, 4, 101))
>arg1 : Symbol(arg1, Decl(file.tsx, 8, 70))
>a : Symbol(a, Decl(file.tsx, 9, 41))
>ignore-prop : Symbol(ignore-prop, Decl(file.tsx, 9, 51))
>a0 : Symbol(a0, Decl(file.tsx, 8, 7))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 0, 31), Decl(file.tsx, 2, 53), Decl(file.tsx, 3, 101))
>arg1 : Symbol(arg1, Decl(file.tsx, 7, 70))
>a : Symbol(a, Decl(file.tsx, 8, 41))
>ignore-prop : Symbol(ignore-prop, Decl(file.tsx, 8, 51))

let a1 = <OverloadComponent {...arg2} ignore-pro="hello world" />;
>a1 : Symbol(a1, Decl(file.tsx, 10, 7))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 1, 31), Decl(file.tsx, 3, 53), Decl(file.tsx, 4, 101))
>arg2 : Symbol(arg2, Decl(file.tsx, 8, 78))
>ignore-pro : Symbol(ignore-pro, Decl(file.tsx, 10, 41))
>a1 : Symbol(a1, Decl(file.tsx, 9, 7))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 0, 31), Decl(file.tsx, 2, 53), Decl(file.tsx, 3, 101))
>arg2 : Symbol(arg2, Decl(file.tsx, 7, 78))
>ignore-pro : Symbol(ignore-pro, Decl(file.tsx, 9, 41))

let a2 = <OverloadComponent {...arg2} />;
>a2 : Symbol(a2, Decl(file.tsx, 11, 7))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 1, 31), Decl(file.tsx, 3, 53), Decl(file.tsx, 4, 101))
>arg2 : Symbol(arg2, Decl(file.tsx, 8, 78))
>a2 : Symbol(a2, Decl(file.tsx, 10, 7))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 0, 31), Decl(file.tsx, 2, 53), Decl(file.tsx, 3, 101))
>arg2 : Symbol(arg2, Decl(file.tsx, 7, 78))

let a3 = <OverloadComponent {...arg1} ignore-prop />;
>a3 : Symbol(a3, Decl(file.tsx, 12, 7))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 1, 31), Decl(file.tsx, 3, 53), Decl(file.tsx, 4, 101))
>arg1 : Symbol(arg1, Decl(file.tsx, 8, 70))
>ignore-prop : Symbol(ignore-prop, Decl(file.tsx, 12, 41))
>a3 : Symbol(a3, Decl(file.tsx, 11, 7))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 0, 31), Decl(file.tsx, 2, 53), Decl(file.tsx, 3, 101))
>arg1 : Symbol(arg1, Decl(file.tsx, 7, 70))
>ignore-prop : Symbol(ignore-prop, Decl(file.tsx, 11, 41))

let a4 = <OverloadComponent />;
>a4 : Symbol(a4, Decl(file.tsx, 13, 7))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 1, 31), Decl(file.tsx, 3, 53), Decl(file.tsx, 4, 101))
>a4 : Symbol(a4, Decl(file.tsx, 12, 7))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 0, 31), Decl(file.tsx, 2, 53), Decl(file.tsx, 3, 101))

let a5 = <OverloadComponent {...arg2} ignore-prop="hello" {...arg1} />;
>a5 : Symbol(a5, Decl(file.tsx, 14, 7))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 1, 31), Decl(file.tsx, 3, 53), Decl(file.tsx, 4, 101))
>arg2 : Symbol(arg2, Decl(file.tsx, 8, 78))
>ignore-prop : Symbol(ignore-prop, Decl(file.tsx, 14, 41))
>arg1 : Symbol(arg1, Decl(file.tsx, 8, 70))
>a5 : Symbol(a5, Decl(file.tsx, 13, 7))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 0, 31), Decl(file.tsx, 2, 53), Decl(file.tsx, 3, 101))
>arg2 : Symbol(arg2, Decl(file.tsx, 7, 78))
>ignore-prop : Symbol(ignore-prop, Decl(file.tsx, 13, 41))
>arg1 : Symbol(arg1, Decl(file.tsx, 7, 70))

let a6 = <OverloadComponent {...arg2} ignore-prop {...arg1} />;
>a6 : Symbol(a6, Decl(file.tsx, 15, 7))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 1, 31), Decl(file.tsx, 3, 53), Decl(file.tsx, 4, 101))
>arg2 : Symbol(arg2, Decl(file.tsx, 8, 78))
>ignore-prop : Symbol(ignore-prop, Decl(file.tsx, 15, 41))
>arg1 : Symbol(arg1, Decl(file.tsx, 8, 70))
>a6 : Symbol(a6, Decl(file.tsx, 14, 7))
>OverloadComponent : Symbol(OverloadComponent, Decl(file.tsx, 0, 31), Decl(file.tsx, 2, 53), Decl(file.tsx, 3, 101))
>arg2 : Symbol(arg2, Decl(file.tsx, 7, 78))
>ignore-prop : Symbol(ignore-prop, Decl(file.tsx, 14, 41))
>arg1 : Symbol(arg1, Decl(file.tsx, 7, 70))
}

declare function Link<U>(l: {func: (arg: U)=>void}): JSX.Element;
>Link : Symbol(Link, Decl(file.tsx, 16, 1), Decl(file.tsx, 18, 65))
>U : Symbol(U, Decl(file.tsx, 18, 22))
>l : Symbol(l, Decl(file.tsx, 18, 25))
>func : Symbol(func, Decl(file.tsx, 18, 29))
>arg : Symbol(arg, Decl(file.tsx, 18, 36))
>U : Symbol(U, Decl(file.tsx, 18, 22))
>Link : Symbol(Link, Decl(file.tsx, 15, 1), Decl(file.tsx, 17, 65))
>U : Symbol(U, Decl(file.tsx, 17, 22))
>l : Symbol(l, Decl(file.tsx, 17, 25))
>func : Symbol(func, Decl(file.tsx, 17, 29))
>arg : Symbol(arg, Decl(file.tsx, 17, 36))
>U : Symbol(U, Decl(file.tsx, 17, 22))
>JSX : Symbol(JSX, Decl(react.d.ts, 2352, 1))
>Element : Symbol(JSX.Element, Decl(react.d.ts, 2355, 27))

declare function Link<U>(l: {func: (arg1:U, arg2: string)=>void}): JSX.Element;
>Link : Symbol(Link, Decl(file.tsx, 16, 1), Decl(file.tsx, 18, 65))
>U : Symbol(U, Decl(file.tsx, 19, 22))
>l : Symbol(l, Decl(file.tsx, 19, 25))
>func : Symbol(func, Decl(file.tsx, 19, 29))
>arg1 : Symbol(arg1, Decl(file.tsx, 19, 36))
>U : Symbol(U, Decl(file.tsx, 19, 22))
>arg2 : Symbol(arg2, Decl(file.tsx, 19, 43))
>Link : Symbol(Link, Decl(file.tsx, 15, 1), Decl(file.tsx, 17, 65))
>U : Symbol(U, Decl(file.tsx, 18, 22))
>l : Symbol(l, Decl(file.tsx, 18, 25))
>func : Symbol(func, Decl(file.tsx, 18, 29))
>arg1 : Symbol(arg1, Decl(file.tsx, 18, 36))
>U : Symbol(U, Decl(file.tsx, 18, 22))
>arg2 : Symbol(arg2, Decl(file.tsx, 18, 43))
>JSX : Symbol(JSX, Decl(react.d.ts, 2352, 1))
>Element : Symbol(JSX.Element, Decl(react.d.ts, 2355, 27))

function createLink(func: (a: number)=>void) {
>createLink : Symbol(createLink, Decl(file.tsx, 19, 79))
>func : Symbol(func, Decl(file.tsx, 20, 20))
>a : Symbol(a, Decl(file.tsx, 20, 27))
>createLink : Symbol(createLink, Decl(file.tsx, 18, 79))
>func : Symbol(func, Decl(file.tsx, 19, 20))
>a : Symbol(a, Decl(file.tsx, 19, 27))

let o = <Link func={func} />
>o : Symbol(o, Decl(file.tsx, 21, 7))
>Link : Symbol(Link, Decl(file.tsx, 16, 1), Decl(file.tsx, 18, 65))
>func : Symbol(func, Decl(file.tsx, 21, 17))
>func : Symbol(func, Decl(file.tsx, 20, 20))
>o : Symbol(o, Decl(file.tsx, 20, 7))
>Link : Symbol(Link, Decl(file.tsx, 15, 1), Decl(file.tsx, 17, 65))
>func : Symbol(func, Decl(file.tsx, 20, 17))
>func : Symbol(func, Decl(file.tsx, 19, 20))

let o1 = <Link func={(a:number, b:string)=>{}} />;
>o1 : Symbol(o1, Decl(file.tsx, 22, 7))
>Link : Symbol(Link, Decl(file.tsx, 16, 1), Decl(file.tsx, 18, 65))
>func : Symbol(func, Decl(file.tsx, 22, 18))
>a : Symbol(a, Decl(file.tsx, 22, 26))
>b : Symbol(b, Decl(file.tsx, 22, 35))
>o1 : Symbol(o1, Decl(file.tsx, 21, 7))
>Link : Symbol(Link, Decl(file.tsx, 15, 1), Decl(file.tsx, 17, 65))
>func : Symbol(func, Decl(file.tsx, 21, 18))
>a : Symbol(a, Decl(file.tsx, 21, 26))
>b : Symbol(b, Decl(file.tsx, 21, 35))
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
=== tests/cases/conformance/jsx/file.tsx ===

import React = require('react')
>React : typeof React

Expand Down Expand Up @@ -131,8 +130,8 @@ function createLink(func: (a: number)=>void) {
>o1 : JSX.Element
><Link func={(a:number, b:string)=>{}} /> : JSX.Element
>Link : { <U>(l: { func: (arg: U) => void; }): JSX.Element; <U>(l: { func: (arg1: U, arg2: string) => void; }): JSX.Element; }
>func : (a: number, b: string) => void
>(a:number, b:string)=>{} : (a: number, b: string) => void
>func : (a: number, b: string) => any
>(a:number, b:string)=>{} : (a: number, b: string) => any
>a : number
>b : string
}
Loading