Is it possible to infer a generic class type from a non-constructor method parameter in Typescript?
A- Constructor Method Case
When I want to infer the generic class type from a constructor method parameter:
interface ObjectAsMap { [key: string]: boolean }
interface MyClass<T extends ObjectAsMap> {
data: T
constructor(data: T): MyClass<T>
}
class MyClass<T extends ObjectAsMap> {
constructor(data: T) {
this.data = data
}
}
const a = new MyClass('Wrong parameter type')
const b = new MyClass({
first: true,
second: false,
})
console.log(b.data.first)
console.log(b.data.wrongProperty)
As expected, I get 2 errors:
new MyClass('Wrong parameter type')triggersArgument of type '"Wrong parameter type"' is not assignable to parameter of type 'ObjectAsMap'.
b.data.wrongPropertytriggersProperty 'wrongProperty' does not exist on type '{ first: true; second: false; }'.
B- Non-Constructor Method Case
Now, if I want to trigger exactly the same expected behavior from a non-constructor method:
interface ObjectAsMap { [key: string]: boolean }
interface MyClass<T extends ObjectAsMap> {
data: T
declare(data: T): MyClass<T>
}
class MyClass<T extends ObjectAsMap> {
public data: T
public declare(data: T) {
this.data = data
return this
}
}
const myClassInstance = new MyClass()
const a = myClassInstance.declare('Wrong parameter type')
const b = myClassInstance.declare({
first: true,
second: false,
})
console.log(b.data.first)
console.log(b.data.wrongProperty)
I only get the first error:
myClassInstance.declare('Wrong parameter type')triggersArgument of type '"Wrong parameter type"' is not assignable to parameter of type 'ObjectAsMap'..
The b.data.wrongProperty should also trigger an error since this property does not exist within b#data. When I hover the mouse above b.data, it tells me (property) MyClass<ObjectAsMap>.data: ObjectAsMap instead of (property) MyClass<{ first: true; second: false; }>.data: { first: true; second: false; }.
Question
Is there a way to infer the parameter type in Case B like I do it in Case A ?
typescript
add a comment |
A- Constructor Method Case
When I want to infer the generic class type from a constructor method parameter:
interface ObjectAsMap { [key: string]: boolean }
interface MyClass<T extends ObjectAsMap> {
data: T
constructor(data: T): MyClass<T>
}
class MyClass<T extends ObjectAsMap> {
constructor(data: T) {
this.data = data
}
}
const a = new MyClass('Wrong parameter type')
const b = new MyClass({
first: true,
second: false,
})
console.log(b.data.first)
console.log(b.data.wrongProperty)
As expected, I get 2 errors:
new MyClass('Wrong parameter type')triggersArgument of type '"Wrong parameter type"' is not assignable to parameter of type 'ObjectAsMap'.
b.data.wrongPropertytriggersProperty 'wrongProperty' does not exist on type '{ first: true; second: false; }'.
B- Non-Constructor Method Case
Now, if I want to trigger exactly the same expected behavior from a non-constructor method:
interface ObjectAsMap { [key: string]: boolean }
interface MyClass<T extends ObjectAsMap> {
data: T
declare(data: T): MyClass<T>
}
class MyClass<T extends ObjectAsMap> {
public data: T
public declare(data: T) {
this.data = data
return this
}
}
const myClassInstance = new MyClass()
const a = myClassInstance.declare('Wrong parameter type')
const b = myClassInstance.declare({
first: true,
second: false,
})
console.log(b.data.first)
console.log(b.data.wrongProperty)
I only get the first error:
myClassInstance.declare('Wrong parameter type')triggersArgument of type '"Wrong parameter type"' is not assignable to parameter of type 'ObjectAsMap'..
The b.data.wrongProperty should also trigger an error since this property does not exist within b#data. When I hover the mouse above b.data, it tells me (property) MyClass<ObjectAsMap>.data: ObjectAsMap instead of (property) MyClass<{ first: true; second: false; }>.data: { first: true; second: false; }.
Question
Is there a way to infer the parameter type in Case B like I do it in Case A ?
typescript
add a comment |
A- Constructor Method Case
When I want to infer the generic class type from a constructor method parameter:
interface ObjectAsMap { [key: string]: boolean }
interface MyClass<T extends ObjectAsMap> {
data: T
constructor(data: T): MyClass<T>
}
class MyClass<T extends ObjectAsMap> {
constructor(data: T) {
this.data = data
}
}
const a = new MyClass('Wrong parameter type')
const b = new MyClass({
first: true,
second: false,
})
console.log(b.data.first)
console.log(b.data.wrongProperty)
As expected, I get 2 errors:
new MyClass('Wrong parameter type')triggersArgument of type '"Wrong parameter type"' is not assignable to parameter of type 'ObjectAsMap'.
b.data.wrongPropertytriggersProperty 'wrongProperty' does not exist on type '{ first: true; second: false; }'.
B- Non-Constructor Method Case
Now, if I want to trigger exactly the same expected behavior from a non-constructor method:
interface ObjectAsMap { [key: string]: boolean }
interface MyClass<T extends ObjectAsMap> {
data: T
declare(data: T): MyClass<T>
}
class MyClass<T extends ObjectAsMap> {
public data: T
public declare(data: T) {
this.data = data
return this
}
}
const myClassInstance = new MyClass()
const a = myClassInstance.declare('Wrong parameter type')
const b = myClassInstance.declare({
first: true,
second: false,
})
console.log(b.data.first)
console.log(b.data.wrongProperty)
I only get the first error:
myClassInstance.declare('Wrong parameter type')triggersArgument of type '"Wrong parameter type"' is not assignable to parameter of type 'ObjectAsMap'..
The b.data.wrongProperty should also trigger an error since this property does not exist within b#data. When I hover the mouse above b.data, it tells me (property) MyClass<ObjectAsMap>.data: ObjectAsMap instead of (property) MyClass<{ first: true; second: false; }>.data: { first: true; second: false; }.
Question
Is there a way to infer the parameter type in Case B like I do it in Case A ?
typescript
A- Constructor Method Case
When I want to infer the generic class type from a constructor method parameter:
interface ObjectAsMap { [key: string]: boolean }
interface MyClass<T extends ObjectAsMap> {
data: T
constructor(data: T): MyClass<T>
}
class MyClass<T extends ObjectAsMap> {
constructor(data: T) {
this.data = data
}
}
const a = new MyClass('Wrong parameter type')
const b = new MyClass({
first: true,
second: false,
})
console.log(b.data.first)
console.log(b.data.wrongProperty)
As expected, I get 2 errors:
new MyClass('Wrong parameter type')triggersArgument of type '"Wrong parameter type"' is not assignable to parameter of type 'ObjectAsMap'.
b.data.wrongPropertytriggersProperty 'wrongProperty' does not exist on type '{ first: true; second: false; }'.
B- Non-Constructor Method Case
Now, if I want to trigger exactly the same expected behavior from a non-constructor method:
interface ObjectAsMap { [key: string]: boolean }
interface MyClass<T extends ObjectAsMap> {
data: T
declare(data: T): MyClass<T>
}
class MyClass<T extends ObjectAsMap> {
public data: T
public declare(data: T) {
this.data = data
return this
}
}
const myClassInstance = new MyClass()
const a = myClassInstance.declare('Wrong parameter type')
const b = myClassInstance.declare({
first: true,
second: false,
})
console.log(b.data.first)
console.log(b.data.wrongProperty)
I only get the first error:
myClassInstance.declare('Wrong parameter type')triggersArgument of type '"Wrong parameter type"' is not assignable to parameter of type 'ObjectAsMap'..
The b.data.wrongProperty should also trigger an error since this property does not exist within b#data. When I hover the mouse above b.data, it tells me (property) MyClass<ObjectAsMap>.data: ObjectAsMap instead of (property) MyClass<{ first: true; second: false; }>.data: { first: true; second: false; }.
Question
Is there a way to infer the parameter type in Case B like I do it in Case A ?
typescript
typescript
asked Nov 17 '18 at 14:47
Edouard HienrichsEdouard Hienrichs
38211
38211
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You just need to add an extra type parameter to capture the actual type of data in the call
interface ObjectAsMap { [key: string]: boolean }
interface MyClass<T extends ObjectAsMap> {
data: T
declar<U extends T>(data: U): MyClass<U>
}
class MyClass<T extends ObjectAsMap> {
public data: T
public declare<U extends T>(data: U): MyClass<U> {
this.data = data
return this as any
}
}
const myClassInstance = new MyClass()
const a = myClassInstance.declare('Wrong parameter type')
const b = myClassInstance.declare({
first: true,
second: false,
})
console.log(b.data.first)
console.log(b.data.wrongProperty)
It still doesn't catch the parameter type from what I just tested. You can double-check on the playground (the code sharing link is too long to insert it in a comment).
– Edouard Hienrichs
Nov 17 '18 at 15:17
@EdouardHienrichs not usre what you mean .. I get an error onb.data.wrongProperty. It will not affect the type of the originalmyClassInstance
– Titian Cernicova-Dragomir
Nov 17 '18 at 16:39
Sorry @titian-cernicova-dragomir I indeed forgot the second return declaration asMyClass<U>. It works, thank you very much !
– Edouard Hienrichs
Nov 19 '18 at 2:02
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%2f53352289%2fis-it-possible-to-infer-a-generic-class-type-from-a-non-constructor-method-param%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
You just need to add an extra type parameter to capture the actual type of data in the call
interface ObjectAsMap { [key: string]: boolean }
interface MyClass<T extends ObjectAsMap> {
data: T
declar<U extends T>(data: U): MyClass<U>
}
class MyClass<T extends ObjectAsMap> {
public data: T
public declare<U extends T>(data: U): MyClass<U> {
this.data = data
return this as any
}
}
const myClassInstance = new MyClass()
const a = myClassInstance.declare('Wrong parameter type')
const b = myClassInstance.declare({
first: true,
second: false,
})
console.log(b.data.first)
console.log(b.data.wrongProperty)
It still doesn't catch the parameter type from what I just tested. You can double-check on the playground (the code sharing link is too long to insert it in a comment).
– Edouard Hienrichs
Nov 17 '18 at 15:17
@EdouardHienrichs not usre what you mean .. I get an error onb.data.wrongProperty. It will not affect the type of the originalmyClassInstance
– Titian Cernicova-Dragomir
Nov 17 '18 at 16:39
Sorry @titian-cernicova-dragomir I indeed forgot the second return declaration asMyClass<U>. It works, thank you very much !
– Edouard Hienrichs
Nov 19 '18 at 2:02
add a comment |
You just need to add an extra type parameter to capture the actual type of data in the call
interface ObjectAsMap { [key: string]: boolean }
interface MyClass<T extends ObjectAsMap> {
data: T
declar<U extends T>(data: U): MyClass<U>
}
class MyClass<T extends ObjectAsMap> {
public data: T
public declare<U extends T>(data: U): MyClass<U> {
this.data = data
return this as any
}
}
const myClassInstance = new MyClass()
const a = myClassInstance.declare('Wrong parameter type')
const b = myClassInstance.declare({
first: true,
second: false,
})
console.log(b.data.first)
console.log(b.data.wrongProperty)
It still doesn't catch the parameter type from what I just tested. You can double-check on the playground (the code sharing link is too long to insert it in a comment).
– Edouard Hienrichs
Nov 17 '18 at 15:17
@EdouardHienrichs not usre what you mean .. I get an error onb.data.wrongProperty. It will not affect the type of the originalmyClassInstance
– Titian Cernicova-Dragomir
Nov 17 '18 at 16:39
Sorry @titian-cernicova-dragomir I indeed forgot the second return declaration asMyClass<U>. It works, thank you very much !
– Edouard Hienrichs
Nov 19 '18 at 2:02
add a comment |
You just need to add an extra type parameter to capture the actual type of data in the call
interface ObjectAsMap { [key: string]: boolean }
interface MyClass<T extends ObjectAsMap> {
data: T
declar<U extends T>(data: U): MyClass<U>
}
class MyClass<T extends ObjectAsMap> {
public data: T
public declare<U extends T>(data: U): MyClass<U> {
this.data = data
return this as any
}
}
const myClassInstance = new MyClass()
const a = myClassInstance.declare('Wrong parameter type')
const b = myClassInstance.declare({
first: true,
second: false,
})
console.log(b.data.first)
console.log(b.data.wrongProperty)
You just need to add an extra type parameter to capture the actual type of data in the call
interface ObjectAsMap { [key: string]: boolean }
interface MyClass<T extends ObjectAsMap> {
data: T
declar<U extends T>(data: U): MyClass<U>
}
class MyClass<T extends ObjectAsMap> {
public data: T
public declare<U extends T>(data: U): MyClass<U> {
this.data = data
return this as any
}
}
const myClassInstance = new MyClass()
const a = myClassInstance.declare('Wrong parameter type')
const b = myClassInstance.declare({
first: true,
second: false,
})
console.log(b.data.first)
console.log(b.data.wrongProperty)
answered Nov 17 '18 at 15:06
Titian Cernicova-DragomirTitian Cernicova-Dragomir
59.9k33553
59.9k33553
It still doesn't catch the parameter type from what I just tested. You can double-check on the playground (the code sharing link is too long to insert it in a comment).
– Edouard Hienrichs
Nov 17 '18 at 15:17
@EdouardHienrichs not usre what you mean .. I get an error onb.data.wrongProperty. It will not affect the type of the originalmyClassInstance
– Titian Cernicova-Dragomir
Nov 17 '18 at 16:39
Sorry @titian-cernicova-dragomir I indeed forgot the second return declaration asMyClass<U>. It works, thank you very much !
– Edouard Hienrichs
Nov 19 '18 at 2:02
add a comment |
It still doesn't catch the parameter type from what I just tested. You can double-check on the playground (the code sharing link is too long to insert it in a comment).
– Edouard Hienrichs
Nov 17 '18 at 15:17
@EdouardHienrichs not usre what you mean .. I get an error onb.data.wrongProperty. It will not affect the type of the originalmyClassInstance
– Titian Cernicova-Dragomir
Nov 17 '18 at 16:39
Sorry @titian-cernicova-dragomir I indeed forgot the second return declaration asMyClass<U>. It works, thank you very much !
– Edouard Hienrichs
Nov 19 '18 at 2:02
It still doesn't catch the parameter type from what I just tested. You can double-check on the playground (the code sharing link is too long to insert it in a comment).
– Edouard Hienrichs
Nov 17 '18 at 15:17
It still doesn't catch the parameter type from what I just tested. You can double-check on the playground (the code sharing link is too long to insert it in a comment).
– Edouard Hienrichs
Nov 17 '18 at 15:17
@EdouardHienrichs not usre what you mean .. I get an error on
b.data.wrongProperty. It will not affect the type of the original myClassInstance– Titian Cernicova-Dragomir
Nov 17 '18 at 16:39
@EdouardHienrichs not usre what you mean .. I get an error on
b.data.wrongProperty. It will not affect the type of the original myClassInstance– Titian Cernicova-Dragomir
Nov 17 '18 at 16:39
Sorry @titian-cernicova-dragomir I indeed forgot the second return declaration as
MyClass<U>. It works, thank you very much !– Edouard Hienrichs
Nov 19 '18 at 2:02
Sorry @titian-cernicova-dragomir I indeed forgot the second return declaration as
MyClass<U>. It works, thank you very much !– Edouard Hienrichs
Nov 19 '18 at 2:02
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%2f53352289%2fis-it-possible-to-infer-a-generic-class-type-from-a-non-constructor-method-param%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