Estoy intentando cambiar el tipo de Entidad. Estoy llamando a una función que requiere 2 parámetros, 1 es la entidad y el otro es una ubicación. Aunque cuando intento pasar un Tipo para la primera entidad, me da este error:
Argument of type 'Node<EntityBasic>' is not assignable to parameter of type 'Node<AlertBasic>'. Type 'EntityBasic' is not assignable to type 'AlertBasic'. Property 'id' is optional in type 'EntityBasic' but required in type 'AlertBasic'.Esta es la función:
public async func() { const entBasic: EntityBasic = this.getEntity(entity); const NEO4J_TYPE = entType === 'alert' ? 'Alert' : 'Guide'; const entNode = await this.neo4jService.createOrUpdate<EntityBasic>( NEO4J_TYPE, entBasic.id, entBasic, ); if (NEO4J_TYPE === 'Alert') await this.calendarEventsSyncService.handleEvents(entNode, location); } Necesito cambiar el entNode de EntityBasic a AlertBasic
He intentado:
if (NEO4J_TYPE === 'Alert') await this.calendarEventsSyncService.handleEvents(entNode: AlertBasic, location); Pero obtengo un Expected 2 parameter instead of 3
Por lo que sé, solo puede usar entNode: AlertBasic para declarar una firma de función. No cuando llamas a una función, que es lo que estás haciendo.
if (NEO4J_TYPE === 'Alert') await this.calendarEventsSyncService.handleEvents(entNode: AlertBasic, location);Hay varias formas de manejar esto y cuál es la apropiada realmente depende del sistema más grande.
Una solución arriesgada: usar como
Puede usar as para decirle a TypeScript que finja que un tipo es otro. Esto podría generar problemas, ya que está introduciendo una mentira en su sistema.
if (NEO4J_TYPE === 'Alert') await this.calendarEventsSyncService.handleEvents(entNode as Node<AlertBasic>, location);transformar el tipo de antemano
Es posible que desee transformar un tipo en otro mediante una función. Con una firma como:
function transform_AlertBasic_toEntityBasic( alert_basic: AlertBasic ): EntityBasic { // ... some code that makes the transformation }comprobar si hay un supertipo
Puede haber una superclase que cubra una API para ambos tipos que puede usar.
Puede convertir entNode a Node<AlertBasic> :
await this.calendarEventsSyncService.handleEvents(entNode as Node<AlertBasic>, location); o en caso de conflicto de interfaz entre AlertBasic y EntityBasic :
await this.calendarEventsSyncService.handleEvents(entNode as unknown as Node<AlertBasic>, location);