You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Flow Analyze don't recognize combined Intersection and Union Type #12345
//change type to required, works wellvarba: B&A;if(ba.type=='a'){console.log(ba.a)}//change type to required, works wellvarbc: B&C;if(bc.type=='c'){console.log(bc.c)}
This works as intended.
You should not use the intersection syntax, or BaseCharSeries should not have type field.
The intersection type has conflicting string type that invalidates flow analyze.
You can use interface extends instead, or remove type field.
BaseSeriesSettings is base class for series settings, may include some common property, every field may be optional, because it may be a small Object only for override some field. BaseSeries is base class extends BaseSeriesSettings , with some fields (from XxSeriesSettings) to be marked as required (e.g. type) , and some other common property (e.g. name) . ChartSeries is just like a versatile class with automatically type check to use.
ChartSeries may be final thing transfered to a Chart component, and a SeriesSettings may be a optional Object to override something (so type and some other fields be optionnal) .
But it compiled Error.
test.ts(32,17): error TS2339: Property 'lineColor' does not exist on type 'ChartSeries'.
Property 'lineColor' does not exist on type 'BarSeries'.
So why there is error when nested extend relationship exist?
//Base Class for ChartSeriesinterfaceBaseChartSeries{name: string;type: string;}interfaceBaseSeriesSettings{type: string;commonProp1: string;commonProp2: number;}//LineinterfaceLineSeriesSettingsextendsBaseSeriesSettings{type: 'line';lineColor?: string;}typeLineSeries=BaseChartSeries&LineSeriesSettings;//BarinterfaceBarSeriesSettingsextendsBaseSeriesSettings{type: 'bar';}typeBarSeries=BaseChartSeries&BarSeriesSettings;//Export ChartSeriestypeChartSeries=LineSeries|BarSeries;//Test Code (Compile Error)varseries:ChartSeries;if(series.type=='line'){vara=series.lineColor;}
//change type to required, works wellvarba: B&A;if(ba.type=='a'){console.log(ba.a)}//change type to required, works wellvarbc: B&C;if(bc.type=='c'){console.log(bc.c)}
changed the title [-]Flow Analyze don't recognize nested type&interface[/-][+]Flow Analyze don't recognize combined Intersection and Union Type[/+]on Nov 21, 2016
You initial example code works in latest if you remove type: string from BaseChartSeries. If you are suggesting simplification of string & 'literal' to just 'literal' it's already been suggested in #12289.
The latest example seems to be a special case of simplification with literal union. On a side note: such use of optional discrimination properties looks vague and rather confusing.
Thanks Marin Marinov (@gcnew) ~
But removing type: string is different with my intention.
As type in B (lastest code), the purpose is to keep 2 same interface, which have different requirement of fields.
It may not be vague in a specific scene.
like ChartSeries and SeriesSettings.
Another example may be React.
Every React component have a State interface.
You need provide every required field in initialization of a State,
but don't need to do so when setState({xxx:xxx})
Activity
HerringtonDarkholme commentedon Nov 18, 2016
This works as intended.
You should not use the intersection syntax, or BaseCharSeries should not have
typefield.The intersection
typehas conflictingstringtype that invalidates flow analyze.You can use interface extends instead, or remove
typefield.kitsonk commentedon Nov 18, 2016
Just to note that removal of the
typefromBaseChartSeriesstill does not narrow.HerringtonDarkholme commentedon Nov 18, 2016
Kitson Kelly (@kitsonk) It works in nightly due to #11717
k8w commentedon Nov 21, 2016
Herrington Darkholme (@HerringtonDarkholme) Right, it works after removing
typefromBaseChartSeries.But what if I need to ensure all extended
XXXSeriesto contain fieldtype?HerringtonDarkholme commentedon Nov 21, 2016
k8w commentedon Nov 21, 2016
Herrington Darkholme (@HerringtonDarkholme)
My actual scene is below with the same problem.
BaseSeriesSettingsis base class for series settings, may include some common property, every field may be optional, because it may be a small Object only for override some field.BaseSeriesis base class extends BaseSeriesSettings , with some fields (fromXxSeriesSettings) to be marked as required (e.g.type) , and some other common property (e.g.name) .ChartSeriesis just like a versatile class with automatically type check to use.ChartSeriesmay be final thing transfered to a Chart component, and aSeriesSettingsmay be a optional Object to override something (sotypeand some other fields beoptionnal) .But it compiled Error.
So why there is error when nested extend relationship exist?
HerringtonDarkholme commentedon Nov 21, 2016
@twoeo Github issues isn't for Q&A. Would you mind putting this in stackoverflow? I'm happy to answer it.
Your original issues isn't a bug but rather a question. So I think we can close discussion here and move to a better place.
k8w commentedon Nov 21, 2016
Herrington Darkholme (@HerringtonDarkholme)
Well.. Never regard this as Q&A, maybe I should change a way to describe.
Main problem is:
Flow Analyze don't work as expected when combining Intersection and Union syntax.
Example
B&AB&Cworks well as Code1.A|Cworks well as Code2.B&A | B&Ccompiled error as Code3.So should this be working as intended?
(B is to change
typeto required, as Code1, this works.)Def A,B,C
Code 1:
B&AB&Cworks wellCode 2:
A|Cworks wellCode 3:
B&A | B&Cnot works[-]Flow Analyze don't recognize nested type&interface[/-][+]Flow Analyze don't recognize combined Intersection and Union Type[/+]gcnew commentedon Nov 21, 2016
You initial example code works in latest if you remove
type: stringfromBaseChartSeries. If you are suggesting simplification ofstring & 'literal'to just'literal'it's already been suggested in #12289.The latest example seems to be a special case of simplification with literal union. On a side note: such use of optional discrimination properties looks vague and rather confusing.
k8w commentedon Nov 21, 2016
Thanks Marin Marinov (@gcnew) ~
But removing
type: stringis different with my intention.As
typeinB(lastest code), the purpose is to keep 2 same interface, which have different requirement of fields.k8w commentedon Nov 21, 2016
It may not be vague in a specific scene.
like
ChartSeriesandSeriesSettings.Another example may be React.
Every React component have a State interface.
You need provide every required field in initialization of a State,
but don't need to do so when setState({xxx:xxx})
HerringtonDarkholme commentedon Nov 21, 2016
If you still need LineSeriesSettings without BaseChartSeries, declare new interface.
gcnew commentedon Nov 21, 2016
Maybe the new Mapped Types and
Partial<T>can help with the optionalization.k8w commentedon Nov 21, 2016
Thanks Herrington Darkholme (@HerringtonDarkholme) , it works and useful.
Thanks Marin Marinov (@gcnew) , this may be best solution, waiting for 2.2.