-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Flow Analyze don't recognize combined Intersection and Union Type #12345
Comments
This works as intended. You can use interface extends instead, or remove |
Just to note that removal of the |
@HerringtonDarkholme Right, it works after removing But what if I need to ensure all extended |
interface BaseChartSeries {
name: string;
type: string;
}
//Line
interface LineSeriesSettings extends BaseChartSeries {
type: 'line';
lineColor?: string;
} |
@HerringtonDarkholme
But it compiled Error.
So why there is error when nested extend relationship exist? //Base Class for ChartSeries
interface BaseChartSeries {
name: string;
type: string;
}
interface BaseSeriesSettings{
type: string;
commonProp1: string;
commonProp2: number;
}
//Line
interface LineSeriesSettings extends BaseSeriesSettings{
type: 'line';
lineColor?: string;
}
type LineSeries = BaseChartSeries & LineSeriesSettings;
//Bar
interface BarSeriesSettings extends BaseSeriesSettings{
type: 'bar';
}
type BarSeries = BaseChartSeries & BarSeriesSettings;
//Export ChartSeries
type ChartSeries = LineSeries | BarSeries;
//Test Code (Compile Error)
var series:ChartSeries;
if (series.type == 'line') {
var a = series.lineColor;
} |
@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. |
@HerringtonDarkholme Main problem is: Example
So should this be working as intended? (B is to change Def A,B,C interface A {
type?: 'a';
a?: string;
}
interface B{
type: 'a' | 'c';
}
interface C{
type?: 'c';
c?: string;
} Code 1: //change type to required, works well
var ba: B&A;
if(ba.type == 'a'){
console.log(ba.a)
}
//change type to required, works well
var bc: B&C;
if(bc.type == 'c'){
console.log(bc.c)
} Code 2: //works well
var ac: A|C;
if(ac.type == 'a'){
console.log(ac.a)
} Code 3: //Compiled error
var abc: B&A | B&C;
if(abc.type == 'a'){
console.log(abc.a)
} |
You initial example code works in latest if you remove 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 @gcnew ~ |
It may not be vague in a specific scene. Another example may be React. |
//Base Class for ChartSeries
interface BaseChartSeries {
name: string;
type: string;
}
interface BaseSeriesSettings{
type: string;
commonProp1: string;
commonProp2: number;
}
//Line
interface LineSeriesSettings extends BaseSeriesSettings, BaseChartSeries{
type: 'line';
lineColor?: string;
}
type LineSeries = LineSeriesSettings;
//Bar
interface BarSeriesSettings extends BaseSeriesSettings, BaseChartSeries {
type: 'bar';
}
type BarSeries = BarSeriesSettings;
//Export ChartSeries
type ChartSeries = LineSeries | BarSeries;
//Test Code (Compile Error)
var series:ChartSeries;
if (series.type == 'line') {
var a = series.lineColor;
} If you still need LineSeriesSettings without BaseChartSeries, declare new interface. |
Maybe the new Mapped Types and |
Thanks @HerringtonDarkholme , it works and useful. |
TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)
Both 2.0.10 and 2.1.1
Problem
Flow Analyze don't work as expected when combining Intersection and Union syntax.
Example
B&A
B&C
works well as Code1.A|C
works well as Code2.B&A | B&C
compiled error as Code3.So should this be working as intended?
(B is to change
type
to required, as Code1, this works.)Def A,B,C
Code 1:
B&A
B&C
works wellCode 2:
A|C
works wellCode 3:
B&A | B&C
not worksThe text was updated successfully, but these errors were encountered: