-
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
Recursive generics in function parameters are inferred as {}, and produce funky Intellisense #17522
Comments
The problem here is that in order to get the type But since there are no completed inferences for @ahejlsberg worked on a way to prevent this when inferring the type of |
This seems to be doable with some slight changes: declare function build<D>(options: OptionsWithData<D>): void;
declare function build(options: Options): void;
interface Options {
processSelf(self: Options): void;
}
interface OptionsWithData<TData> extends Options {
data: TData
processSelf(self: OptionsWithData<TData>): void;
}
build({
data: { hello: "world" },
processSelf(self) {
self.data.hello;
}
}); |
it is important to note that the constraint is not an inference location. all what |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.4.2
Code
Expected behavior:
The inferred type of
O
should be the type of the options object passed in.self
should be of typeO
.self.data.hello
should be accessible within theprocessSelf
function.If this is not supposed to work, I would expect
O extends Options<O>
to be an error.Actual behavior:
The inferred type of
O
isOptions<{}>
.self
is of type{}
.self.data.hello
is therefore not accessible.Additionally, the Intellisense VSCode is providing for this seems to be wrong, or at least confusing. If this is unrelated then I'm happy to make a separate issue for that and remove it from here.
As these screenshots show,
self
is inferred as{}
but it allows access toself.data
.self.data
is given typeany
according to the tooltip, but the error at.hello
isWhen I explicitly specify
self: {}
,self.data
becomes an error.Real scenario:
In my real scenario
Options
has many more optional properties, and I wantself
to have only the properties specified, not every optional property. There are also many optional functions with the signature ofprocessSelf(self: TSelf)
and they do need to be part of the same options object.The text was updated successfully, but these errors were encountered: