Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer types of arguments more precisely #3123

Merged
merged 5 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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