Skip to content

Commit

Permalink
feat: Implement this types in method parameters (#1062)
Browse files Browse the repository at this point in the history
**Description:**

```ts
// @strictNullChecks: true
// @declaration: true


class A<T> {
	props: T & { foo: string };
}

class B extends A<{ x: number}> {
	f(p: this["props"]) {
		p.x;
	}
}
```
  • Loading branch information
sunrabbit123 authored Aug 10, 2023
1 parent 9d957b4 commit 0130bbe
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
8 changes: 7 additions & 1 deletion crates/stc_ts_file_analyzer/src/analyzer/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,6 @@ impl Analyzer<'_, '_> {
},
_ => {}
}

return Err(ErrorKind::Unimplemented {
span,
msg: format!("access_property_inner: global_this: {:?}", prop),
Expand Down Expand Up @@ -3053,6 +3052,13 @@ impl Analyzer<'_, '_> {
// TODO
return Ok(Type::any(span, Default::default()));
}
Some(ScopeKind::Method { is_static: false }) => {
if let Some(Some(parent)) = scope.map(|s| s.parent()) {
if let Some(this) = parent.this().map(|v| v.into_owned()).or(parent.get_super_class(false)) {
return self.access_property(span, &this, prop, type_mode, id_ctx, opts);
}
}
}
None => {
// Global this
return Ok(Type::any(span, Default::default()));
Expand Down
13 changes: 13 additions & 0 deletions crates/stc_ts_file_analyzer/tests/pass-only/types/keyof/2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @strictNullChecks: true
// @declaration: true


class A<T> {
props: T & { foo: string };
}

class B extends A<{ x: number}> {
f(p: this["props"]) {
p.x;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"extra_errors": {
"TS2345": 8,
"TS2339": 2,
"TS2322": 12,
"TS0": 1
"TS2322": 12
},
"extra_error_lines": {
"TS2345": [
Expand Down Expand Up @@ -35,9 +34,6 @@
640,
645,
658
],
"TS0": [
523
]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 5,
extra_error: 23,
extra_error: 22,
panic: 0,
}
2 changes: 1 addition & 1 deletion crates/stc_ts_type_checker/tests/tsc-stats.rust-debug
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 3502,
matched_error: 6533,
extra_error: 767,
extra_error: 766,
panic: 74,
}

0 comments on commit 0130bbe

Please sign in to comment.