Confused by `object` type?
I am trying to come up with a type signature for a function parameter that itself is a function with a single parameter of type any object. Like this (typescript playground):

But in the strict compiler mode I am getting this error:
Argument of type '(a: { n: number; }) => void' is not assignable to parameter of type '(a: object) => void'.
Types of parameters 'a' and 'a' are incompatible.
Type 'object' is not assignable to type '{ n: number; }'.
Property 'n' is missing in type '{}'.
What am I doing wrong?
Thanks!
typescript typescript-typings
add a comment |
I am trying to come up with a type signature for a function parameter that itself is a function with a single parameter of type any object. Like this (typescript playground):

But in the strict compiler mode I am getting this error:
Argument of type '(a: { n: number; }) => void' is not assignable to parameter of type '(a: object) => void'.
Types of parameters 'a' and 'a' are incompatible.
Type 'object' is not assignable to type '{ n: number; }'.
Property 'n' is missing in type '{}'.
What am I doing wrong?
Thanks!
typescript typescript-typings
add a comment |
I am trying to come up with a type signature for a function parameter that itself is a function with a single parameter of type any object. Like this (typescript playground):

But in the strict compiler mode I am getting this error:
Argument of type '(a: { n: number; }) => void' is not assignable to parameter of type '(a: object) => void'.
Types of parameters 'a' and 'a' are incompatible.
Type 'object' is not assignable to type '{ n: number; }'.
Property 'n' is missing in type '{}'.
What am I doing wrong?
Thanks!
typescript typescript-typings
I am trying to come up with a type signature for a function parameter that itself is a function with a single parameter of type any object. Like this (typescript playground):

