Skip to content

Commit

Permalink
Merge pull request #4106 from RyanCavanaugh/allowTsxStringIndexers
Browse files Browse the repository at this point in the history
Implements #4105 - a string indexer in 'props' disables errors due to surplus attributes
  • Loading branch information
RyanCavanaugh committed Aug 6, 2015
2 parents 085f0df + 5f96788 commit ba77592
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7273,10 +7273,19 @@ namespace ts {
else if (elementAttributesType && !isTypeAny(elementAttributesType)) {
let correspondingPropSymbol = getPropertyOfType(elementAttributesType, node.name.text);
correspondingPropType = correspondingPropSymbol && getTypeOfSymbol(correspondingPropSymbol);
// If there's no corresponding property with this name, error
if (!correspondingPropType && isUnhyphenatedJsxName(node.name.text)) {
error(node.name, Diagnostics.Property_0_does_not_exist_on_type_1, node.name.text, typeToString(elementAttributesType));
return unknownType;
if (isUnhyphenatedJsxName(node.name.text)) {
// Maybe there's a string indexer?
let indexerType = getIndexTypeOfType(elementAttributesType, IndexKind.String);
if (indexerType) {
correspondingPropType = indexerType
}
else {
// If there's no corresponding property with this name, error
if (!correspondingPropType) {
error(node.name, Diagnostics.Property_0_does_not_exist_on_type_1, node.name.text, typeToString(elementAttributesType));
return unknownType;
}
}
}
}

Expand Down
35 changes: 35 additions & 0 deletions tests/baselines/reference/tsxAttributeResolution10.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
tests/cases/conformance/jsx/file.tsx(11,14): error TS2322: Type 'string' is not assignable to type 'boolean'.


==== tests/cases/conformance/jsx/react.d.ts (0 errors) ====

declare module JSX {
interface Element { }
interface IntrinsicElements {
}
interface ElementAttributesProperty {
props;
}
}

==== tests/cases/conformance/jsx/file.tsx (1 errors) ====
export class MyComponent {
render() {
}

props: {
[s: string]: boolean;
}
}

// Should be an error
<MyComponent bar='world' />;
~~~~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'boolean'.

// Should be OK
<MyComponent bar={true} />;

// Should be ok
<MyComponent data-bar='hello' />;

50 changes: 50 additions & 0 deletions tests/baselines/reference/tsxAttributeResolution10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//// [tests/cases/conformance/jsx/tsxAttributeResolution10.tsx] ////

//// [react.d.ts]

declare module JSX {
interface Element { }
interface IntrinsicElements {
}
interface ElementAttributesProperty {
props;
}
}

//// [file.tsx]
export class MyComponent {
render() {
}

props: {
[s: string]: boolean;
}
}

// Should be an error
<MyComponent bar='world' />;

// Should be OK
<MyComponent bar={true} />;

// Should be ok
<MyComponent data-bar='hello' />;


//// [file.jsx]
define(["require", "exports"], function (require, exports) {
var MyComponent = (function () {
function MyComponent() {
}
MyComponent.prototype.render = function () {
};
return MyComponent;
})();
exports.MyComponent = MyComponent;
// Should be an error
<MyComponent bar='world'/>;
// Should be OK
<MyComponent bar={true}/>;
// Should be ok
<MyComponent data-bar='hello'/>;
});
31 changes: 31 additions & 0 deletions tests/cases/conformance/jsx/tsxAttributeResolution10.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//@jsx: preserve
//@module: amd

//@filename: react.d.ts
declare module JSX {
interface Element { }
interface IntrinsicElements {
}
interface ElementAttributesProperty {
props;
}
}

//@filename: file.tsx
export class MyComponent {
render() {
}

props: {
[s: string]: boolean;
}
}

// Should be an error
<MyComponent bar='world' />;

// Should be OK
<MyComponent bar={true} />;

// Should be ok
<MyComponent data-bar='hello' />;

0 comments on commit ba77592

Please sign in to comment.