microsoft / microsoft/TypeScript
[Regression] Circular reference error when passing a class expression with non-primitive static properties to a generic function
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
class expression static type
🕗 Version & Regression Information
- This changed between versions 4.0.5 and 4.1.5
⏯ Playground Link
💻 Code
declare function returnInput<T>(input: T): T;
const a = returnInput(class A {
static foo = {}
// ^?
})
🙁 Actual behavior
A.foo: any
Error: 'foo' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.(7022)
And also note that this bug only happens when the type of foo is not a primitive type.
🙂 Expected behavior
A.foo: {}, as it works in 4.0.5
Additional information about the issue
To define a React class component, it is a common practice to include its "sub-components" as private static properties. I also use a "observer" function to make the state change auto observed. So the real-world code may look like:
import { observer } from 'mobx-react';
import { styled } from 'styled-components';
@observer
class ListView extends React.Component {
// render method here...
private static readonly Item = observer(class Item extends React.Component {
private static readonly Header = styled.h4` // <- Cannot infer the type of Header
// CSS style here...
`;
})
}
A workaround is to use static block to initialize the static property but it's cumbersome:
const a = returnInput(class A {
static foo
// ^? OK
static { this.foo = {}; }
})
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground and compare the regression between versions 4.0.5 and 4.1.5 using the generic function and class-expression example. Investigate why the non-primitive static property is inferred as any, and consider the issue done when the example reports A.foo as {} without error 7022 while preserving the documented workaround behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100