Skip to content
Open
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
15 changes: 15 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13765,6 +13765,21 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
undefined;
}
if (t.flags & TypeFlags.Index) {
if ((t as IndexType).type.flags & TypeFlags.IndexedAccess) {
const indexedAccess = (t as IndexType).type as IndexedAccessType;
// generic objects can always be instantiated with more keys so we can't narrow down those cases
if (!isGenericObjectType(indexedAccess.objectType)) {
const baseIndexType = getBaseConstraint(indexedAccess.indexType);
const indexedAccessType = baseIndexType && getIndexedAccessTypeOrUndefined(indexedAccess.objectType, baseIndexType);
const mappedIndexTypeOfIndexedAccess = indexedAccessType && mapType(indexedAccessType, getIndexType);
const narrowed = mappedIndexTypeOfIndexedAccess && filterType(keyofConstraintType, t => {
return !(getIntersectionType([t, mappedIndexTypeOfIndexedAccess]).flags & TypeFlags.Never);
});
if (narrowed) {
return narrowed;
}
}
}
return keyofConstraintType;
}
if (t.flags & TypeFlags.TemplateLiteral) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
keyofAndGenericIndexedAccessNarrowableConstraint.ts(15,15): error TS2731: Implicit conversion of a 'symbol' to a 'string' will fail at runtime. Consider wrapping this expression in 'String(...)'.
keyofAndGenericIndexedAccessNarrowableConstraint.ts(15,20): error TS2731: Implicit conversion of a 'symbol' to a 'string' will fail at runtime. Consider wrapping this expression in 'String(...)'.
keyofAndGenericIndexedAccessNarrowableConstraint.ts(19,16): error TS2731: Implicit conversion of a 'symbol' to a 'string' will fail at runtime. Consider wrapping this expression in 'String(...)'.


==== keyofAndGenericIndexedAccessNarrowableConstraint.ts (3 errors) ====
export type Context = {
V1: { a: string },
V2: { b: string },
}

export function path<V extends keyof Context, K extends keyof Context[V]>(v: V, k: K) {
return `${v}.${k}` // ok
}

export function path2<K extends keyof Context[keyof Context]>(k: K) {
return `.${k}` // ok
}

export function path3<O extends Context, V extends keyof O, K extends keyof O[V]>(v: V, k: K) {
return `${v}.${k}` // error
~
!!! error TS2731: Implicit conversion of a 'symbol' to a 'string' will fail at runtime. Consider wrapping this expression in 'String(...)'.
~
!!! error TS2731: Implicit conversion of a 'symbol' to a 'string' will fail at runtime. Consider wrapping this expression in 'String(...)'.
}

export function path4<O extends Context[keyof Context], K extends keyof O>(k: K) {
return `.${k}` // error
~
!!! error TS2731: Implicit conversion of a 'symbol' to a 'string' will fail at runtime. Consider wrapping this expression in 'String(...)'.
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//// [tests/cases/conformance/types/keyof/keyofAndGenericIndexedAccessNarrowableConstraint.ts] ////

=== keyofAndGenericIndexedAccessNarrowableConstraint.ts ===
export type Context = {
>Context : Symbol(Context, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 0, 0))

V1: { a: string },
>V1 : Symbol(V1, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 0, 23))
>a : Symbol(a, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 1, 9))

V2: { b: string },
>V2 : Symbol(V2, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 1, 22))
>b : Symbol(b, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 2, 9))
}

export function path<V extends keyof Context, K extends keyof Context[V]>(v: V, k: K) {
>path : Symbol(path, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 3, 1))
>V : Symbol(V, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 5, 21))
>Context : Symbol(Context, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 0, 0))
>K : Symbol(K, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 5, 45))
>Context : Symbol(Context, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 0, 0))
>V : Symbol(V, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 5, 21))
>v : Symbol(v, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 5, 74))
>V : Symbol(V, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 5, 21))
>k : Symbol(k, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 5, 79))
>K : Symbol(K, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 5, 45))

