mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 16:38:05 -06:00
infer number index signature in JS object literals (#26221)
Fixes: #26208
This commit is contained in:
parent
16343208c8
commit
7299bceafb
@ -16836,7 +16836,7 @@ namespace ts {
|
||||
|
||||
function createObjectLiteralType() {
|
||||
const stringIndexInfo = hasComputedStringProperty ? getObjectLiteralIndexInfo(node.properties, offset, propertiesArray, IndexKind.String) : undefined;
|
||||
const numberIndexInfo = hasComputedNumberProperty && !isJSObjectLiteral ? getObjectLiteralIndexInfo(node.properties, offset, propertiesArray, IndexKind.Number) : undefined;
|
||||
const numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node.properties, offset, propertiesArray, IndexKind.Number) : undefined;
|
||||
const result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
|
||||
const freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : TypeFlags.FreshLiteral;
|
||||
result.flags |= TypeFlags.ContainsObjectLiteral | freshObjectLiteralFlag | (typeFlags & TypeFlags.PropagatingFlags);
|
||||
|
||||
@ -0,0 +1,23 @@
|
||||
//// [file.js]
|
||||
// @ts-check
|
||||
|
||||
let n = Math.random();
|
||||
let s = `${n}`;
|
||||
|
||||
const numericIndex = { [n]: 1 };
|
||||
numericIndex[n].toFixed();
|
||||
|
||||
const stringIndex = { [s]: 1 };
|
||||
stringIndex[s].toFixed();
|
||||
|
||||
|
||||
|
||||
//// [file.js]
|
||||
// @ts-check
|
||||
var _a, _b;
|
||||
var n = Math.random();
|
||||
var s = "" + n;
|
||||
var numericIndex = (_a = {}, _a[n] = 1, _a);
|
||||
numericIndex[n].toFixed();
|
||||
var stringIndex = (_b = {}, _b[s] = 1, _b);
|
||||
stringIndex[s].toFixed();
|
||||
@ -0,0 +1,36 @@
|
||||
=== tests/cases/compiler/file.js ===
|
||||
// @ts-check
|
||||
|
||||
let n = Math.random();
|
||||
>n : Symbol(n, Decl(file.js, 2, 3))
|
||||
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
|
||||
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
|
||||
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
|
||||
|
||||
let s = `${n}`;
|
||||
>s : Symbol(s, Decl(file.js, 3, 3))
|
||||
>n : Symbol(n, Decl(file.js, 2, 3))
|
||||
|
||||
const numericIndex = { [n]: 1 };
|
||||
>numericIndex : Symbol(numericIndex, Decl(file.js, 5, 5))
|
||||
>[n] : Symbol([n], Decl(file.js, 5, 22))
|
||||
>n : Symbol(n, Decl(file.js, 2, 3))
|
||||
|
||||
numericIndex[n].toFixed();
|
||||
>numericIndex[n].toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
|
||||
>numericIndex : Symbol(numericIndex, Decl(file.js, 5, 5))
|
||||
>n : Symbol(n, Decl(file.js, 2, 3))
|
||||
>toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
|
||||
|
||||
const stringIndex = { [s]: 1 };
|
||||
>stringIndex : Symbol(stringIndex, Decl(file.js, 8, 5))
|
||||
>[s] : Symbol([s], Decl(file.js, 8, 21))
|
||||
>s : Symbol(s, Decl(file.js, 3, 3))
|
||||
|
||||
stringIndex[s].toFixed();
|
||||
>stringIndex[s].toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
|
||||
>stringIndex : Symbol(stringIndex, Decl(file.js, 8, 5))
|
||||
>s : Symbol(s, Decl(file.js, 3, 3))
|
||||
>toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
|
||||
|
||||
|
||||
@ -0,0 +1,46 @@
|
||||
=== tests/cases/compiler/file.js ===
|
||||
// @ts-check
|
||||
|
||||
let n = Math.random();
|
||||
>n : number
|
||||
>Math.random() : number
|
||||
>Math.random : () => number
|
||||
>Math : Math
|
||||
>random : () => number
|
||||
|
||||
let s = `${n}`;
|
||||
>s : string
|
||||
>`${n}` : string
|
||||
>n : number
|
||||
|
||||
const numericIndex = { [n]: 1 };
|
||||
>numericIndex : { [x: number]: number; }
|
||||
>{ [n]: 1 } : { [x: number]: number; }
|
||||
>[n] : number
|
||||
>n : number
|
||||
>1 : 1
|
||||
|
||||
numericIndex[n].toFixed();
|
||||
>numericIndex[n].toFixed() : string
|
||||
>numericIndex[n].toFixed : (fractionDigits?: number) => string
|
||||
>numericIndex[n] : number
|
||||
>numericIndex : { [x: number]: number; }
|
||||
>n : number
|
||||
>toFixed : (fractionDigits?: number) => string
|
||||
|
||||
const stringIndex = { [s]: 1 };
|
||||
>stringIndex : { [x: string]: number; }
|
||||
>{ [s]: 1 } : { [x: string]: number; }
|
||||
>[s] : number
|
||||
>s : string
|
||||
>1 : 1
|
||||
|
||||
stringIndex[s].toFixed();
|
||||
>stringIndex[s].toFixed() : string
|
||||
>stringIndex[s].toFixed : (fractionDigits?: number) => string
|
||||
>stringIndex[s] : number
|
||||
>stringIndex : { [x: string]: number; }
|
||||
>s : string
|
||||
>toFixed : (fractionDigits?: number) => string
|
||||
|
||||
|
||||
15
tests/cases/compiler/checkJsObjectLiteralIndexSignatures.ts
Normal file
15
tests/cases/compiler/checkJsObjectLiteralIndexSignatures.ts
Normal file
@ -0,0 +1,15 @@
|
||||
// @allowJs: true
|
||||
// @noImplicitAny: true
|
||||
// @outDir: ./out
|
||||
// @filename: file.js
|
||||
// @ts-check
|
||||
|
||||
let n = Math.random();
|
||||
let s = `${n}`;
|
||||
|
||||
const numericIndex = { [n]: 1 };
|
||||
numericIndex[n].toFixed();
|
||||
|
||||
const stringIndex = { [s]: 1 };
|
||||
stringIndex[s].toFixed();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user