Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unnecessary-condition] add additional test ca…
Browse files Browse the repository at this point in the history
…ses for branded key type's index access
  • Loading branch information
yf-yang committed Nov 17, 2023
1 parent 728a4fb commit 3b6693d
Showing 1 changed file with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,102 @@ declare const foo: Foo;
declare const key: Key;
foo?.[key]?.trim();
`,
{
code: `
type BrandedKey = string & { __brand: string };
type Foo = { [key: BrandedKey]: string } | null;
declare const foo: Foo;
const key = '1' as BrandedKey;
foo?.[key]?.trim();
`,
parserOptions: {
EXPERIMENTAL_useProjectService: false,
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig.noUncheckedIndexedAccess.json',
},
dependencyConstraints: {
typescript: '4.1',
},
},
{
code: `
type BrandedKey<S extends string> = S & { __brand: string };
type Foo = { [key: string]: string; foo: 'foo'; bar: 'bar' } | null;
type Key = BrandedKey<'bar'> | BrandedKey<'foo'>;
declare const foo: Foo;
declare const key: Key;
foo?.[key].trim();
`,
parserOptions: {
EXPERIMENTAL_useProjectService: false,
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig.noUncheckedIndexedAccess.json',
},
dependencyConstraints: {
typescript: '4.1',
},
},
{
code: `
type BrandedKey = string & { __brand: string };
interface Outer {
inner?: {
[key: BrandedKey]: string | undefined;
};
}
function Foo(outer: Outer, key: BrandedKey): number | undefined {
return outer.inner?.[key]?.charCodeAt(0);
}
`,
parserOptions: {
EXPERIMENTAL_useProjectService: false,
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig.noUncheckedIndexedAccess.json',
},
dependencyConstraints: {
typescript: '4.1',
},
},
{
code: `
interface Outer {
inner?: {
[key: string & { __brand: string }]: string | undefined;
bar: 'bar';
};
}
type Foo = 'foo' & { __brand: string };
function Foo(outer: Outer, key: Foo): number | undefined {
return outer.inner?.[key]?.charCodeAt(0);
}
`,
parserOptions: {
EXPERIMENTAL_useProjectService: false,
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig.noUncheckedIndexedAccess.json',
},
dependencyConstraints: {
typescript: '4.1',
},
},
{
code: `
type BrandedKey<S extends string> = S & { __brand: string };
type Foo = { [key: string]: string; foo: 'foo'; bar: 'bar' } | null;
type Key = BrandedKey<'bar'> | BrandedKey<'foo'> | BrandedKey<'baz'>;
declare const foo: Foo;
declare const key: Key;
foo?.[key]?.trim();
`,
parserOptions: {
EXPERIMENTAL_useProjectService: false,
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig.noUncheckedIndexedAccess.json',
},
dependencyConstraints: {
typescript: '4.1',
},
},
`
let latencies: number[][] = [];
Expand Down

0 comments on commit 3b6693d

Please sign in to comment.