return `${v}.${k}` // ok
>v : Symbol(v, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 5, 74))
>k : Symbol(k, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 5, 79))
}

export function path2<K extends keyof Context[keyof Context]>(k: K) {
>path2 : Symbol(path2, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 7, 1))
>K : Symbol(K, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 9, 22))
>Context : Symbol(Context, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 0, 0))
>Context : Symbol(Context, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 0, 0))
>k : Symbol(k, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 9, 62))
>K : Symbol(K, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 9, 22))

return `.${k}` // ok
>k : Symbol(k, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 9, 62))
}

export function path3<O extends Context, V extends keyof O, K extends keyof O[V]>(v: V, k: K) {
>path3 : Symbol(path3, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 11, 1))
>O : Symbol(O, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 13, 22))
>Context : Symbol(Context, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 0, 0))
>V : Symbol(V, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 13, 40))
>O : Symbol(O, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 13, 22))
>K : Symbol(K, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 13, 59))
>O : Symbol(O, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 13, 22))
>V : Symbol(V, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 13, 40))
>v : Symbol(v, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 13, 82))
>V : Symbol(V, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 13, 40))
>k : Symbol(k, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 13, 87))
>K : Symbol(K, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 13, 59))

return `${v}.${k}` // error
>v : Symbol(v, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 13, 82))
>k : Symbol(k, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 13, 87))
}

export function path4<O extends Context[keyof Context], K extends keyof O>(k: K) {
>path4 : Symbol(path4, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 15, 1))
>O : Symbol(O, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 17, 22))
>Context : Symbol(Context, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 0, 0))
>Context : Symbol(Context, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 0, 0))
>K : Symbol(K, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 17, 55))
>O : Symbol(O, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 17, 22))
>k : Symbol(k, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 17, 75))
>K : Symbol(K, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 17, 55))

return `.${k}` // error
>k : Symbol(k, Decl(keyofAndGenericIndexedAccessNarrowableConstraint.ts, 17, 75))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//// [tests/cases/conformance/types/keyof/keyofAndGenericIndexedAccessNarrowableConstraint.ts] ////

=== keyofAndGenericIndexedAccessNarrowableConstraint.ts ===
export type Context = {
>Context : { V1: { a: string;}; V2: { b: string;}; }

V1: { a: string },
>V1 : { a: string; }
>a : string

V2: { b: string },
>V2 : { b: string; }
>b : string
}

export function path<V extends keyof Context, K extends keyof Context[V]>(v: V, k: K) {
>path : <V extends keyof Context, K extends keyof Context[V]>(v: V, k: K) => string
>v : V
>k : K

return `${v}.${k}` // ok
>`${v}.${k}` : string
>v : V
>k : K
}

export function path2<K extends keyof Context[keyof Context]>(k: K) {
>path2 : <K extends never>(k: K) => string
>k : K

return `.${k}` // ok
>`.${k}` : string
>k : K
}

export function path3<O extends Context, V extends keyof O, K extends keyof O[V]>(v: V, k: K) {
>path3 : <O extends Context, V extends keyof O, K extends keyof O[V]>(v: V, k: K) => string
>v : V
>k : K

return `${v}.${k}` // error
>`${v}.${k}` : string
>v : V
>k : K
}

export function path4<O extends Context[keyof Context], K extends keyof O>(k: K) {
>path4 : <O extends { a: string; } | { b: string; }, K extends keyof O>(k: K) => string
>k : K

return `.${k}` // error
>`.${k}` : string
>k : K
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @noEmit: true
// @strict: true

export type Context = {
V1: { a: string },
V2: { b: string },
}

export function path<V extends keyof Context, K extends keyof Context[V]>(v: V, k: K) {
return `${v}.${k}` // ok
}

export function path2<K extends keyof Context[keyof Context]>(k: K) {
return `.${k}` // ok
}

export function path3<O extends Context, V extends keyof O, K extends keyof O[V]>(v: V, k: K) {
return `${v}.${k}` // error
}

export function path4<O extends Context[keyof Context], K extends keyof O>(k: K) {
return `.${k}` // error
}