Make tuple and custom array declared as interface compatible#413
Make tuple and custom array declared as interface compatible#413devgony wants to merge 41 commits intodudykr:mainfrom
Conversation
| for type_arg in extend.type_args.as_ref() { | ||
| for param in type_arg.to_owned().params { | ||
| match param { | ||
| Type::Array(array) => return Ok(Cow::Owned(Type::Array(array))), |
There was a problem hiding this comment.
I tried early return here to see what makes difference but it does not affect the result of ./scripts/test.sh arityAndOrderCompatibility at all
Is ./scripts/test.sh arityAndOrderCompatibility correct to test?
There was a problem hiding this comment.
You should add Type::Interface to
stc/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs
Lines 512 to 521 in d6dc481
I made it to remove |
|
No, it's okay to split PRs. I'll review this PR once you undraft it. |
|
I'm trying to solve nested_assignment error in advance of undraft but cannot get which part is wrong yet. |
| }) => { | ||
| for extend in extends { | ||
| let types = extend.to_owned().type_args.into_iter().flat_map(|cur| cur.params); | ||
| let union_type = Type::new_union(*span, types); |
There was a problem hiding this comment.
It almost solved nest_assignment errors but not RegExpExecArray yet.
Could you give me any hint where to get RegExpExecArray type to return Array?
There was a problem hiding this comment.
RegExpExecArray is declared as
interface RegExpExecArray extends Array<string> {
index: number;
input: string;
}So you need to check the type arguments of Array
4eecbee to
370071b
Compare
|
Can you sign the CLA? |
| for extend in extends { | ||
| let types = extend.to_owned().type_args.into_iter().flat_map(|cur| cur.params); | ||
| let union_type = Type::new_union(*span, types); | ||
| if let box RExpr::Ident(ident) = &extend.expr { |
There was a problem hiding this comment.
I think we should check for Type::Array after using self.type_of_ts_entity_name
| | Type::EnumVariant(..) | ||
| | Type::Param(_) | ||
| | Type::Module(_) => return Ok(ty), | ||
| Type::Interface(Interface { |
There was a problem hiding this comment.
Did you mean we should add more condition here to decrease the range of match arm?
Or It is totally wrong location to implement?
There was a problem hiding this comment.
Can you give any tips like where to start first?
There was a problem hiding this comment.
I think some match expressions should be added to
| tracker, | ||
| }) => { | ||
| for parent in extends { | ||
| let parent = self.type_of_ts_entity_name(parent.span(), &parent.expr, parent.type_args.as_deref())?; |
There was a problem hiding this comment.
A user can add methods to their own array type.
There was a problem hiding this comment.
And this does not handle tuple types correctly
| metadata: InterfaceMetadata { common }, | ||
| tracker, | ||
| }) => { | ||
| for parent in extends { |
| }) => { | ||
| for parent in extends { | ||
| let ty = self.type_of_ts_entity_name(parent.span(), &parent.expr, parent.type_args.as_deref())?; | ||
| if let Type::Array(_) = &ty { |
There was a problem hiding this comment.
Checking for Type::Array is not enough, and we should handle this by accessing iterating over properties
There was a problem hiding this comment.
interface MyTuple extends Array<unknown> {
0: number
1: number
}
declare let a: MyTuple
declare let b: [number, number]
a = b // ok
b = a // errorThere was a problem hiding this comment.
interface MyTuple extends Array<any> {
0: number
1: number
}
declare let a: MyTuple
declare let b: [number, number]
a = b // ok
b = a // errorThere was a problem hiding this comment.
As you can see, we can't rely on the type argument in extends Array<T>
| { | ||
| println!("@@@\n{lkind:#?}:{rkind:#?}"); | ||
| let diff = lkind == rkind; | ||
| println!("@@@\n{diff}"); |
There was a problem hiding this comment.
- Is there any existing function to compare two KeywordType and return reasonable error?
- Do you think I'm going correct way?
There was a problem hiding this comment.
- You can use
assign_with_opts(...)?to do it - I think you should not reimplement whole logic here, but delegate to a recursive call to
assign_with_opts
There was a problem hiding this comment.
It looks like we should keep logic here.
Otherwise, it breaks other tests
https://github.com/dudykr/stc/actions/runs/4110781244/jobs/7093929846
|
Adding more tests would be awsome. Thank you! |
|
Adding more tests would be awesome. Thank you! |
| // x = z; // should get TS2322 but pass | ||
| y = x; | ||
| y = z; | ||
| // z = x; // should pass but got TS2322 |
There was a problem hiding this comment.
According to TS Playground,
It seems that x = z and z = x work in opposite directions.
Should I follow TS playground and fix it in this PR?

TSPlayground
There was a problem hiding this comment.
@sunrabbit123
Thanks for the information.
So it looks like it does not need to be fixed, at least in this PR.
There was a problem hiding this comment.
z = x must be not emit 2322
x = z must be emit 2322
I think the code is fine.
And it seems to be out of the conventional test case.
When I commented above, I thought it was an existing test case.
I'm sorry.
| required_error: 874, | ||
| matched_error: 1749, | ||
| extra_error: 284, | ||
| panic: 20, |
There was a problem hiding this comment.
I got thread 'conformance::types::uniqueSymbol::uniqueSymbolsDeclarations.ts' has overflowed its stack
Should i pass some option to avoid the overflow?
There was a problem hiding this comment.
The way is simple
- Check that the code you wrote affects stackoverflow.
- If it does, insert some stackguards.
- if it doesn't, it's broken and you can report it in an issue
Thanks you
The following code detects the overflow and handles it.
let _stack = match stack::track(actual_span) {
Ok(v) => v,
Err(err) => {
// print_backtrace();
return Err(err.into());
}
};
|
| var o2: [string, number] = y; | ||
| var o3: [string, number] = y; | ||
|
|
||
| x = y; |
There was a problem hiding this comment.
This test file is copied from the official tsc conformance test suite, so we should not modify it.
kdy1
left a comment
There was a problem hiding this comment.
It seems like this PR does not improve the test stats?
Description: Make tuple and custom array declared as interface compatible
BREAKING CHANGE:
Related issue (if exists): To resolve #216