-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
When you have something like this (also see a playground):
function makeFoo<T>(fn: () => T): () => T {
// do some work here ...
return function() {
// ... or here
return fn()
}
}
const obj = {
name: 'test',
foo: makeFoo(() => obj.name.length)
}
type of obj
will be infered as any
, because in order to determine type of obj
typescript first looks at return type of makeFoo(...)
which uses obj
.
The actual error is this: 'obj' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.(7022)
What if we could give some hint to a compiler to handle this case? Maybe like this:
const obj = {
name: 'test',
foo: makeFoo(() => obj.name.length) as prop // or a better name
}
Right now I have a workaround for this:
function getter<T, R>(t: T, k: keyof T, value: R) {
Object.defineProperty(t, k, {value, enumerable: true})
return value
}
const obj = {
name: 'test',
get foo() {
return getter(obj, 'foo', makeFoo(() => obj.name.length))
}
}
but it would be cool if it could be done more easily.
theScottyJam, s97712, thorn0 and coder-ka
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed