-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fixing react defaultize+generic default props interaction #27088
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
weswigham
merged 4 commits into
microsoft:master
from
weswigham:fixing-that-strange-react-problem
Sep 14, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
tests/baselines/reference/conditionalTypeContextualTypeSimplificationsSuceeds.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//// [conditionalTypeContextualTypeSimplificationsSuceeds.ts] | ||
// repro from https://github.com/Microsoft/TypeScript/issues/26395 | ||
interface Props { | ||
when: (value: string) => boolean; | ||
} | ||
|
||
function bad<P extends Props>( | ||
attrs: string extends keyof P ? { [K in keyof P]: P[K] } : { [K in keyof P]: P[K] }) { } | ||
function good1<P extends Props>( | ||
attrs: string extends keyof P ? P : { [K in keyof P]: P[K] }) { } | ||
function good2<P extends Props>( | ||
attrs: { [K in keyof P]: P[K] }) { } | ||
|
||
bad({ when: value => false }); | ||
good1({ when: value => false }); | ||
good2({ when: value => false }); | ||
|
||
//// [conditionalTypeContextualTypeSimplificationsSuceeds.js] | ||
"use strict"; | ||
function bad(attrs) { } | ||
function good1(attrs) { } | ||
function good2(attrs) { } | ||
bad({ when: function (value) { return false; } }); | ||
good1({ when: function (value) { return false; } }); | ||
good2({ when: function (value) { return false; } }); |
68 changes: 68 additions & 0 deletions
68
tests/baselines/reference/conditionalTypeContextualTypeSimplificationsSuceeds.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
=== tests/cases/compiler/conditionalTypeContextualTypeSimplificationsSuceeds.ts === | ||
// repro from https://github.com/Microsoft/TypeScript/issues/26395 | ||
interface Props { | ||
>Props : Symbol(Props, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 0, 0)) | ||
|
||
when: (value: string) => boolean; | ||
>when : Symbol(Props.when, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 1, 17)) | ||
>value : Symbol(value, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 2, 11)) | ||
} | ||
|
||
function bad<P extends Props>( | ||
>bad : Symbol(bad, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 3, 1)) | ||
>P : Symbol(P, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 5, 13)) | ||
>Props : Symbol(Props, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 0, 0)) | ||
|
||
attrs: string extends keyof P ? { [K in keyof P]: P[K] } : { [K in keyof P]: P[K] }) { } | ||
>attrs : Symbol(attrs, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 5, 30)) | ||
>P : Symbol(P, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 5, 13)) | ||
>K : Symbol(K, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 6, 39)) | ||
>P : Symbol(P, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 5, 13)) | ||
>P : Symbol(P, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 5, 13)) | ||
>K : Symbol(K, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 6, 39)) | ||
>K : Symbol(K, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 6, 66)) | ||
>P : Symbol(P, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 5, 13)) | ||
>P : Symbol(P, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 5, 13)) | ||
>K : Symbol(K, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 6, 66)) | ||
|
||
function good1<P extends Props>( | ||
>good1 : Symbol(good1, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 6, 92)) | ||
>P : Symbol(P, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 7, 15)) | ||
>Props : Symbol(Props, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 0, 0)) | ||
|
||
attrs: string extends keyof P ? P : { [K in keyof P]: P[K] }) { } | ||
>attrs : Symbol(attrs, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 7, 32)) | ||
>P : Symbol(P, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 7, 15)) | ||
>P : Symbol(P, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 7, 15)) | ||
>K : Symbol(K, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 8, 43)) | ||
>P : Symbol(P, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 7, 15)) | ||
>P : Symbol(P, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 7, 15)) | ||
>K : Symbol(K, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 8, 43)) | ||
|
||
function good2<P extends Props>( | ||
>good2 : Symbol(good2, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 8, 69)) | ||
>P : Symbol(P, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 9, 15)) | ||
>Props : Symbol(Props, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 0, 0)) | ||
|
||
attrs: { [K in keyof P]: P[K] }) { } | ||
>attrs : Symbol(attrs, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 9, 32)) | ||
>K : Symbol(K, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 10, 14)) | ||
>P : Symbol(P, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 9, 15)) | ||
>P : Symbol(P, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 9, 15)) | ||
>K : Symbol(K, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 10, 14)) | ||
|
||
bad({ when: value => false }); | ||
>bad : Symbol(bad, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 3, 1)) | ||
>when : Symbol(when, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 12, 5)) | ||
>value : Symbol(value, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 12, 11)) | ||
|
||
good1({ when: value => false }); | ||
>good1 : Symbol(good1, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 6, 92)) | ||
>when : Symbol(when, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 13, 7)) | ||
>value : Symbol(value, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 13, 13)) | ||
|
||
good2({ when: value => false }); | ||
>good2 : Symbol(good2, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 8, 69)) | ||
>when : Symbol(when, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 14, 7)) | ||
>value : Symbol(value, Decl(conditionalTypeContextualTypeSimplificationsSuceeds.ts, 14, 13)) | ||
|
53 changes: 53 additions & 0 deletions
53
tests/baselines/reference/conditionalTypeContextualTypeSimplificationsSuceeds.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
=== tests/cases/compiler/conditionalTypeContextualTypeSimplificationsSuceeds.ts === | ||
// repro from https://github.com/Microsoft/TypeScript/issues/26395 | ||
interface Props { | ||
when: (value: string) => boolean; | ||
>when : (value: string) => boolean | ||
>value : string | ||
} | ||
|
||
function bad<P extends Props>( | ||
>bad : <P extends Props>(attrs: string extends keyof P ? { [K in keyof P]: P[K]; } : { [K in keyof P]: P[K]; }) => void | ||
|
||
attrs: string extends keyof P ? { [K in keyof P]: P[K] } : { [K in keyof P]: P[K] }) { } | ||
>attrs : string extends keyof P ? { [K in keyof P]: P[K]; } : { [K in keyof P]: P[K]; } | ||
|
||
function good1<P extends Props>( | ||
>good1 : <P extends Props>(attrs: string extends keyof P ? P : { [K in keyof P]: P[K]; }) => void | ||
|
||
attrs: string extends keyof P ? P : { [K in keyof P]: P[K] }) { } | ||
>attrs : string extends keyof P ? P : { [K in keyof P]: P[K]; } | ||
|
||
function good2<P extends Props>( | ||
>good2 : <P extends Props>(attrs: { [K in keyof P]: P[K]; }) => void | ||
|
||
attrs: { [K in keyof P]: P[K] }) { } | ||
>attrs : { [K in keyof P]: P[K]; } | ||
|
||
bad({ when: value => false }); | ||
>bad({ when: value => false }) : void | ||
>bad : <P extends Props>(attrs: string extends keyof P ? { [K in keyof P]: P[K]; } : { [K in keyof P]: P[K]; }) => void | ||
>{ when: value => false } : { when: (value: string) => false; } | ||
>when : (value: string) => false | ||
>value => false : (value: string) => false | ||
>value : string | ||
>false : false | ||
|
||
good1({ when: value => false }); | ||
>good1({ when: value => false }) : void | ||
>good1 : <P extends Props>(attrs: string extends keyof P ? P : { [K in keyof P]: P[K]; }) => void | ||
>{ when: value => false } : { when: (value: string) => false; } | ||
>when : (value: string) => false | ||
>value => false : (value: string) => false | ||
>value : string | ||
>false : false | ||
|
||
good2({ when: value => false }); | ||
>good2({ when: value => false }) : void | ||
>good2 : <P extends Props>(attrs: { [K in keyof P]: P[K]; }) => void | ||
>{ when: value => false } : { when: (value: string) => false; } | ||
>when : (value: string) => false | ||
>value => false : (value: string) => false | ||
>value : string | ||
>false : false | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
tests/baselines/reference/reactDefaultPropsInferenceSuccess.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
tests/cases/compiler/reactDefaultPropsInferenceSuccess.tsx(26,36): error TS2322: Type '(value: string) => void' is not assignable to type '(value: string) => boolean'. | ||
Type 'void' is not assignable to type 'boolean'. | ||
tests/cases/compiler/reactDefaultPropsInferenceSuccess.tsx(48,37): error TS2322: Type '(value: string) => void' is not assignable to type '(value: string) => boolean'. | ||
Type 'void' is not assignable to type 'boolean'. | ||
|
||
|
||
==== tests/cases/compiler/reactDefaultPropsInferenceSuccess.tsx (2 errors) ==== | ||
/// <reference path="/.lib/react16.d.ts" /> | ||
|
||
import React from 'react'; | ||
|
||
interface BaseProps { | ||
when?: (value: string) => boolean; | ||
} | ||
|
||
interface Props extends BaseProps { | ||
} | ||
|
||
class FieldFeedback<P extends Props = BaseProps> extends React.Component<P> { | ||
static defaultProps = { | ||
when: () => true | ||
}; | ||
|
||
render() { | ||
return <div>Hello</div>; | ||
} | ||
} | ||
|
||
// OK | ||
const Test1 = () => <FieldFeedback when={value => !!value} />; | ||
|
||
// Error: Void not assignable to boolean | ||
const Test2 = () => <FieldFeedback when={value => console.log(value)} />; | ||
~~~~ | ||
!!! error TS2322: Type '(value: string) => void' is not assignable to type '(value: string) => boolean'. | ||
!!! error TS2322: Type 'void' is not assignable to type 'boolean'. | ||
!!! related TS6500 tests/cases/compiler/reactDefaultPropsInferenceSuccess.tsx:6:3: The expected type comes from property 'when' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<FieldFeedback<Props>> & Pick<Readonly<{ children?: ReactNode; }> & Readonly<Props>, "children"> & Partial<Pick<Readonly<{ children?: ReactNode; }> & Readonly<Props>, "when">> & Partial<Pick<{ when: () => boolean; }, never>>' | ||
|
||
|
||
interface MyPropsProps extends Props { | ||
when: (value: string) => boolean; | ||
} | ||
|
||
class FieldFeedback2<P extends MyPropsProps = MyPropsProps> extends FieldFeedback<P> { | ||
static defaultProps = { | ||
when: () => true | ||
}; | ||
|
||
render() { | ||
this.props.when("now"); // OK, always defined | ||
return <div>Hello</div>; | ||
} | ||
} | ||
|
||
// OK | ||
const Test3 = () => <FieldFeedback2 when={value => !!value} />; | ||
|
||
// Error: Void not assignable to boolean | ||
const Test4 = () => <FieldFeedback2 when={value => console.log(value)} />; | ||
~~~~ | ||
!!! error TS2322: Type '(value: string) => void' is not assignable to type '(value: string) => boolean'. | ||
!!! error TS2322: Type 'void' is not assignable to type 'boolean'. | ||
!!! related TS6500 tests/cases/compiler/reactDefaultPropsInferenceSuccess.tsx:30:3: The expected type comes from property 'when' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<FieldFeedback2<MyPropsProps>> & Pick<Readonly<{ children?: ReactNode; }> & Readonly<MyPropsProps>, "children"> & Partial<Pick<Readonly<{ children?: ReactNode; }> & Readonly<MyPropsProps>, "when">> & Partial<Pick<{ when: () => boolean; }, never>>' | ||
|
||
// OK | ||
const Test5 = () => <FieldFeedback2 />; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This intersection is unfortunate. Is there a way to avoid adding
(a: { x: string }) => string
to the type?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The target is
Props & BaseProps<Values>
where we've already inferedProps
as{ initialValues: {x: string}; nextValues: (cur: {x: string}) => string; }
(since that's the entire attributes type as written, which is inferred toProps
) andBaseProps<Values>
asinterface BaseProps<T> { initialValues: T; nextValues: (cur: T) => T; }
instantiated withT = Values = {x: string}
(since that's what first matchesT
in the first context-free inference pass) - the intersection of both'snextValues
member is the type we see here. Everything is as it should be.