打字稿说我的对象缺少属性,因为它并不缺少
我正在研究一个具有以下模式的项目:
interface IAuthProvider {
middleware: () => void
getInput: () => void
}
class Password implements IAuthProvider {
middleware = () => {}
getInput = () => {}
}
class Email implements IAuthProvider {
middleware = () => {}
getInput = () => {}
}
const providerNames = ['Email', 'Password']
type AuthProviderNamespace = { [K in typeof providerNames[number]]: IAuthProvider }
// error: Type 'typeof Password' is missing the following properties from type 'IAuthProvider': middleware, getInput
const providers: AuthProviderNamespace = { Email, Password }
这种对象将在模块中导入并直接传递到将遍历它的函数:
import * as AuthProviders from './providers/index.ts'
const providers = instantiateProviders(AuthProviders)
我需要使用authprovidernamespace
,因为 我需要键入authProviders
。但是我做错了一些事情,因此Typescript无法将电子邮件和密码识别为IAUTHPROVIDER的实施者。我可以解决这个问题吗?
I'm working on a project that has the following pattern:
interface IAuthProvider {
middleware: () => void
getInput: () => void
}
class Password implements IAuthProvider {
middleware = () => {}
getInput = () => {}
}
class Email implements IAuthProvider {
middleware = () => {}
getInput = () => {}
}
const providerNames = ['Email', 'Password']
type AuthProviderNamespace = { [K in typeof providerNames[number]]: IAuthProvider }
// error: Type 'typeof Password' is missing the following properties from type 'IAuthProvider': middleware, getInput
const providers: AuthProviderNamespace = { Email, Password }
I need to use AuthProviderNamespace
because this kind of object will be imported in a module and passed directly to a function that will iterate over it:
import * as AuthProviders from './providers/index.ts'
const providers = instantiateProviders(AuthProviders)
And so I need to type AuthProviders
. But I'm doing something wrong such that typescript doesn't recognize Email and Password as implementers of IAuthProvider. Can I fix this?
Playground: https://tsplay.dev/wQV7jN
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误是正确的。值
{电子邮件:电子邮件,密码:密码}
是一个对象,其属性为iauthprovider
对象的每个构造函数。但是类型{email:iauthprovider,密码:iauthprovider}
对应于emaim> email> email
和password
properties的对象iauthprovider
实例,而不是此类实例的构造函数。类构造函数电子邮件
没有getInput
属性,因此它不能是iauthprovider
:假设您使用
提供者是使用其属性来构建
iauthprovider
的新实例,如下:然后,您需要更改
authprovidernamespace
type type,以便其属性是iauthprovider 实例。您可以使用构造签名{new():iauthprovider} 或喜欢
new()=> IAuthProvider
:And then everything works as desired:
The error is correct. The value
{Email: Email, Password: Password}
is an object whose properties are each constructors ofIAuthProvider
objects. But the type{Email: IAuthProvider, Password: IAuthProvider}
corresponds to an object whoseEmail
andPassword
properties areIAuthProvider
instances, not constructors of such instances. The class constructorEmail
does not have agetInput
property, so it cannot be anIAuthProvider
:Assuming your intent with
providers
is to use its properties to construct new instances ofIAuthProvider
, like this:Then you need to change the
AuthProviderNamespace
type so that its properties are no-arg constructors ofIAuthProvider
instances. You can use a construct signature like{ new(): IAuthProvider }
or likenew () => IAuthProvider
:And then everything works as desired:
Playground link to code