Skip to content

Commit

Permalink
Infer types of arguments more precisely (#3123)
Browse files Browse the repository at this point in the history
* Prefer inferring types of nodes as tuples

* Implement for IndexNode

* Test for types

* Use tuple type for array node

---------

Co-authored-by: Jos de Jong <[email protected]>
  • Loading branch information
sylee957 and josdejong authored Jan 15, 2024
1 parent e3d6c74 commit e806813
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
19 changes: 19 additions & 0 deletions test/typescript-tests/testTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2523,3 +2523,22 @@ min/max return types
number | BigNumber | Fraction | Complex | Unit
>()
}

/*
Match types of exact positional arguments.
*/
{
const node1 = new ConstantNode(2)
const node2 = new SymbolNode('x')
const node3 = new FunctionNode('sqrt', [node2])
const node4 = new OperatorNode('+', 'add', [node1, node3])
expectTypeOf(node4.args[0]).toMatchTypeOf<ConstantNode>()
expectTypeOf(node4.args[1].args[0]).toMatchTypeOf<SymbolNode>()
}
{
const node1 = new ConstantNode(2)
const node2 = new SymbolNode('x')
const node3 = new ArrayNode([node1, node2])
expectTypeOf(node3.items[0]).toMatchTypeOf<ConstantNode>()
expectTypeOf(node3.items[1]).toMatchTypeOf<SymbolNode>()
}
22 changes: 11 additions & 11 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ export interface ArrayNode<TItems extends MathNode[] = MathNode[]>
extends MathNode {
type: 'ArrayNode'
isArrayNode: true
items: TItems
items: [...TItems]
}
export interface ArrayNodeCtor {
new <TItems extends MathNode[] = MathNode[]>(
items: MathNode[]
items: [...TItems]
): ArrayNode<TItems>
}

Expand Down Expand Up @@ -283,12 +283,12 @@ export interface FunctionNode<
type: 'FunctionNode'
isFunctionNode: true
fn: TFn
args: TArgs
args: [...TArgs]
}
export interface FunctionNodeCtor {
new <TFn = SymbolNode, TArgs extends MathNode[] = MathNode[]>(
fn: TFn,
args: TArgs
args: [...TArgs]
): FunctionNode<TFn, TArgs>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onUndefinedFunction: (name: string) => any
Expand All @@ -298,13 +298,13 @@ export interface IndexNode<TDims extends MathNode[] = MathNode[]>
extends MathNode {
type: 'IndexNode'
isIndexNode: true
dimensions: TDims
dimensions: [...TDims]
dotNotation: boolean
}
export interface IndexNodeCtor {
new <TDims extends MathNode[] = MathNode[]>(dimensions: TDims): IndexNode
new <TDims extends MathNode[] = MathNode[]>(dimensions: [...TDims]): IndexNode
new <TDims extends MathNode[] = MathNode[]>(
dimensions: TDims,
dimensions: [...TDims],
dotNotation: boolean
): IndexNode<TDims>
}
Expand Down Expand Up @@ -367,7 +367,7 @@ export interface OperatorNode<
isOperatorNode: true
op: TOp
fn: TFn
args: TArgs
args: [...TArgs]
implicit: boolean
isUnary(): boolean
isBinary(): boolean
Expand All @@ -381,7 +381,7 @@ export interface OperatorNodeCtor extends MathNode {
>(
op: TOp,
fn: TFn,
args: TArgs,
args: [...TArgs],
implicit?: boolean
): OperatorNode<TOp, TFn, TArgs>
}
Expand Down Expand Up @@ -423,12 +423,12 @@ export interface RelationalNode<TParams extends MathNode[] = MathNode[]>
type: 'RelationalNode'
isRelationalNode: true
conditionals: string[]
params: TParams
params: [...TParams]
}
export interface RelationalNodeCtor {
new <TParams extends MathNode[] = MathNode[]>(
conditionals: string[],
params: TParams
params: [...TParams]
): RelationalNode<TParams>
}

Expand Down

0 comments on commit e806813

Please sign in to comment.