#Type 'unknown' is not assignable to type

2 messages · Page 1 of 1 (latest)

vale grail
#
export type TradeAlgorithmResult = {
  [key: string]: Array<unknown>
  positions: Array<Position>
}

export type TradeAlgorithm = (props?: unknown) => (candles: Array<Candle>) => TradeAlgorithmResult
export type RSIReversalAlgorithmProps = {
  rsiLength: number
  smaLength: number
}

export const rsiReversalAlgorithm: TradeAlgorithm = ({ rsiLength, smaLength }: RSIReversalAlgorithmProps) => {
  return (candles) => {
    // ...
  }
}

This gives the error:

Type '({ rsiLength, smaLength }: RSIReversalAlgorithmProps) => (candles: Candle[]) => { positions: Position[]; rsiValues: number[]; smaValues: number[]; }' is not assignable to type 'TradeAlgorithm'.
  Types of parameters '__0' and 'props' are incompatible.
    Type 'unknown' is not assignable to type 'RSIReversalAlgorithmProps'.ts(2322)

How do I fix this?

shadow gale
#

function types are contravariant in their parameter types (in TS/C# terminology the arguments are in parameters, not out). for example that means (x: never) => string is a supertype of (x: number) => string, and (x: unknown) => string is a subtype of it

so you can solve your problem by making TradeAlgorithm's type wider:

export type TradeAlgorithm = (props: never) => (candles: Array<Candle>) => TradeAlgorithmResult

that may not be what you actually want, though, depending on how TradeAlgorithm is used. it might be better to make it generic or something, but if you want advice on that you'll have to share more details