TypeScript:省略只读对象文字 代码

问题描述

我需要确保 sorted() 的值必须是只读对象 old_list= ['list4','this1','my3','is2'] def extract_number(string): digits = ''.join([c for c in string if c.isdigit()]) return int(digits) new_list = sorted(old_list,key = extract_number) 的键,除了键 var option={ "phantomPath": "./node_modules/phantomjs/bin/phantomjs",} var fs = require('fs'); var pdf = require('html-pdf'); var html = fs.readFileSync('./test/businesscard.html','utf8'); pdf.create(html,options).toFile('./businesscard.pdf',function(err,res) { if (err) return console.log(err); console.log(res); // { filename: '/app/businesscard.pdf' } });

代码

bar

解决方法

Omit<T,K> 用于对象/记录和属性,您正在寻找适用于联合的 Exclude<T,U>

const bar: Exclude<keyof typeof FOO,'c'> = 'c'; // error

Playground

或者,对记录类型应用 Omit,然后对结果应用 keyof

const bar: keyof Omit<typeof FOO,'c'> = 'c'; // error