Restricting possible values of an input to a derived type based on the input of a previous type( TypeScript )
In functional programming with types you often can restrict types in your application to provided values of input.
However I'm at a loss in how to do this with TypeScript with the possible values of the keys for a Map.
Consider the following:
const match = <T, V, R>(p : (x : V) => T, ... c : Array< [T, (x : V) => R] >) : (x : V) => R => {
const cases = new Map(c);
return (x : any) => cases.get(p(x))(x);
}
A more human readable format:
const match = <Row, Value, ReturnType>(p : (x : Value) => Row, ...c : Array< [Row, (x : Value) => ReturnType] >) : (x : Value) => ReturnType => {
const cases = new Map(c);
return x => cases.get(p(x))(x);
}
However there is a problem with this notation as it isn't actually doing what is intended to be done:
match(x => Math.random() > 0.5 ? "foo" : "bar", ["baz", x => x])
This should, in a sufficiently advanced Typing system be able to be an error, however in TypeScript it is not detecting that the "foo" or "bar" values are not any of the expected values and would actually cause the code to throw.
A more in depth example:
const validDouble = match( x => x * 2,
[4, x => "four"],
[6, x => "six"]
)
validDouble(4) // TS ERROR: 8 is not a valid value for Type ROW (4 | 6)
validDouble(2) // "four", no TS Error
I know I can't enforce the rest parameter to be a forced value of arrays(I could however wrap it in an enforced array and require extra characters from the developer), but I am curious if there is a way to enforce that the values in the row match the output values in the predicate function?
EDIT: Yes I know this throws on a non matching input. That logic is EASY to insert with conditionals or try catches and turning this into a matchMaybe, however this example was simplified because that has no impact or implication on the typing system available in TypeScript
typescript
|
show 3 more comments
In functional programming with types you often can restrict types in your application to provided values of input.
However I'm at a loss in how to do this with TypeScript with the possible values of the keys for a Map.
Consider the following:
const match = <T, V, R>(p : (x : V) => T, ... c : Array< [T, (x : V) => R] >) : (x : V) => R => {
const cases = new Map(c);
return (x : any) => cases.get(p(x))(x);
}
A more human readable format:
const match = <Row, Value, ReturnType>(p : (x : Value) => Row, ...c : Array< [Row, (x : Value) => ReturnType] >) : (x : Value) => ReturnType => {
const cases = new Map(c);
return x => cases.get(p(x))(x);
}
However there is a problem with this notation as it isn't actually doing what is intended to be done:
match(x => Math.random() > 0.5 ? "foo" : "bar", ["baz", x => x])
This should, in a sufficiently advanced Typing system be able to be an error, however in TypeScript it is not detecting that the "foo" or "bar" values are not any of the expected values and would actually cause the code to throw.
A more in depth example:
const validDouble = match( x => x * 2,
[4, x => "four"],
[6, x => "six"]
)
validDouble(4) // TS ERROR: 8 is not a valid value for Type ROW (4 | 6)
validDouble(2) // "four", no TS Error
I know I can't enforce the rest parameter to be a forced value of arrays(I could however wrap it in an enforced array and require extra characters from the developer), but I am curious if there is a way to enforce that the values in the row match the output values in the predicate function?
EDIT: Yes I know this throws on a non matching input. That logic is EASY to insert with conditionals or try catches and turning this into a matchMaybe, however this example was simplified because that has no impact or implication on the typing system available in TypeScript
typescript
Corrected, thanks
– Robert Mennell
Dec 31 '18 at 10:26
I didn't dive too deeply, but basically you want to restrict return values of the first parameter (which is a function) to"baz"? Am I right?
– Nurbol Alpysbayev
Dec 31 '18 at 10:29
If I have multiple key values in the set it should only be one of the values, otherwise TS should throw an error about imcompatible types
– Robert Mennell
Dec 31 '18 at 10:31
Ok so it should be a union, give me a few minutes..
– Nurbol Alpysbayev
Dec 31 '18 at 10:32
Robert can you please explain in a couple of words what this function actually should do. I see it's namedmatch, but what does it matches?
– Nurbol Alpysbayev
Dec 31 '18 at 10:36
|
show 3 more comments
In functional programming with types you often can restrict types in your application to provided values of input.
However I'm at a loss in how to do this with TypeScript with the possible values of the keys for a Map.
Consider the following:
const match = <T, V, R>(p : (x : V) => T, ... c : Array< [T, (x : V) => R] >) : (x : V) => R => {
const cases = new Map(c);
return (x : any) => cases.get(p(x))(x);
}
A more human readable format:
const match = <Row, Value, ReturnType>(p : (x : Value) => Row, ...c : Array< [Row, (x : Value) => ReturnType] >) : (x : Value) => ReturnType => {
const cases = new Map(c);
return x => cases.get(p(x))(x);
}
However there is a problem with this notation as it isn't actually doing what is intended to be done:
match(x => Math.random() > 0.5 ? "foo" : "bar", ["baz", x => x])
This should, in a sufficiently advanced Typing system be able to be an error, however in TypeScript it is not detecting that the "foo" or "bar" values are not any of the expected values and would actually cause the code to throw.
A more in depth example:
const validDouble = match( x => x * 2,
[4, x => "four"],
[6, x => "six"]
)
validDouble(4) // TS ERROR: 8 is not a valid value for Type ROW (4 | 6)
validDouble(2) // "four", no TS Error
I know I can't enforce the rest parameter to be a forced value of arrays(I could however wrap it in an enforced array and require extra characters from the developer), but I am curious if there is a way to enforce that the values in the row match the output values in the predicate function?
EDIT: Yes I know this throws on a non matching input. That logic is EASY to insert with conditionals or try catches and turning this into a matchMaybe, however this example was simplified because that has no impact or implication on the typing system available in TypeScript
typescript
In functional programming with types you often can restrict types in your application to provided values of input.
However I'm at a loss in how to do this with TypeScript with the possible values of the keys for a Map.
Consider the following:
const match = <T, V, R>(p : (x : V) => T, ... c : Array< [T, (x : V) => R] >) : (x : V) => R => {
const cases = new Map(c);
return (x : any) => cases.get(p(x))(x);
}
A more human readable format:
const match = <Row, Value, ReturnType>(p : (x : Value) => Row, ...c : Array< [Row, (x : Value) => ReturnType] >) : (x : Value) => ReturnType => {
const cases = new Map(c);
return x => cases.get(p(x))(x);
}
However there is a problem with this notation as it isn't actually doing what is intended to be done:
match(x => Math.random() > 0.5 ? "foo" : "bar", ["baz", x => x])
This should, in a sufficiently advanced Typing system be able to be an error, however in TypeScript it is not detecting that the "foo" or "bar" values are not any of the expected values and would actually cause the code to throw.
A more in depth example:
const validDouble = match( x => x * 2,
[4, x => "four"],
[6, x => "six"]
)
validDouble(4) // TS ERROR: 8 is not a valid value for Type ROW (4 | 6)
validDouble(2) // "four", no TS Error
I know I can't enforce the rest parameter to be a forced value of arrays(I could however wrap it in an enforced array and require extra characters from the developer), but I am curious if there is a way to enforce that the values in the row match the output values in the predicate function?
EDIT: Yes I know this throws on a non matching input. That logic is EASY to insert with conditionals or try catches and turning this into a matchMaybe, however this example was simplified because that has no impact or implication on the typing system available in TypeScript
typescript
typescript
edited Jan 1 at 23:08
Robert Mennell
asked Dec 31 '18 at 7:59
Robert MennellRobert Mennell
963517
963517
Corrected, thanks
– Robert Mennell
Dec 31 '18 at 10:26
I didn't dive too deeply, but basically you want to restrict return values of the first parameter (which is a function) to"baz"? Am I right?
– Nurbol Alpysbayev
Dec 31 '18 at 10:29
If I have multiple key values in the set it should only be one of the values, otherwise TS should throw an error about imcompatible types
– Robert Mennell
Dec 31 '18 at 10:31
Ok so it should be a union, give me a few minutes..
– Nurbol Alpysbayev
Dec 31 '18 at 10:32
Robert can you please explain in a couple of words what this function actually should do. I see it's namedmatch, but what does it matches?
– Nurbol Alpysbayev
Dec 31 '18 at 10:36
|
show 3 more comments
Corrected, thanks
– Robert Mennell
Dec 31 '18 at 10:26
I didn't dive too deeply, but basically you want to restrict return values of the first parameter (which is a function) to"baz"? Am I right?
– Nurbol Alpysbayev
Dec 31 '18 at 10:29
If I have multiple key values in the set it should only be one of the values, otherwise TS should throw an error about imcompatible types
– Robert Mennell
Dec 31 '18 at 10:31
Ok so it should be a union, give me a few minutes..
– Nurbol Alpysbayev
Dec 31 '18 at 10:32
Robert can you please explain in a couple of words what this function actually should do. I see it's namedmatch, but what does it matches?
– Nurbol Alpysbayev
Dec 31 '18 at 10:36
Corrected, thanks
– Robert Mennell
Dec 31 '18 at 10:26
Corrected, thanks
– Robert Mennell
Dec 31 '18 at 10:26
I didn't dive too deeply, but basically you want to restrict return values of the first parameter (which is a function) to
"baz"? Am I right?– Nurbol Alpysbayev
Dec 31 '18 at 10:29
I didn't dive too deeply, but basically you want to restrict return values of the first parameter (which is a function) to
"baz"? Am I right?– Nurbol Alpysbayev
Dec 31 '18 at 10:29
If I have multiple key values in the set it should only be one of the values, otherwise TS should throw an error about imcompatible types
– Robert Mennell
Dec 31 '18 at 10:31
If I have multiple key values in the set it should only be one of the values, otherwise TS should throw an error about imcompatible types
– Robert Mennell
Dec 31 '18 at 10:31
Ok so it should be a union, give me a few minutes..
– Nurbol Alpysbayev
Dec 31 '18 at 10:32
Ok so it should be a union, give me a few minutes..
– Nurbol Alpysbayev
Dec 31 '18 at 10:32
Robert can you please explain in a couple of words what this function actually should do. I see it's named
match, but what does it matches?– Nurbol Alpysbayev
Dec 31 '18 at 10:36
Robert can you please explain in a couple of words what this function actually should do. I see it's named
match, but what does it matches?– Nurbol Alpysbayev
Dec 31 '18 at 10:36
|
show 3 more comments
1 Answer
1
active
oldest
votes
Ok here is the fixed function (may need some tidy-up and probably improvements).
const match = <
Row extends string | number,
Value,
ReturnType,
V extends (x: Value) => ReturnType,
C extends [...[Row, V]],
>
(
predicate: (x: Value) => Row,
...c: C & [...[Row, V]]
): (x: Value) => ReturnType => {
const cases = new Map<Row, V>(c);
return x =>
// put a runtime check below instead of asserting
// non-undefined value with `!`
cases.get(predicate(x))!(x)
}
match(x => 'baz', ["baz", (x: any) => x]) // ok
match(x => 'qwe', ["baz", (x: any) => x]) // err
match(x => 'qwe', ["baz", (x: any) => x], ["qwe", (x: any) => x]) //ok
Playground
The first problem was that rest parameter should be a tuple. The second problem was to place type parameters correctly so that everything infers correctly.
P.S. Complex types in typescript are hard. I feel lack of the documentation on type parameters, hence I always feel inconfidence about them (I think others do as well).
I am not asking for the function to be fixed. This is a question about types and type enforcement on derived types
– Robert Mennell
Dec 31 '18 at 19:48
@RobertMennell Your original question was literally this: In functional programming with types you often can restrict types in your application to provided values of input. However I'm at a loss in how to do this with TypeScript.. To me it means that you want to know how to do this with TypeScript. And I showed it. I also provided a note about the lack of documentation on how type params work (even in spec). Your comment actually doesnt make it more clear what you really want. And that level of gratefulness doesnt help
– Nurbol Alpysbayev
Jan 1 at 4:56
if you reread the edits you missed how that's background information. I have, since the beginning, been looking how to limit output values of the predict based on the key value of the map
– Robert Mennell
Jan 1 at 5:29
@RobertMennell And don't last 3 lines of the code block in my answer show that output values of the predicate are limited based on the keys of the map? I've also just added a link to the playground.
– Nurbol Alpysbayev
Jan 1 at 5:37
If you ran the example o provide it does the same thing. I want the TypeScript compiler and inspector to prevent it, not a runtime error
– Robert Mennell
Jan 1 at 5:40
|
show 11 more comments
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%2f53985022%2frestricting-possible-values-of-an-input-to-a-derived-type-based-on-the-input-of%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
Ok here is the fixed function (may need some tidy-up and probably improvements).
const match = <
Row extends string | number,
Value,
ReturnType,
V extends (x: Value) => ReturnType,
C extends [...[Row, V]],
>
(
predicate: (x: Value) => Row,
...c: C & [...[Row, V]]
): (x: Value) => ReturnType => {
const cases = new Map<Row, V>(c);
return x =>
// put a runtime check below instead of asserting
// non-undefined value with `!`
cases.get(predicate(x))!(x)
}
match(x => 'baz', ["baz", (x: any) => x]) // ok
match(x => 'qwe', ["baz", (x: any) => x]) // err
match(x => 'qwe', ["baz", (x: any) => x], ["qwe", (x: any) => x]) //ok
Playground
The first problem was that rest parameter should be a tuple. The second problem was to place type parameters correctly so that everything infers correctly.
P.S. Complex types in typescript are hard. I feel lack of the documentation on type parameters, hence I always feel inconfidence about them (I think others do as well).
I am not asking for the function to be fixed. This is a question about types and type enforcement on derived types
– Robert Mennell
Dec 31 '18 at 19:48
@RobertMennell Your original question was literally this: In functional programming with types you often can restrict types in your application to provided values of input. However I'm at a loss in how to do this with TypeScript.. To me it means that you want to know how to do this with TypeScript. And I showed it. I also provided a note about the lack of documentation on how type params work (even in spec). Your comment actually doesnt make it more clear what you really want. And that level of gratefulness doesnt help
– Nurbol Alpysbayev
Jan 1 at 4:56
if you reread the edits you missed how that's background information. I have, since the beginning, been looking how to limit output values of the predict based on the key value of the map
– Robert Mennell
Jan 1 at 5:29
@RobertMennell And don't last 3 lines of the code block in my answer show that output values of the predicate are limited based on the keys of the map? I've also just added a link to the playground.
– Nurbol Alpysbayev
Jan 1 at 5:37
If you ran the example o provide it does the same thing. I want the TypeScript compiler and inspector to prevent it, not a runtime error
– Robert Mennell
Jan 1 at 5:40
|
show 11 more comments
Ok here is the fixed function (may need some tidy-up and probably improvements).
const match = <
Row extends string | number,
Value,
ReturnType,
V extends (x: Value) => ReturnType,
C extends [...[Row, V]],
>
(
predicate: (x: Value) => Row,
...c: C & [...[Row, V]]
): (x: Value) => ReturnType => {
const cases = new Map<Row, V>(c);
return x =>
// put a runtime check below instead of asserting
// non-undefined value with `!`
cases.get(predicate(x))!(x)
}
match(x => 'baz', ["baz", (x: any) => x]) // ok
match(x => 'qwe', ["baz", (x: any) => x]) // err
match(x => 'qwe', ["baz", (x: any) => x], ["qwe", (x: any) => x]) //ok
Playground
The first problem was that rest parameter should be a tuple. The second problem was to place type parameters correctly so that everything infers correctly.
P.S. Complex types in typescript are hard. I feel lack of the documentation on type parameters, hence I always feel inconfidence about them (I think others do as well).
I am not asking for the function to be fixed. This is a question about types and type enforcement on derived types
– Robert Mennell
Dec 31 '18 at 19:48
@RobertMennell Your original question was literally this: In functional programming with types you often can restrict types in your application to provided values of input. However I'm at a loss in how to do this with TypeScript.. To me it means that you want to know how to do this with TypeScript. And I showed it. I also provided a note about the lack of documentation on how type params work (even in spec). Your comment actually doesnt make it more clear what you really want. And that level of gratefulness doesnt help
– Nurbol Alpysbayev
Jan 1 at 4:56
if you reread the edits you missed how that's background information. I have, since the beginning, been looking how to limit output values of the predict based on the key value of the map
– Robert Mennell
Jan 1 at 5:29
@RobertMennell And don't last 3 lines of the code block in my answer show that output values of the predicate are limited based on the keys of the map? I've also just added a link to the playground.
– Nurbol Alpysbayev
Jan 1 at 5:37
If you ran the example o provide it does the same thing. I want the TypeScript compiler and inspector to prevent it, not a runtime error
– Robert Mennell
Jan 1 at 5:40
|
show 11 more comments
Ok here is the fixed function (may need some tidy-up and probably improvements).
const match = <
Row extends string | number,
Value,
ReturnType,
V extends (x: Value) => ReturnType,
C extends [...[Row, V]],
>
(
predicate: (x: Value) => Row,
...c: C & [...[Row, V]]
): (x: Value) => ReturnType => {
const cases = new Map<Row, V>(c);
return x =>
// put a runtime check below instead of asserting
// non-undefined value with `!`
cases.get(predicate(x))!(x)
}
match(x => 'baz', ["baz", (x: any) => x]) // ok
match(x => 'qwe', ["baz", (x: any) => x]) // err
match(x => 'qwe', ["baz", (x: any) => x], ["qwe", (x: any) => x]) //ok
Playground
The first problem was that rest parameter should be a tuple. The second problem was to place type parameters correctly so that everything infers correctly.
P.S. Complex types in typescript are hard. I feel lack of the documentation on type parameters, hence I always feel inconfidence about them (I think others do as well).
Ok here is the fixed function (may need some tidy-up and probably improvements).
const match = <
Row extends string | number,
Value,
ReturnType,
V extends (x: Value) => ReturnType,
C extends [...[Row, V]],
>
(
predicate: (x: Value) => Row,
...c: C & [...[Row, V]]
): (x: Value) => ReturnType => {
const cases = new Map<Row, V>(c);
return x =>
// put a runtime check below instead of asserting
// non-undefined value with `!`
cases.get(predicate(x))!(x)
}
match(x => 'baz', ["baz", (x: any) => x]) // ok
match(x => 'qwe', ["baz", (x: any) => x]) // err
match(x => 'qwe', ["baz", (x: any) => x], ["qwe", (x: any) => x]) //ok
Playground
The first problem was that rest parameter should be a tuple. The second problem was to place type parameters correctly so that everything infers correctly.
P.S. Complex types in typescript are hard. I feel lack of the documentation on type parameters, hence I always feel inconfidence about them (I think others do as well).
edited Jan 2 at 9:48
answered Dec 31 '18 at 10:50
Nurbol AlpysbayevNurbol Alpysbayev
4,1011327
4,1011327
I am not asking for the function to be fixed. This is a question about types and type enforcement on derived types
– Robert Mennell
Dec 31 '18 at 19:48
@RobertMennell Your original question was literally this: In functional programming with types you often can restrict types in your application to provided values of input. However I'm at a loss in how to do this with TypeScript.. To me it means that you want to know how to do this with TypeScript. And I showed it. I also provided a note about the lack of documentation on how type params work (even in spec). Your comment actually doesnt make it more clear what you really want. And that level of gratefulness doesnt help
– Nurbol Alpysbayev
Jan 1 at 4:56
if you reread the edits you missed how that's background information. I have, since the beginning, been looking how to limit output values of the predict based on the key value of the map
– Robert Mennell
Jan 1 at 5:29
@RobertMennell And don't last 3 lines of the code block in my answer show that output values of the predicate are limited based on the keys of the map? I've also just added a link to the playground.
– Nurbol Alpysbayev
Jan 1 at 5:37
If you ran the example o provide it does the same thing. I want the TypeScript compiler and inspector to prevent it, not a runtime error
– Robert Mennell
Jan 1 at 5:40
|
show 11 more comments
I am not asking for the function to be fixed. This is a question about types and type enforcement on derived types
– Robert Mennell
Dec 31 '18 at 19:48
@RobertMennell Your original question was literally this: In functional programming with types you often can restrict types in your application to provided values of input. However I'm at a loss in how to do this with TypeScript.. To me it means that you want to know how to do this with TypeScript. And I showed it. I also provided a note about the lack of documentation on how type params work (even in spec). Your comment actually doesnt make it more clear what you really want. And that level of gratefulness doesnt help
– Nurbol Alpysbayev
Jan 1 at 4:56
if you reread the edits you missed how that's background information. I have, since the beginning, been looking how to limit output values of the predict based on the key value of the map
– Robert Mennell
Jan 1 at 5:29
@RobertMennell And don't last 3 lines of the code block in my answer show that output values of the predicate are limited based on the keys of the map? I've also just added a link to the playground.
– Nurbol Alpysbayev
Jan 1 at 5:37
If you ran the example o provide it does the same thing. I want the TypeScript compiler and inspector to prevent it, not a runtime error
– Robert Mennell
Jan 1 at 5:40
I am not asking for the function to be fixed. This is a question about types and type enforcement on derived types
– Robert Mennell
Dec 31 '18 at 19:48
I am not asking for the function to be fixed. This is a question about types and type enforcement on derived types
– Robert Mennell
Dec 31 '18 at 19:48
@RobertMennell Your original question was literally this: In functional programming with types you often can restrict types in your application to provided values of input. However I'm at a loss in how to do this with TypeScript.. To me it means that you want to know how to do this with TypeScript. And I showed it. I also provided a note about the lack of documentation on how type params work (even in spec). Your comment actually doesnt make it more clear what you really want. And that level of gratefulness doesnt help
– Nurbol Alpysbayev
Jan 1 at 4:56
@RobertMennell Your original question was literally this: In functional programming with types you often can restrict types in your application to provided values of input. However I'm at a loss in how to do this with TypeScript.. To me it means that you want to know how to do this with TypeScript. And I showed it. I also provided a note about the lack of documentation on how type params work (even in spec). Your comment actually doesnt make it more clear what you really want. And that level of gratefulness doesnt help
– Nurbol Alpysbayev
Jan 1 at 4:56
if you reread the edits you missed how that's background information. I have, since the beginning, been looking how to limit output values of the predict based on the key value of the map
– Robert Mennell
Jan 1 at 5:29
if you reread the edits you missed how that's background information. I have, since the beginning, been looking how to limit output values of the predict based on the key value of the map
– Robert Mennell
Jan 1 at 5:29
@RobertMennell And don't last 3 lines of the code block in my answer show that output values of the predicate are limited based on the keys of the map? I've also just added a link to the playground.
– Nurbol Alpysbayev
Jan 1 at 5:37
@RobertMennell And don't last 3 lines of the code block in my answer show that output values of the predicate are limited based on the keys of the map? I've also just added a link to the playground.
– Nurbol Alpysbayev
Jan 1 at 5:37
If you ran the example o provide it does the same thing. I want the TypeScript compiler and inspector to prevent it, not a runtime error
– Robert Mennell
Jan 1 at 5:40
If you ran the example o provide it does the same thing. I want the TypeScript compiler and inspector to prevent it, not a runtime error
– Robert Mennell
Jan 1 at 5:40
|
show 11 more comments
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%2f53985022%2frestricting-possible-values-of-an-input-to-a-derived-type-based-on-the-input-of%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
Corrected, thanks
– Robert Mennell
Dec 31 '18 at 10:26
I didn't dive too deeply, but basically you want to restrict return values of the first parameter (which is a function) to
"baz"? Am I right?– Nurbol Alpysbayev
Dec 31 '18 at 10:29
If I have multiple key values in the set it should only be one of the values, otherwise TS should throw an error about imcompatible types
– Robert Mennell
Dec 31 '18 at 10:31
Ok so it should be a union, give me a few minutes..
– Nurbol Alpysbayev
Dec 31 '18 at 10:32
Robert can you please explain in a couple of words what this function actually should do. I see it's named
match, but what does it matches?– Nurbol Alpysbayev
Dec 31 '18 at 10:36