How to extend the Error interface with generic strings in Flow?
When I try to extend the Error interface in Flow to make the name property mandatory, Flow doesn't recognise my generic types as strings despite explicitly describing them as strings.
When I write that:
interface CustomError<A: string, B: string> extends Error {
message: A;
name: B;
}
I get these 2 (same) errors:
- Cannot extend
Error1 withCustomErrorbecauseA[2] is incompatible with string [3] in propertymessage. - Cannot extend
Error1 withCustomErrorbecauseB[2] is incompatible with string [3] in propertyname.
What's strange is that it tells me because A [2] is incompatible when A should be described as a string right away...
flowtype
add a comment |
When I try to extend the Error interface in Flow to make the name property mandatory, Flow doesn't recognise my generic types as strings despite explicitly describing them as strings.
When I write that:
interface CustomError<A: string, B: string> extends Error {
message: A;
name: B;
}
I get these 2 (same) errors:
- Cannot extend
Error1 withCustomErrorbecauseA[2] is incompatible with string [3] in propertymessage. - Cannot extend
Error1 withCustomErrorbecauseB[2] is incompatible with string [3] in propertyname.
What's strange is that it tells me because A [2] is incompatible when A should be described as a string right away...
flowtype
Why do you need to changeErrorbasic members type:messageandname? What you plan to put there beside the string?
– Alex
Nov 22 '18 at 8:34
1
@Alex I actually tried to isolate my issue. Not only I added other properties in my original code but the goal here is to make thename, which is natively not a mandatory property, a mandatory one. As for why I need it to be generic: this allows autocompletion to "show" the related strings since there is an error dictionary in the form of a string map object linked to these generic types.
– Edouard Hienrichs
Nov 22 '18 at 8:43
add a comment |
When I try to extend the Error interface in Flow to make the name property mandatory, Flow doesn't recognise my generic types as strings despite explicitly describing them as strings.
When I write that:
interface CustomError<A: string, B: string> extends Error {
message: A;
name: B;
}
I get these 2 (same) errors:
- Cannot extend
Error1 withCustomErrorbecauseA[2] is incompatible with string [3] in propertymessage. - Cannot extend
Error1 withCustomErrorbecauseB[2] is incompatible with string [3] in propertyname.
What's strange is that it tells me because A [2] is incompatible when A should be described as a string right away...
flowtype
When I try to extend the Error interface in Flow to make the name property mandatory, Flow doesn't recognise my generic types as strings despite explicitly describing them as strings.
When I write that:
interface CustomError<A: string, B: string> extends Error {
message: A;
name: B;
}
I get these 2 (same) errors:
- Cannot extend
Error1 withCustomErrorbecauseA[2] is incompatible with string [3] in propertymessage. - Cannot extend
Error1 withCustomErrorbecauseB[2] is incompatible with string [3] in propertyname.
What's strange is that it tells me because A [2] is incompatible when A should be described as a string right away...
flowtype
flowtype
edited Nov 21 '18 at 6:51
Edouard Hienrichs
asked Nov 19 '18 at 1:53
Edouard HienrichsEdouard Hienrichs
38211
38211
Why do you need to changeErrorbasic members type:messageandname? What you plan to put there beside the string?
– Alex
Nov 22 '18 at 8:34
1
@Alex I actually tried to isolate my issue. Not only I added other properties in my original code but the goal here is to make thename, which is natively not a mandatory property, a mandatory one. As for why I need it to be generic: this allows autocompletion to "show" the related strings since there is an error dictionary in the form of a string map object linked to these generic types.
– Edouard Hienrichs
Nov 22 '18 at 8:43
add a comment |
Why do you need to changeErrorbasic members type:messageandname? What you plan to put there beside the string?
– Alex
Nov 22 '18 at 8:34
1
@Alex I actually tried to isolate my issue. Not only I added other properties in my original code but the goal here is to make thename, which is natively not a mandatory property, a mandatory one. As for why I need it to be generic: this allows autocompletion to "show" the related strings since there is an error dictionary in the form of a string map object linked to these generic types.
– Edouard Hienrichs
Nov 22 '18 at 8:43
Why do you need to change
Error basic members type: message and name? What you plan to put there beside the string?– Alex
Nov 22 '18 at 8:34
Why do you need to change
Error basic members type: message and name? What you plan to put there beside the string?– Alex
Nov 22 '18 at 8:34
1
1
@Alex I actually tried to isolate my issue. Not only I added other properties in my original code but the goal here is to make the
name, which is natively not a mandatory property, a mandatory one. As for why I need it to be generic: this allows autocompletion to "show" the related strings since there is an error dictionary in the form of a string map object linked to these generic types.– Edouard Hienrichs
Nov 22 '18 at 8:43
@Alex I actually tried to isolate my issue. Not only I added other properties in my original code but the goal here is to make the
name, which is natively not a mandatory property, a mandatory one. As for why I need it to be generic: this allows autocompletion to "show" the related strings since there is an error dictionary in the form of a string map object linked to these generic types.– Edouard Hienrichs
Nov 22 '18 at 8:43
add a comment |
1 Answer
1
active
oldest
votes
It's also not clear for me why it doesn't work, however something like this seems to be ok:
class CustomError<A: string, B: string> extends Error {
constructor(name: A, message: B) {
super(name);
this.name = name;
this.message = message;
}
}
type A = 'A' | 'A1';
type B = 'B' | 'B1';
class SpecificError extends CustomError<A, B> {}
//throw new SpecificError('a', 'c'); //error, wrong argument
throw new SpecificError('A', 'B'); //ok
Also, would note that js allows to throw any expression, not only Error children, so you might not need to extend Error class.
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%2f53367318%2fhow-to-extend-the-error-interface-with-generic-strings-in-flow%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
It's also not clear for me why it doesn't work, however something like this seems to be ok:
class CustomError<A: string, B: string> extends Error {
constructor(name: A, message: B) {
super(name);
this.name = name;
this.message = message;
}
}
type A = 'A' | 'A1';
type B = 'B' | 'B1';
class SpecificError extends CustomError<A, B> {}
//throw new SpecificError('a', 'c'); //error, wrong argument
throw new SpecificError('A', 'B'); //ok
Also, would note that js allows to throw any expression, not only Error children, so you might not need to extend Error class.
add a comment |
It's also not clear for me why it doesn't work, however something like this seems to be ok:
class CustomError<A: string, B: string> extends Error {
constructor(name: A, message: B) {
super(name);
this.name = name;
this.message = message;
}
}
type A = 'A' | 'A1';
type B = 'B' | 'B1';
class SpecificError extends CustomError<A, B> {}
//throw new SpecificError('a', 'c'); //error, wrong argument
throw new SpecificError('A', 'B'); //ok
Also, would note that js allows to throw any expression, not only Error children, so you might not need to extend Error class.
add a comment |
It's also not clear for me why it doesn't work, however something like this seems to be ok:
class CustomError<A: string, B: string> extends Error {
constructor(name: A, message: B) {
super(name);
this.name = name;
this.message = message;
}
}
type A = 'A' | 'A1';
type B = 'B' | 'B1';
class SpecificError extends CustomError<A, B> {}
//throw new SpecificError('a', 'c'); //error, wrong argument
throw new SpecificError('A', 'B'); //ok
Also, would note that js allows to throw any expression, not only Error children, so you might not need to extend Error class.
It's also not clear for me why it doesn't work, however something like this seems to be ok:
class CustomError<A: string, B: string> extends Error {
constructor(name: A, message: B) {
super(name);
this.name = name;
this.message = message;
}
}
type A = 'A' | 'A1';
type B = 'B' | 'B1';
class SpecificError extends CustomError<A, B> {}
//throw new SpecificError('a', 'c'); //error, wrong argument
throw new SpecificError('A', 'B'); //ok
Also, would note that js allows to throw any expression, not only Error children, so you might not need to extend Error class.
answered Nov 23 '18 at 1:47
AlexAlex
3,335621
3,335621
add a comment |
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%2f53367318%2fhow-to-extend-the-error-interface-with-generic-strings-in-flow%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

Why do you need to change
Errorbasic members type:messageandname? What you plan to put there beside the string?– Alex
Nov 22 '18 at 8:34
1
@Alex I actually tried to isolate my issue. Not only I added other properties in my original code but the goal here is to make the
name, which is natively not a mandatory property, a mandatory one. As for why I need it to be generic: this allows autocompletion to "show" the related strings since there is an error dictionary in the form of a string map object linked to these generic types.– Edouard Hienrichs
Nov 22 '18 at 8:43