But in the strict compiler mode I am getting this error:
Argument of type '(a: { n: number; }) => void' is not assignable to parameter of type '(a: object) => void'.
Types of parameters 'a' and 'a' are incompatible.
Type 'object' is not assignable to type '{ n: number; }'.
Property 'n' is missing in type '{}'.
What am I doing wrong?
Thanks!
typescript typescript-typings
typescript typescript-typings
asked Nov 23 '18 at 22:40
artemaveartemave
3,37353862
3,37353862
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
bbb expects a function that can be called with any object as argument.
You're passing aaa as argument. But aaa can only be called with objects which have a property n of type number. It can't be called with any kind of object. So it's not a valid argument for bbb.
To make an analogy, bbb is like a juice bar, which needs a fruit juicer to work, i.e. a function that can take any fruit and tranform it to juice.
If you try to create a juice bar with an apple juicer (i.e. a function that can only make juice out of apples), that won't work well.
Thanks, that makes sense! So what is the correct way to express "accept a function with a single parameter of any object"?
– artemave
Nov 24 '18 at 10:41
Not sure what you exactly mean, but I guess you wantfunction bbb(fn: (a: any) => void)
– JB Nizet
Nov 24 '18 at 10:52
1
Perhaps it will be a bit better to go generic:function bbb<T extends object>(fn: (a: T) => void)- can't test it right now, though.
– Cerberus
Nov 24 '18 at 11:10
That works, thanks!
– artemave
Nov 24 '18 at 11:36
However, if I turn it to this I am still getting the same errortype Fn = <T extends object>(a: T) => void; function ccc(fn: Fn) { }; ccc(aaa)
– artemave
Nov 24 '18 at 11:37
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453593%2fconfused-by-object-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
bbb expects a function that can be called with any object as argument.
You're passing aaa as argument. But aaa can only be called with objects which have a property n of type number. It can't be called with any kind of object. So it's not a valid argument for bbb.
To make an analogy, bbb is like a juice bar, which needs a fruit juicer to work, i.e. a function that can take any fruit and tranform it to juice.
If you try to create a juice bar with an apple juicer (i.e. a function that can only make juice out of apples), that won't work well.
Thanks, that makes sense! So what is the correct way to express "accept a function with a single parameter of any object"?
– artemave
Nov 24 '18 at 10:41
Not sure what you exactly mean, but I guess you wantfunction bbb(fn: (a: any) => void)
– JB Nizet
Nov 24 '18 at 10:52
1
Perhaps it will be a bit better to go generic:function bbb<T extends object>(fn: (a: T) => void)- can't test it right now, though.
– Cerberus
Nov 24 '18 at 11:10
That works, thanks!
– artemave
Nov 24 '18 at 11:36
However, if I turn it to this I am still getting the same errortype Fn = <T extends object>(a: T) => void; function ccc(fn: Fn) { }; ccc(aaa)
– artemave
Nov 24 '18 at 11:37
add a comment |
bbb expects a function that can be called with any object as argument.
You're passing aaa as argument. But aaa can only be called with objects which have a property n of type number. It can't be called with any kind of object. So it's not a valid argument for bbb.
To make an analogy, bbb is like a juice bar, which needs a fruit juicer to work, i.e. a function that can take any fruit and tranform it to juice.
If you try to create a juice bar with an apple juicer (i.e. a function that can only make juice out of apples), that won't work well.
Thanks, that makes sense! So what is the correct way to express "accept a function with a single parameter of any object"?
– artemave
Nov 24 '18 at 10:41
Not sure what you exactly mean, but I guess you wantfunction bbb(fn: (a: any) => void)
– JB Nizet
Nov 24 '18 at 10:52
1
Perhaps it will be a bit better to go generic:function bbb<T extends object>(fn: (a: T) => void)- can't test it right now, though.
– Cerberus
Nov 24 '18 at 11:10
That works, thanks!
– artemave
Nov 24 '18 at 11:36
However, if I turn it to this I am still getting the same errortype Fn = <T extends object>(a: T) => void; function ccc(fn: Fn) { }; ccc(aaa)
– artemave
Nov 24 '18 at 11:37
add a comment |
bbb expects a function that can be called with any object as argument.
You're passing aaa as argument. But aaa can only be called with objects which have a property n of type number. It can't be called with any kind of object. So it's not a valid argument for bbb.
To make an analogy, bbb is like a juice bar, which needs a fruit juicer to work, i.e. a function that can take any fruit and tranform it to juice.
If you try to create a juice bar with an apple juicer (i.e. a function that can only make juice out of apples), that won't work well.
bbb expects a function that can be called with any object as argument.
You're passing aaa as argument. But aaa can only be called with objects which have a property n of type number. It can't be called with any kind of object. So it's not a valid argument for bbb.
To make an analogy, bbb is like a juice bar, which needs a fruit juicer to work, i.e. a function that can take any fruit and tranform it to juice.
If you try to create a juice bar with an apple juicer (i.e. a function that can only make juice out of apples), that won't work well.
edited Nov 23 '18 at 23:06
answered Nov 23 '18 at 22:48
JB NizetJB Nizet
536k52864997
536k52864997
Thanks, that makes sense! So what is the correct way to express "accept a function with a single parameter of any object"?
– artemave
Nov 24 '18 at 10:41
Not sure what you exactly mean, but I guess you wantfunction bbb(fn: (a: any) => void)
– JB Nizet
Nov 24 '18 at 10:52
1
Perhaps it will be a bit better to go generic:function bbb<T extends object>(fn: (a: T) => void)- can't test it right now, though.
– Cerberus
Nov 24 '18 at 11:10
That works, thanks!
– artemave
Nov 24 '18 at 11:36
However, if I turn it to this I am still getting the same errortype Fn = <T extends object>(a: T) => void; function ccc(fn: Fn) { }; ccc(aaa)
– artemave
Nov 24 '18 at 11:37
add a comment |
Thanks, that makes sense! So what is the correct way to express "accept a function with a single parameter of any object"?
– artemave
Nov 24 '18 at 10:41
Not sure what you exactly mean, but I guess you wantfunction bbb(fn: (a: any) => void)
– JB Nizet
Nov 24 '18 at 10:52
1
Perhaps it will be a bit better to go generic:function bbb<T extends object>(fn: (a: T) => void)- can't test it right now, though.
– Cerberus
Nov 24 '18 at 11:10
That works, thanks!
– artemave
Nov 24 '18 at 11:36
However, if I turn it to this I am still getting the same errortype Fn = <T extends object>(a: T) => void; function ccc(fn: Fn) { }; ccc(aaa)
– artemave
Nov 24 '18 at 11:37
Thanks, that makes sense! So what is the correct way to express "accept a function with a single parameter of any object"?
– artemave
Nov 24 '18 at 10:41
Thanks, that makes sense! So what is the correct way to express "accept a function with a single parameter of any object"?
– artemave
Nov 24 '18 at 10:41
Not sure what you exactly mean, but I guess you want
function bbb(fn: (a: any) => void)– JB Nizet
Nov 24 '18 at 10:52
Not sure what you exactly mean, but I guess you want
function bbb(fn: (a: any) => void)– JB Nizet
Nov 24 '18 at 10:52
1
1
Perhaps it will be a bit better to go generic:
function bbb<T extends object>(fn: (a: T) => void) - can't test it right now, though.– Cerberus
Nov 24 '18 at 11:10
Perhaps it will be a bit better to go generic:
function bbb<T extends object>(fn: (a: T) => void) - can't test it right now, though.– Cerberus
Nov 24 '18 at 11:10
That works, thanks!
– artemave
Nov 24 '18 at 11:36
That works, thanks!
– artemave
Nov 24 '18 at 11:36
However, if I turn it to this I am still getting the same error
type Fn = <T extends object>(a: T) => void; function ccc(fn: Fn) { }; ccc(aaa) – artemave
Nov 24 '18 at 11:37
However, if I turn it to this I am still getting the same error
type Fn = <T extends object>(a: T) => void; function ccc(fn: Fn) { }; ccc(aaa) – artemave
Nov 24 '18 at 11:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453593%2fconfused-by-object-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown